Data types in java with example
In java there are main 2 types of data types.
1. Primitive Datatype.
2. Non-Primitive Datatypes.
In Primitive also two types Numeric and Non-numeric datatypes.
First we learn Numeric datatypes...
(i)byte: byte is a shortest integer Datatype.byte datatype size is 8 bits and it's range of value is -128 to 127.
ex: // Example of byte datatype.
package com.jaks;
class main{
public static void main (String [] args){
byte A= 120;
byte B= 127;
System.out.println(A+B);
// output is 247.
// if we are take 128 value for byte it gets error.
}
}
now it's time for second type it is short.
(ii) short: short datatype size is 16 bits and also 2 bytes.It's default value is 0. it's range is -32768 to 32767.
ex:. //Example of short datatype.
package com.jaks;
class main{
public static void main (String [] args){
short A= 120;
short B= 127;
System.out.println(A+B);
// output is 247.
}
}
(iii) int: int is a most common used datatype in java. It's size is 32 bits. and 4 bytes.int default value is 0 and Range of value is -2147483648 to 2147483647.
ex: //Example of int datatype.
package com.jaks;
class main{
public static void main (String [] args){
int A= 120;
int B= 127;
System.out.println(A+B);
// output is 247.
}
}
(iv) long: long is a longest integer datatype.it's default value is 0 and it's size is 64 bits and also 8 byte.It's Range is very long.
-9223372036854775808 to 9223372036854775807.
// example of long datatypes.
package com.jaks;
class main{
public static void main (String [] args){
long A= 120;
long B= 127;
System.out.println(A+B);
// output is 247.
}
}
now let's learn floating point datatypes.
(1) float: float datatype is follow IEEE754 standard. It's size is 32 bits. float datatype print a value upto 7 decimal digits.
ex.: -1.23e100f, -3f, 3.14f ....
(2) double: this datatype is also follow IEEE754 standard. it's size is 64 bits and it's default value is 0.0. double datatype print a value upto 16 decimal digits.
ex.: 1.23456e300d , -12.45667e-300d, 1e1d...
char datatype : char datatype is follow unicode characters and it's size is 16 bits and it's follow ASCII value which is 0 to 255. it's default value is null.
Boolean datatype: this is 1 bit datatype. it's default value is false. Boolean has only to value true and false.
//Example of Boolean datatype.
boolean b=true;
boolean b=false;