What Are Data Types in Java ?
What Are Data Types in Java ?
Primitive data types are the types in java that can store a single value and do not provide any special capability.
Data types in Java are of different sizes and values that can be stored in the variable that is made as per convenience
and circumstances to cover up all test cases. Java has two categories in which data types are segregated
1. Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double
2. Non-Primitive Data Type or Object Data type: such as Class ,String, Array, etc.
twos-complement
byte 0 8 bits (none) -128 to 127
integer
twos-complement
short 0 16 bits (none) -32,768 to 32,767
integer
-2,147,483,648
twos-complement
int 0 32 bits -2,-1,0,1,2 to
intger
2,147,483,647
-9,223,372,036,854,775,808
twos-complement
long 0 64 bits -2L,-1L,0L,1L,2L to
integer
9,223,372,036,854,775,807
1.23e100f ,
IEEE 754 floating
float 0.0 32 bits -1.23e-100f , .3f upto 7 decimal digits
point
,3.14F
1.23456e300d ,
IEEE 754 floating
double 0.0 64 bits -123456e-300d , upto 16 decimal digits
point
1e1d
Let us discuss and implement each one of the following data types that are as follows:
boolean booleanVar;
The byte data type is useful for saving memory in large arrays.
Its value-range lies between -128 to 127 (inclusive).
Its minimum value is -128 and maximum value is 127.
Its default value is 0.
Syntax:
byte byteVar;
Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.
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.
Syntax:
short shortVar;
Syntax:
int intVar;
Remember: In Java SE 8 and later, we can use the int data type to represent an unsigned 32-
bit integer, which has a value in the range [0, 232-1]. Use the Integer class to use the int data
type as an unsigned integer.
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.
Syntax:
long longVar;
Remember: In Java SE 8 and later, you can use the long data type to represent an unsigned
64-bit long, which has a minimum value of 0 and a maximum value of 264-1. The Long class
also contains methods like comparing Unsigned, divide Unsigned, etc to support arithmetic
operations for unsigned long.
Syntax:
float floatVar;
double doubleVar;
Note: Both float and double data types were designed especially for scientific calculations,
where approximation errors are acceptable. If accuracy is the most prior concern then, it is
recommended not to use these data types and use BigDecimal class instead.
Syntax:
char charVar;
When copied Two different variables is created along with different Two reference variable is created but
assignment(only values are same) both are pointing to the same object on
Example byte, short, int, long, float, double, char, boolean array, string class, interface etc.
DURGA SIR :-
for itegral datatypes [byte,short,int,long] we can specifies literal value in the follwing base.
int x = 010;
for extra digit [ a to f ] [ A to F] we can use both uppercase and lowercase. characters. this is one place where java is not
casesensitive.
int x = 0x10;
int x= 0xFace;……..yes
int x= 0xBeef;……..yes
int x= 0xBeer;……..no
int x =10;…………...yes
long l =10L;…………..yes
int x = 10L;………………no
long l=10;…………...yes
float f =123.456;………no
float f =123.456F;………yes
double d = 123.456;………yes
double d = 123.456D;………..yes
float f =123.456d;……………no
double d = 123.456;…………….yes
double d = 0123.456;…………yes
double d = 0x123.456;…………no
double d =0786;…………..no
doible d=0xfaee;…………….yes
double d =0786.0;…………..yes
double d =0xface.0;…………no
double d=10;…………..yes
double d=0777;……………..yes
int x=10.0;……………..no
float f = 1.2e3F;….yes
float f = 1.2e3;…………no
boolean b =true;….yes
boolean b = 0;…..no
boolean b =True;……..no
boolean b =”true”;….yes
char ch=’a’;…………yes
char ch=a;………………..no
char ch= “a”;………..no
char ch= ‘ab’;……..no
char ch =0777;…..yes
char ch= oxFace;……….yes
char ch=’\t’;……..yes
char ch=’\m’;……..no
string r =”durga”;
int x = 10;…………10
int y = 010;………….8
int z=0x10;………….16
10L…...
010L……
0x10L……Long