Krivalar Tutorials 
Krivalar Tutorials

Java - Wrapper classes


<<Previous

Next >>





Wrapper classes are a way to wrap primitive data types as objects.Java has primitive data types - byte, short, int, long, char, boolean, float and double. Java is object oriented except for the primitive data types. In order for primitive data types to be used comfortably with rest of the objects in Java, wrapper classes are useful. Once primitive data type is wrapped as an object, object can be added to any of the Java data structures like Vector, ArrayList, HashSet, HashMap. Wrapper classes are a way to wrap primitive data types as objects.



Primitive Data TypesWrapper classes
bytejava.lang.Byte
shortjava.lang.Short
intjava.lang.Integer
longjava.lang.Long
floatjava.lang.Float
doublejava.lang.Double
charjava.lang.Character
booleanjava.lang.Boolean

Number classes

Byte, Short, Integer, Long, Float and Double classes extend from java.lang.Number. Number class is a subclass of Object.
Each of the number classes have the methods byteValue(), shortValue(), intValue(), longValue(), floatValue() and doubleValue() which returns the corresponding primitive data type.

Example


public class ByteExample {
	public static void main(String a[]){
		Byte b=new Byte((byte)4);
		System.out.print(b.byteValue());
	}
}

Output

4
Wrapper class Integer has the following methods:

  • int compare(int num1, int num2) returns the value of num1 minus num2
  • int max(int num1, int num2) returns the minimum of the two values num1 and num2
  • int min(int num1, int num2) returns the maximum of the two values num1 and num2
  • int parseInt(String s, int radix) returns the number extracted from String s. Radix is 10 for decimal values, 8 for octal values, 16 for hexadecimal values and 2 for binary values. Other numbers can be used as Radix to get numbers in other number systems
Similarly, other wrapper classes Byte, Short, Long, Float and Double have similar methods and return the int values
byteshortintlong
byte compare(byte num1, byte num2)short compare(short num1, short num2)int compare(int num1, int num2)long compare(long num1, long num2)
int max(int num1, int num2)long max(long num1, long num2)
int min(int num1, int num2)long min(long num1, long num2)
short parseByte(String s, int radix)short parseShort(String s, int radix)int parseInt(String s, int radix)long parseLong(String s, int radix)

<<Previous

Next >>





















Searching using Binary Search Tree