Java Data types
In order to store different types of data, Java has different data type specifiers. To store integer numbers, there are data types such as byte, short, int and long depending on how large or small value to be stored.
byte data typebyte can only store values between -128 and 127. byte occupies only 1 byte in memory.
Other number data types are explained in the table below.
float data typefloat and double are data types to store floating point numbers based on how large or small value to be stored.
char data typechar data type is used to store single character such as alphabets in upper case or lower case, digits or special characters. char data type is used to store unicode characters. char data type occupies 2 bytes.
boolean data typeboolean data type is used to store only one of two values - true or false. boolean values are not used to store the words true or false but the result of a condition like "is a equal to b?" or "is a>b ?" or any other condition. There is only one of the two possible results for a condition such as either true or false.
Size in memory and values that can be represented in all the Java data types are shown below.
|Java Data type||Size||Minimum Value||Maximum Value|
|byte||1 byte||-27 equal to -128||27-1 equal to 127|
|short||2 bytes||-215 equal to -32768||215-1 equal to 32767|
|int||4 bytes||-231 equal to -2147483648||231-1 equal to 2147483647|
|long||8 bytes||-263 equal to -9223372036854775808||263-1 equal to 9223372036854775807|
|float||4 bytes||Minimum Negative: -4286578688, |
|Largest positive: 2139095040|
|double||8 bytes||Minimum Negative: -4503599627370496,|
|Largest positive: 1.797693134862315708145274237317e+308|
|char||2 bytes||UTF-16 reprsentation.|
\u0000 to \uFFFF, \uD800 to \uDBFF, \uDC00 to \uDFFF
|Java Data type||Size|
|Java Data type||Minimum Value|
|byte||-27 equal to -128|
|short||-215 equal to -32768|
|int||-231 equal to -2147483648|
|long||-263 equal to -9223372036854775808|
|float||Minimum Negative: -4286578688, |
|double||Minimum Negative: -4503599627370496,|
\u0000 to \uFFFF, \uD800 to \uDBFF, \uDC00 to \uDFFF
|Java Data type||Maximum Value|
|byte||27-1 equal to 127|
|short||215-1 equal to 32767|
|int||231-1 equal to 2147483647|
|long||263-1 equal to 9223372036854775807|
|float||Largest positive: 2139095040|
|double||Largest positive: 1.797693134862315708|
Integer LiteralsInteger literals can be decimal literals, binary literals, octal literals or hexadecimal literals.
Decimal literalsDecimal literals correspond to decimal numbers in the number system. Decimal numbers are numbers in the regular usage. Decimal numbers are in base 10 and use the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9
Binary literalsBinary literals correspond to Binary numbers in the number system. Binary numbers are in base 2 and use the digits 0 and 1 only.
Binary Literals are represented in Java with leading 0b
Octal literalsOctal literals correspond to Octal numbers in the number system. Octal numbers are in base 8 and use the digits 0, 1, 2, 3, 4, 5, 6 and 7 only.
Octal Literals are represented in Java with leading 0
HexaDecimal literalsHexaDecimal literals correspond to HexaDecimal numbers in the number system. Decimal numbers are numbers in base 16 and use the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F
Hexadecimal Literals are represented in Java with leading 0x
long Literalslong Literals are represented in Java with numbers ending with L or l (uppercase or lowercase). However, it is better to represent using the uppercase L since lowercase appears similar to the number 1 and it could be confusing. long numbers are used when the numerals used in expressions return results, that are larger than what could be held in an int.
float Literalsfloat Literals are represented in Java with numbers ending with F or f (uppercase or lowercase)
double Literals are represented in Java with numbers ending with D or d (uppercase or lowercase)
char literals are literals that represent only one character. char literals are represented within single quotes. Since it is unicode, it can be used to represent any characters in various language character sets across the world. Unicode characters that are not available in keyboard are represented in Java using the escape \u followed by 4 hexadecimal digits. Examples are shown below.
- '\u0B85' to represent அ
- '\u0A20' to represent ਠ
- '\u03B1' to represent α
- '\u0232' to represent Ȳ
- '\u0041' to represent A
- '\u0031' to represent 1
Java also supports the following char literals to represent escape characters
- \b (backspace), \t (tab)
- \n (line feed), \f (form feed)
- \r (carriage return)
- \" (double quote), \' (single quote)
- \\ (backslash)
String literals can be stored in an instance of String. String data type is used to store characters, words, sentences and any amount of text content. While all other literals are stored in primitve data type, String literals are stored as objects.
- "There is a way"
- "How are you? Nice to meet you. 'I am good'"
Underscores in Numeric characters
Underscores can be used in numbers between digits similar to using commas while writing numbers.
Underscores can be used in the following places
- between digits or hexadecimal characters in Integers
- between digits or hexadecimal characters on either side of the decimal point in floating point numbers.
Underscores can not be used in the following places
- immediately before or after the decimal point in floating point numbers
- at the start or end of the numbers
- immediately before or after L or l used in long literals
- immediately before or after F or f used at the end of float literals
- immediately before or after D or d used at the end of double literals
- immediately before, after, or in middle of 0x used in hexadecimal literals
- immediately before, after, or in middle of 0b used in binary literals
- immediately before or after 0 used in Octal literals