There are majorly two types of languages. First one is Statically typed language where each variable and expression type is already known at compile time.Once a variable is declared to be of a certain data type, it cannot hold values of other data types.Example: C,C++, Java. Other, Dynamically typed languages: These languages can receive different data types over the time. Ruby, Python
Java is statically typed and also a strongly typed language because in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.
Java has two categories of data:
- Primitive data (e.g., number, character)
- Object data (programmer created types)
boolean: boolean data type represents only one bit of information either true or false . Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.
byte: The byte data type is an 8-bit signed two’s complement integer. The byte data type is useful for saving memory in large arrays.
- Size: 8-bit
- Value: -128 to 127
126 127 -128 -127
short: The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.
- Size: 16 bit
- Value: -32,768 to 32,767 (inclusive)
It is a 32-bit signed two’s complement integer.
- Size: 32 bit
- Value: -231 to 231-1
Note: In Java SE 8 and later, we can use the int data type to represent an unsigned 32-bit integer, which has value in range [0, 232-1]. Use the Integer class to use int data type as an unsigned integer.
long: The long data type is a 64-bit two’s complement integer.
- Size: 64 bit
- Value: -263 to 263-1.
Note: In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.
Floating point Numbers : float and double
float: The float data type is a single-precision 32-bit IEEE 754 floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers.
- Size: 32 bits
- Suffix : F/f Example: 9.8f
double: The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice.
Note: Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended not to use these data types and use BigDecimal class instead.
Please see this for details: Rounding off errors in Java
The char data type is a single 16-bit Unicode character. A char is a single character.
- Value: ‘\u0000’ (or 0) to ‘\uffff’ 65535
char: G integer: 89 byte: 4 short: 56 float: 4.7333436 double: 4.355453532
This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Rounding off errors in Java
- Enum with Customized Value in Java
- Loops in Java
- Java Identifiers
- Classes and Objects in Java
- Variables in Java
- throw and throws in Java
- Packages In Java
- How JVM Works - JVM Architecture?
- enum in Java
- Scope of Variables In Java
- Myth about the file name and class name in Java
- Does JVM create object of Main class (the class with main())?
- What are C++ features missing in Java?
- Are static local variables allowed in Java?
Improved By : ShreyasWaghmare