When you are Creating variables in Java, that time you need to define a data type with the varible name. The varible memory allocating (size) is depended on Java data types.
This Tutorial you will learn about basic of data types in Java.
There are 2 data types in Java, then further divided into many parts as bellow.
- Primitive data types: Predefined class data types include boolean, char, byte, short, int, long, float, and double.
- Non-primitive data types (Reference/Object): The non-primitive data types include Classes, Strings, Interface, and Arrays.
Primitive data types in Java are:
- boolean - The type whose values store or sates are either true or false
- char - The character type whose values are 16-bit Unicode characters, used for store character values.
- The arithmetic or Numeric types:
- integral types:
- byte - It's value-range lies between -128 to 127 (inclusive).
- short - It's value-range lies between -32,768 to 32,767 (inclusive)
- int - It's value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive).
- long - Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).
- floating-point types:
- float - It's default value is 0.0F.
- double - It's default value is 0.0d.
- integral types:
Java data types size and details.
Here is more details about data type with its default value, size and example.
Type | Description | Default | Size | Example Literals |
---|---|---|---|---|
boolean | true or false | false | 1 bit | true, false |
byte | twos complement integer | 0 | 8 bits | (none) |
char | Unicode character | \u0000 | 16 bits | 'a', '\u0041', '\101', '\\', '\'', '\n', 'ß' |
short | twos complement integer | 0 | 16 bits | (none) |
int | twos complement integer | 0 | 32 bits | -2, -1, 0, 1, 2 |
long | twos complement integer | 0 | 64 bits | -2L, -1L, 0L, 1L, 2L |
float | IEEE 754 floating point | 0.0 | 32 bits | 1.23e100f, -1.23e-100f, .3f, 3.14F |
double | IEEE 754 floating point | 0.0 | 64 bits | 1.23456e300d, -1.23456e-300d, 1e1d |
Java data types examples
Simple Primitive data type examples.
class Hello { public static void main(String args[]) { Boolean active = false; byte byteV = 10; short shortV = 400; int iintV = 100000; long longV = 100330L; float flotV = 84.5f; double doubleV = 19.8d; char letterA = 'E'; } }
Non – Primitive data type Examples
class Hello { public static void main(String args[]) { String name = "EyeHunts"; int a[] = new int[20]; //Array size of 20 } }
Must read below linked tutoiral based on Non – Primitive data type
- String Class | Syntax | Methods | Examples in Java
- Java Array | Initialization | Length | Methods | Types with Examples
- Java Class | Declaration | Types | Example
Do comment if you have any doubt and suggestion on this tutorial.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1Java version 11
All Java Data Types Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.