Datatypes And Wrapper Class in Java

Java datatypes are attributes that identify the type of data an object can hold in Java so that the same amount of memory will be allocated. Datatypes can be divided into two types:

  • Primitive
  • Non primitive

Java supports 8 primitive data types and 2 non-primitive data types.

java datatypes

Why Java is Partially Object Oriented Language

Java is partially object oriented for the following reasons:

  • Java supports predefined data types which are not objects like int, char etc.
  • If a language supports primitive datatype the language cannot be pure object-oriented language.
  • In Java, data types do not support the concept of a cycle so beyond the range of the datatype it would be an error.
  • There is no signed and unsigned keyword available in Java so the default nature of datatype is always signed.

Note: Signed datatypes are those which can have both negative and positive values. Unsigned datatypes contain only positive values.

Details of Java Datatypes

java datatypes

Wrapper Class In Java

  • Java provides one class for each data type which is technically known as a wrapper class.
  • Wrapper class was introduced by SunMicro System to make Java pure object-oriented.
  • Wrapper class is generally used for the replacement of datatype in an object format.
  • A wrapper class is also used for type conversion.

Byte

  • The class Byte wraps a value of primitive type byte in an object.
  • Every negative number is stored in the memory in form of 2's complement.
  • To evaluate the value of negative binary user has to do 2's complement again.

For Example:

public class Demo {
public static void main(String[] args) {
	byte x=10;
	System.out.println(x); //10
	System.out.println(Byte.SIZE); //8
	System.out.println(Byte.MAX_VALUE); //127
	System.out.println(Byte.MIN_VALUE); //-128
	System.out.println(Byte.parseByte("10")+x); //20 
	System.out.println(Byte.toString(x)+50); //1050
}
}

Key points to note from the above program:

  1. Since parseByte and toString are static method, hence they are called using the class name Byte
  2. Size is a predefined constant present in each wrapper class which always returns the size of datatype in a bit format. As it is static in nature it can be called by class name.
  3. MAX_VALUE and MIN_VALUE are two predefined static constants present in each wrapper class which always return the max and min value corresponding to the datatype.
  4. parseXXX is a predefined static method present in every wrapper class which converts a String to a specific datatype. Here XXX can be Byte, Int, Double etc.
  5. toString is a static method present in each wrapper class which converts a datatype to string format.

Integer

public class Demo {
public static void main(String[] args) {
System.out.println(Integer.SIZE); //32
System.out.println(Integer.MAX_VALUE); //2147483647
System.out.println(Integer.MIN_VALUE); //-2147483648
System.out.println(Integer.toBinaryString(10)); //1010
System.out.println(Integer.toOctalString(10)); //12
System.out.println(Integer.toHexString(10)); //a
System.out.println(010+10); //18
System.out.println(0x10+10); //26
}
}

Keynotes from the above program: 

  1. If a number starts with 0 then it is an octal number.
  2. If a number starts with 0x then it's a hex number.

Float and Double

  • Float and double always store values in the form of exponent and mantissa.
  • Nature of any real constant is always Double.
  • 32-bit recurring binary is never equal to 64-bit recurring binary.

Let us look at an example:

public class Demo {
public static void main(String[] args) {
System.out.println(Float.MAX_VALUE); //3.4028235E38
System.out.println(Double.MAX_VALUE); //1.7976931348623157E308
System.out.println(4.0f==4.0); //true (32 bit is equal to 64 bit, 4.0 is double bydefault)
System.err.println(4.2f==4.2); //false (since 32 bit is not equal to 64 bit as it would be reccuring, 4.2 is double bydefault)
/*
If you still have doubts understanding the last two outputs
then convert 4.0, 4.2 to binary 32 bits and 64 bits.
You would see that for the 1st output the 32 bit binary is equal to 64 bit.
But, for the 2nd output it is not as there would be reccuring.
*/
}
}

Char

  • In Java size of a character is 2 byte so it supports 65536 number of characters.
  • Java supports Unicode (Unicode=ASCII+Regional characters) whereas C supports ASCII.

Let's have a look at the below example:



public class Demo {
public static void main(String[] args) {
char x='a';
System.out.println(x); //a
System.out.println((int)x); //97
System.out.println(Character.isDigit('4')); //true
System.out.println(Character.isLetter('a')); //true
System.out.println(Character.isUpperCase('A')); //true
System.out.println(Character.isLowerCase('a')); //true
System.out.println(Character.isWhitespace(' ')); //true
System.out.println('A'+10); //75
}
}

Boolean

  • True and false are the two boolean literals available in Java.
public class Demo {
public static void main(String[] args) {
boolean x=true;
System.out.println(x); //true
System.out.println(x==Boolean.parseBoolean("True")); //true
}
}