Java Data Types
- Integer
Name Width(in bits) Range
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
short 16 –32,768 to 32,767
byte 8 –128 to 127
- Long : long is a signed 64-bit type and is useful storing a value that is beyond the range of integer. The range of a long is quite large which makes it an ideal choice for handling big values.
- Int : The int is the most widely used numerical data types. The main reason behind it is that it very efficient in handling most of the values that a programmer needs. For example for using loops, if statements and other such tasks int is the first choice because of its range(–2,147,483,648 to 2,147,483,647) and memory occupancy. If there is an integer expression involving bytes, shorts, ints, and literal numbers, the entire expression is promoted to int before the calculation is done.
- Short : Short is used for handling small numerical values. You can use it for storing any value that is between–32,768 to 32,767. This is the same range that int of C supports.
- Byte :The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to 127. Variables of type byte are especially useful when you’re working with a stream of data from a network or file. Java’s file handling mechanism uses this datatype extensively.
2. Floating-Point Types
Name Width(in Bits) Approximate Range
double 64 4.9e–324 to 1.8e+308
float 32 1.4e-045 to 3.4e+038
- Float :Variables of type float are useful when you need a fractional component (decimal value), but don’t require a large degree of precision. For example, float can be useful in handling weight,price,temperature etc where accuracy to the last digit of decimal is not compulsory.
- Double : In complex mathematical calculations where the decimal value of even the last place is very important the double data type is used. Although it requires more memory than float its capability to handle large decimal value is very effective.
3. Characters :
As we all know, everything is not numerical, there are many things that are alphabetical or symbolic in nature. The “char” datatype is used for store characters in Java. There is a difference between the char of C/C++ and of Java. In C/C++, char is an integer that occupies 1 bytes but Java uses Unicode to represent characters. Unicode is a international character set that can represent all of the characters found in all human languages. It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits or 2 bytes. The range of a char is 0 to 65,536. Any value to be stored in a character must be enclosed within a pair of single quotations (‘ ‘). We can store capital or small alphabets, digits or any symbol in a char variable as long as its length is 1.
4. Boolean :
Remember the “flag” variable that we use in C/C++ to mark the status of any event. It works as a true/false indicator for different conditions. You can use the same logic in Java also but apart from that Java has a different datatype known as “Boolean” that is used to store the values “true” or “false”. Since all the relational operators, such
Remember the “flag” variable that we use in C/C++ to mark the status of any event. It works as a true/false indicator for different conditions. You can use the same logic in Java also but apart from that Java has a different datatype known as “Boolean” that is used to store the values “true” or “false”. Since all the relational operators, such
as x>y return either true or false, we can use this datatype for handling such results.. boolean is also the type required by the conditional expressions that govern the control statements such as if and for.