Saturday, December 27, 2008

What are Java Data Types

Welcome back to Java Code Online. You will come to know that Java care about its variables, and would not allow a larger value in a smaller variable. Every variable in Java can be classified as:-
1. Primitive
2. Reference

The data types in Java are concerned with the Primitive type variables, and I will only discuss these in this article. In my later article I will discuss about the reference variables.

In Java, we need variables to store values and perform operations, for these they need to be assigned some sort of data type. A Data Type defines the intrinsic property of the variable, like its range, the maximum and minimum value it can hold, etc. A correct data type need to be used for every operation in Java.

Java supports eight Data Types which could be grouped in four domains. They are:-
1. Integers (byte, short, int, long)
2. Floating Point Numbers (float, double)
3. Characters (char)
4. Boolean (boolean)

The names in the bracket are the actual Data Types used in Java. For example for a mathematical operation, most probably an Integer type or Floating type of data variable will be required. There might be values which are very large and may need a long or double data type. Similarly for a logical condition giving output as 1 or 0, or could be true or false needs to be assigned a Boolean data type. So we see that assigning a correct Data Type is essential for the correct operation of the Java Code.

I would be covering all these four data types in my next consecutive articles. I will also discuss, why and where to use which type of Data Type. And the most important thing is that why not to use the data type with the largest range if it is not required. As it is commonly seen, that while performing a mathematical operation, we assign the Data Type with the largest range, so as to avoid any range overflow. But this is a bad practice. We should be aware of, that where to use which type of Data Type and where not to.

I hope this article gave you a good glimpse of Java Data Type. If you liked this article then do leave a comment. For more info on Java, keep buzzing Java Code Online.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.