Data Types in Java

Data type defines the values that a variable can take, for example if a variable has int data type, it can only take integer values. In java we have two categories of data type: 1) Primitive data types 2) Non-primitive data types – Arrays and Strings are non-primitive data types, we will discuss them later in the coming tutorials. Here we will discuss primitive data types and literals in Java.

Java is a statically typed language. A language is statically typed, if the data type of a variable is known at compile time. This means that you must specify the type of the variable (Declare the variable) before you can use it.

In the last tutorial about Java Variables, we learned how to declare a variable, lets recall it:

int num;

So in order to use the variable num in our program, we must declare it first as shown above. It is a good programming practice to declare all the variables ( that you are going to use) in the beginning of the program.

1) Primitive data types

In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double. Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.

There are 8 types of primitive data types:
  • boolean data type
  • byte data type
  • char data type
  • short data type
  • int data type
  • long data type
  • float data type
  • double data type
Java Data Types
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Boolean Data Type

The Boolean data type is used to store only two possible values: true and false. This data type is used for simple flags that track true/false conditions.

The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.

class JavaExample { public static void main(String[] args) { boolean b = false; System.out.println(b); } }


Output:
false
Byte Data Type

This can hold whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type.

Default size of this data type: 1 byte.

Default value: 0

class JavaExample { public static void main(String[] args) { byte num; num = 113; System.out.println(num); } }


Output:
113
Short Data Type

The short data type is a 16-bit signed two's complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.

The short data type can also be used to save memory just like byte data type. A short data type is 2 times smaller than an integer.

class JavaExample { public static void main(String[] args) { short num; num = 150; System.out.println(num); } }


Output:
150
Int Data Type

The int data type is a 32-bit signed two's complement integer. Its value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is - 2,147,483,648and maximum value is 2,147,483,647. Its default value is 0.

The int data type is generally used as a default data type for integral values unless if there is no problem about memory.

class JavaExample { public static void main(String[] args) { int num; num = 3136; System.out.println(num); } }


Output:
3136
Long Data Type

The long data type is a 64-bit two's complement integer. 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). Its minimum value is - 9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a range of values more than those provided by int.

class JavaExample { public static void main(String[] args) { long num = -12332252626L; System.out.println(num); } }


Output:
-12332252626
Float Data Type

The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is unlimited. It is recommended to use a float (instead of double) if you need to save memory in large arrays of floating point numbers. The float data type should never be used for precise values, such as currency. Its default value is 0.0d.

class JavaExample { public static void main(String[] args) { float num = 19.98f; System.out.println(num); } }


Output:
19.98
Double Data Type

The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. The double data type is generally used for decimal values just like float. The double data type also should never be used for precise values, such as currency.Its default value is 0.0d.

class JavaExample { public static void main(String[] args) { double num = -42937737.9d; System.out.println(num); } }


Output:
-4.29377379E7
Char Data Type

The char data type is a single 16-bit Unicode character. Its value-range lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to store characters.

class JavaExample { public static void main(String[] args) { char ch = 'c'; System.out.println(ch); } }


Output:
c
Why char uses 2 byte in java and what is \u0000 ?

It is because java uses Unicode system not ASCII code system. The \u0000 is the lowest range of Unicode system. To get detail explanation about Unicode visit next page.

Literals in Java

A literal is a fixed value that we assign to a variable in a Program.

int num=10;

Here value 10 is a Integer literal.

char ch = 'A'; Here A is a char literal
Integer Literal

Integer literals are assigned to the variables of data type byte, short, int and long.

byte b = 100; short s = 200; int num = 13313131; long l = 928389283L;
Float Literals

Used for data type float and double.

double num1 = 22.4; float num2 = 22.4f;

Note: Always suffix float value with the “f” else compiler will consider it as double.

Char and String Literal

Used for char and String type.

char ch = 'Z'; String str = "cprogramcoding.com";



Instagram