Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Java Wrapper Class Example

 
 

Share on Google+Share on Google+

Java Wrapper Class Example

Advertisement
Java Wrapper class example explains you the use of various wrapper classes.

Java Wrapper Class Example

In this section we will read about how to use the wrapper classes in Java.

Wrapper classes wrap the primitive data types to objects of that class. In Java there are eight primitive data types and each data type has the corresponding wrapper class. These wrapper classes belongs to the java.lang package. In Java the wrapper classes are required to allow the null values because objects can be null whereas, the primitives can't. Wrapper classes are used in to include the values in Collection, it is required for type safety. It is required when treating the primitives values as an object with other objects. The table given below lists the primitive data types and its respective wrapper class :

Primitive Data Type Wrapper Class
boolean Boolean
byte Byte
char Character
int Integer
float Float
double Double
long Long
short Short

Following image demonstrates the hierarchy of the Wrapper class :

Almost all of each wrapper class has the parseXXX(), toString(), xxxxValue() etc. Below we are discussing some of the methods of java.lang.Integer class but, these methods are usually applies on almost all wrapper classes.

Method Name Method Description
parseInt(String s) This method is used to parse the String representation of number to signed decimal integer.
Syntax : public static int parseInt(String s) throws NumberFormatException
toString() This method is used to represent an Integer value to a String object.
Syntax : public String toString()
valueOf(String s) This method is used to represent the value of String as an Integer object.
Syntax : public static Integer valueOf(String s) throws NumberFormatException
equals(Object obj) This method is used to compare the object with the specified object whether they are equals or not.
Syntax : public boolean equals(Object obj)
hashCode() This method is used to find the hash code of the Integer.
Syntax : public int hashCode()
compareTo(Integer anotherInteger) This method is used to compare two Integers. It returns 0 (zero) if the values are equal, returns negative value if the specified object value is greater than the invoking object vlaue, returns positive value if the specified object value is lower than the invoking object value.
Syntax : public int compareTo(Integer anotherInteger)
compareTo(Object o) This method is used to compare the Integer object to another object.
Syntax : public int compareTo(Object o)

Example

Here I am giving a simple example which will demonstrate you about how to use the wrapper classes in Java. In this example we will create a Java class where we will use the various methods of java.lang.Integer class such as compareTo(), equal(), toString() etc.

WrapperClassExample.java

package net.roseindia.wrapperclassexample;

public class WrapperClassExample {
		
	public static void main(String args[])
	{
		String num = "20";
		int i = Integer.parseInt(num);
		Integer it1 = new Integer(num);
		Integer it2 = new Integer(20);
		
		String str = it2.toString();
		Integer intstr = Integer.valueOf(str);
		boolean bol = it1.equals(i);
		
		int c = it2.compareTo(intstr);
		if(c == 0)
		{
			System.out.println("it2 and intstr are equals");
		}
		System.out.println("String representation of it2 = "+ str);
		System.out.println("Integer object = "+ it1);
		System.out.println("Integer representation of num = "+i);
		System.out.println("it1 and i are equal : "+bol);
	}

}

Output

When you will compile and execute the above example you will get the output as follows :

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 26, 2013

Related Tutorials

Discuss: Java Wrapper Class Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com