Java Data types

<<Previous

Next >>




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

byte 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

float and double are data types to store floating point numbers based on how large or small value to be stored.

char

char 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

boolean 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 typeSizeMinimum ValueMaximum Value
byte1 byte-27 equal to -12827-1 equal to 127
short2 bytes-215 equal to -32768215-1 equal to 32767
int4 bytes-231 equal to -2147483648231-1 equal to 2147483647
long8 bytes-263 equal to -9223372036854775808263-1 equal to 9223372036854775807
float4 bytesMinimum Negative: -4286578688,
Smallest positive:
1.4012984643248170709237295832899e-45
Largest positive: 2139095040
double8 bytesMinimum Negative: -4503599627370496,
Smallest positive:
4.9406564584124654417656879286822e-324
Largest positive: 1.797693134862315708145274237317e+308
char2 bytesUTF-16 reprsentation.
\u0000 to \uFFFF, \uD800 to \uDBFF, \uDC00 to \uDFFF
booleantrue, false
Java Data typeSize
byte1 byte
short2 bytes
int4 bytes
long8 bytes
float4 bytes
double8 bytes
char2 bytes
boolean


Java Data typeMinimum Value
byte-27 equal to -128
short-215 equal to -32768
int-231 equal to -2147483648
long-263 equal to -9223372036854775808
floatMinimum Negative: -4286578688,
Smallest positive:
1.4012984643248170709237295832899e-45
doubleMinimum Negative: -4503599627370496,
Smallest positive:
4.9406564584124654417656879286822e-324
charUTF-16 reprsentation.
\u0000 to \uFFFF, \uD800 to \uDBFF, \uDC00 to \uDFFF
booleantrue, false


Java Data typeMaximum Value
byte27-1 equal to 127
short215-1 equal to 32767
int231-1 equal to 2147483647
long263-1 equal to 9223372036854775807
floatLargest positive: 2139095040
doubleLargest positive: 1.797693134862315708145274237317e+308

Literals

Integer Literals

Integer literals can be decimal literals, binary literals, octal literals or hexadecimal literals.

Decimal literals

Decimal 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
Examples:
  • 2
  • 4
  • 65000

Binary literals

Binary 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
Examples
  • 0b101011101
  • 0b1110
  • 0b101110
  • 0b1101

Octal literals

Octal 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
Examples
  • 0234
  • 045
  • 09000

HexaDecimal literals

HexaDecimal 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
  • 0x456
  • 0x5006
  • 0x56000

long Literals

long 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.
  • 2242003040L
  • 3400000000L
  • 4500000000L
  • 5600000000L
  • 6566600000330L
  • 5678L

float Literals

float Literals are represented in Java with numbers ending with F or f (uppercase or lowercase)
  • 45.67f
  • 105.678923f
  • 0.7f
  • 56.7e6f

double Literals

double Literals are represented in Java with numbers ending with D or d (uppercase or lowercase)

  • 45.67
  • 100.678
  • 0.7
  • 45.6e12
  • 56.7e6

char Literals

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.

  • 'c'
  • 'M'
  • '\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

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.

  • "Good"
  • "Morning"
  • "There is a way"
  • "How are you? Nice to meet you. 'I am good'"

boolean literals

  • true
  • false

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

<<Previous

Next >>