Different Data type in Java

There are 8 different data types, there are byte, short, int, long, char, float, double, boolean.

These data type are grouped in to 4 groups

Integers:-
These data type is used to define whole numbers like 2, 54, 69 etc.

This group include byte, short, int and long.

Example:-

class data_int{
public static void main(String arg[]){
int num1, num2,sum1;
// example for int
num1 = 10;
num2 = 20;
sum1 = num1+num2;
System.out.println("The sum of 2 value:"+sum1);
// example for long
long num3, num4, sum2;
num3=5869;
num4=89874;
sum2 = num3 + num4;
System.out.println("The sum of 2 value:"+sum2);
// example for short
short num5;
num5=889;
System.out.println("The num5 value is:"+num5);
// example for byte
byte num6;
num6=9;
System.out.println("The num5 value is:"+num6);
}
}

Output

The sum of 2 value:30
The sum of 2 value:95743
The num5 value is:889
The num5 value is:9

Floating Point:-
These one is used to define number with fractional precision like 2.5, 5.0, 6.689 etc.

These group include float and double.

class data_float{
public static void main(String arg[]){
float num1, num2, sum1;
// example for float
num1 = 5;
num2 = 2;
sum1 = num1+num2;
System.out.println("The sum of 2 value:"+sum1);
// example for double
double num3, num4 = 0, sum2;
num3=58.69;
num4=8987.4;
sum2 = num3 + num4;
System.out.println("The sum of 2 value:"+sum2);
}
}

Output

The sum of 2 value:7.0
The sum of 2 value:9046.09


Characters:-
Character is used to define character set like letters and numbers.

The Character group include only one data type char.

Example:-

class data_char{
public static void main(String arg[]){
char data;
// example for char
data = 'h';
System.out.println(data);
}
}

output

h

Boolean:-
Boolean is used to define either true or false.

This group include only one data type boolean

Example:-

class group{
public static void main(String arg[]){
boolean data1,data2;
// example for boolean
data1 = true;
data2= false;
System.out.println("data1:"+data1);
System.out.println("data2:"+data2);
}
}

Output

data1:true
data2:false



Data
type

Width

Range

long

64

-9,223,372,036,854,775,808
to 9,223,372,036,854,775,807

int

32

-2,147,483,648
to 2,147,483,647

short

16

-32,768
to 32,767

byte

8

-128
to 127

double

64

4.9e-324
to 1.8e+308

float

32

1.4e-045
to 3.4e+038

character

8

0
to 65,536

boolean

-

True
or false

Note:-
# Number cannot convert from double to float, but float can be convert to double.
# Number cannot convert from long to int but int can be convert to long.


Previous Code:-
Run Java Program

List Code:-
Java codes

Next Code:-
Keyword used in java

Java Programs:-
List of Java Programs

Leave reply

Add your comments here

Back to Top