Data Types in Java

In this tutorial we are going to learn about the data types in java, we will be deeply looking at the specified data types that go hand in hand with the variables that we declare in Java.


Data types in java divided in two sections:

data types in java


Primitive Data Types

These data types include int, long, byte, boolean, double, float and char.

Let’s look at the Integer part of the Primitive Data Types first:

Integers

Byte: Byte takes 1 byte of size in the memory and stores whole numbers between – 120 to 127. You can use byte when you are sure about adding a value that is in between -128 and 127.

Example:

byte num = 20;
System.out.println(num);

Short: The short data type takes 2 bytes of memory size and stores a range of numbers between -32,768 and 32,767.

Example:

short num = 3000;
System.out.println(num);

Int: The int data type stores range of whole numbers between -2147483648 to 214748347. This is the most used data type in Java, since we can safely put any number because the range of whole numbers in int data type is ginormous.

Recommended Books



Example:

int num = 10000;
System.out.println(num);

Long: The long data type can store a range of whole numbers between -9223372036854775808 to 9223372036854775807. We usually do not use this data type in Java a lot, but when our value is much bigger then we use the long data type. Always remember to end the value with capital ‘L’ when using the long data type.

Example:

long num = 1500000000L;
System.out.println(num);

Now let’s just look at the floating point primitive data types:

Floating Point Types

We use this kind of type when we need a number with a decimal, for example 0.99 or 1.12.

Float: Float data type can Store decimal values with exponents ranging from 3.4e-038 to 3.4e+038. Make sure that you end the float data type value with an ‘f’;

Example:

float num = 2.52f;
System.out.println(num);

Double: This data type can store numbers ranging between 1.7e-308 and 1.7e+308. Make sure you end the value of ‘double’ with a ‘d’ at the end.





Example:

double num = 10.11d;
System.out.println(num);

Note: Sometimes you might come across a situation, where you are confused whether to use a float data type or a double data type. There is no need to worry about it because the difference between float and double data type is about precision. The float data type can have 6 or 7 digits after the decimal whereas, double data type can have a precision of 15 digits after the decimal. Scientifically, it is vital to use the double data type for calculations.

Boolean

Boolean: This data type is used for storing only two values: True or false. The Boolean data type defines one state of data, but you can’t be sure about its size.

Example:

boolean num = true;
boolean num1 = false;
System.out.println(num); //Output is true
System.out.println(num1); //Output is false

Character

Character: This is used for storing a single character. Make sure you  use  the single quotes around the character when using the char the data type, for example: ‘b’.

Example:

char mychar = 'e';
System.out.println(mychar);

Non-Primitive Data Types

These data types include string, arrays, classes, which we will thoroughly discuss in the upcoming tutorials.