# 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 type

**can only store values between -128 and 127.**

*byte**byte*occupies only 1 byte in memory.

Other number data types are explained in the table below.

## float data type

**and**

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

*double*## char data type

**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.**

*char*## boolean data type

**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**

*boolean**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 | -2^{7} equal to -128 | 2^{7}-1 equal to 127 |

short | 2 bytes | -2^{15} equal to -32768 | 2^{15}-1 equal to 32767 |

int | 4 bytes | -2^{31} equal to -2147483648 | 2^{31}-1 equal to 2147483647 |

long | 8 bytes | -2^{63} equal to -9223372036854775808 | 2^{63}-1 equal to 9223372036854775807 |

float | 4 bytes | Minimum Negative: -4286578688, Smallest positive: 1.401298464324817070 9237295832899e-45 | Largest positive: 2139095040 |

double | 8 bytes | Minimum Negative: -4503599627370496, Smallest positive: 4.940656458412465441 7656879286822e-324 | Largest positive: 1.797693134862315708145274237317e+308 |

char | 2 bytes | UTF-16 reprsentation. \u0000 to \uFFFF, \uD800 to \uDBFF, \uDC00 to \uDFFF | |

boolean | true, false |

Java Data type | Size |
---|---|

byte | 1 byte |

short | 2 bytes |

int | 4 bytes |

long | 8 bytes |

float | 4 bytes |

double | 8 bytes |

char | 2 bytes |

boolean |

Java Data type | Minimum Value |
---|---|

byte | -2^{7} equal to -128 |

short | -2^{15} equal to -32768 |

int | -2^{31} equal to -2147483648 |

long | -2^{63} equal to -9223372036854775808 |

float | Minimum Negative: -4286578688, Smallest positive: 1.401298464324817070 9237295832899e-45 |

double | Minimum Negative: -4503599627370496, Smallest positive: 4.9406564584124654417 656879286822e-324 |

char | UTF-16 reprsentation. \u0000 to \uFFFF, \uD800 to \uDBFF, \uDC00 to \uDFFF |

boolean | true, false |

Java Data type | Maximum Value |
---|---|

byte | 2^{7}-1 equal to 127 |

short | 2^{15}-1 equal to 32767 |

int | 2^{31}-1 equal to 2147483647 |

long | 2^{63}-1 equal to 9223372036854775807 |

float | Largest positive: 2139095040 |

double | Largest positive: 1.797693134862315708 145274237317e+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 9Examples:

- 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 FHexadecimal Literals are represented in Java with leading 0x

- 0x456
- 0x5006
- 0x56000

### long Literals

**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.**

*long*- 2242003040L
- 3400000000L
- 4500000000L
- 5600000000L
- 6566600000330L
- 5678L

### float Literals

**Literals are represented in Java with numbers ending with F or f (uppercase or lowercase)**

*float*- 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