Java Datatypes — some Quality stuff
Broadly divided as primitive and non primitive data types
Primitive types
These datatypes come out of the box in Java
(datatype-default-memory/size)
boolean— false — 1bit
int — 0–-4 bytes range 2-³¹ to 2³¹-1
byte — 0–-1 byte
short — 0–-2 bytes
long — 0L — 8 bytes
float — 0.0f — single precision- 4bytes
double — 0.0d-double precision — 8 bytes
char — ‘\u0000’ — Unicode character defaults to NUL or value of 0
Points to be noted:
please refer Unicode system- why preferred for Java.
We cannot assign null values to a primitive datatype
when we try to assign byte p = 130, which is greater than its range it will throw
Type mismatch: cannot convert from int to byte
We can use hexadecimal, Octal, decimal declarations for int datatype
int i =0X65 returns 101
based on 5*16⁰+6*16¹
Wrapper Class
Wrapper classes are used to map the primitive types in an object format.,As we all know that Collections don’t accept the primitives. Wrapper will be useful in that case
Example for a wrapper class
Integer i = new Integer(1);
or Integer i=1;
In our latest version of Java we don’t need to manually convert a primitive to Wrapper and vice-versa.
The concept of Autoboxing and Unboxing does it for us
int i=0;
Integer x= i;
will work for sure in Java 8 since Autoboxing is there
Unboxing works in the reverse
We can also convert from one datatype to another in a wrapper class using the parse functions
String x =”100";
Integer i = Integer.parseInt(x);
will convert it to Integer datatype
The wrapper classes are available for all the primitives..,
So that’s a quick refresh on the datatypes in Java.,
Have fun and Happy coding!!!!!