• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Wrapper classes

October 5, 2011 By j2eereference Leave a Comment

What is a wrapper class?

Every primitive data type in java has a dedicated class known as a wrapper class. The class is called as wrapper class because they wrap the primitive data type into an object of that class.Wrapper classes are part of java.lang package.All the  methods  of a wrapper class are static so they can be used without creating an instance of the wrapper class.

Converting wrapper class object to its corresponding primitive type and vice versa

1.  Float wrapper class

Example: To convert a Float object to float primitive type.

 public class  Convert

{

public static void main(String [] args)

{

Float fObj  = new Float(“5.5”);

float f = fObj.floatValue();

         }

}

Example: To convert a float primitive type to a Float object .

 public class Convert

{

public static void main(String [] args)

{

float f  = 5.5;

Float fObj = new Float(f);

       }

}

 2. Integer wrapper class

Example: To convert an Integer object to integer primitive type.

public class Convert

{

public static void main(String [] args)

{

Integer intObj  = new Integer(“5”);

int i = intObj.intValue();

}

}

Example: To convert an integer primitive type to a Integer object .

 public class Convert

{

public static void main(String [] args)

{

int i  = 5;

Integer intObj = new Integer(i);

}

}

3. Double wrapper class

Example: To convert a Double object to double primitive type.

public class Convert{

public static void main(String [] args)

{

Double dObj  = new Integer(“10.5”);

double d = dObj.doubleValue();

         }

}

Example: To convert a double primitive type to a Double object .

public class Convert

{

public static void main(String [] args)
{
double d  = 10. 5;

Double dObj = new Double(d);

}

}

4. Boolean wrapper class

Example: To convert a Boolean object to boolean primitive type.

public class Convert

{

public static void main(String [] args)

{

Boolean bObj  = new Boolean(“true”);   // Construct new Boolean object

Boolean b = bObj .booleanValue();   // Use booleanValue of Boolean class to convert into boolean type

}

}

Example:To convert a boolean primitive type to a Boolean object .

public class Convert

{

public static void main(String [] args)

{

boolean b  = true;

Boolean b1Obj = new Boolean(b);          // Using constructor

Boolean b2Obj =Boolean.valueOf(b);     // Using valueOf method which is a static method

}

}

5. Byte wrapper class

Example: To convert a Byte object to byte primitive type.

public class Convert

{

public static void main(String [] args)

{

Byte byObj  = new Byte(“5”);

byte by = byObj.byteValue(); // uses byteValue method of Byte class to convert to primitive type

}

}

Example:To convert an byte primitive type to a Byte object .

public class Convert{

public static void main(String [] args)
{

byte by  = 5;

Byte byObj = new Byte(by); // Uses constructor of Byte class to convert primitive type to object

}

}

6. Short wrapper class

Example: To convert a Short object to short primitive type.

public class Convert

{

public static void main(String [] args)

{

Short shObj  = new Short (“5”);

short s = shObj.shortValue(); // uses shortValue method of Short class to convert to primitive type

}

}

Example: To convert a short primitive type to a Short object.

public class Convert{

public static void main(String [] args)

{

short s  = 5;

Short shObj = new Short(s); // Uses constructor of Short class to convert primitive type to object

}

}

7. Long wrapper class

Example: To convert a Long object to long primitive type.

public class Convert{

public static void main(String [] args) {

Long lObj  = new Long (“5”);

long l = lObj.longValue(); // uses longValue method of Long class to convert to primitive type

}

}

Example: To convert long primitive type to a Long object.

public class Convert

{

public static void main(String [] args)

{

long l  = 5;

Long lObj = new Long (l); // Uses constructor of Long class to convert primitive type to object

         }

}

8. Character wrapper class

Example:To convert a Character object to char primitive type.

public class Convert

{

public static void main(String [] args)

{

Character chObj  = new Character (“abc”);

char s = chObj.charValue(); // uses charValue method of Character class to convert to primitive type

}

}

Example:To convert char data type to a Character object.

public class Convert{

public static void main(String [] args)

{

char ch  = abc;

Character chObj = new Character (ch); // Uses constructor of Character class to convert primitive type to object

        }

}

Related Posts

  • Busy Spinning in mutithreading
  • Implementing queue in java
  • TreeSet vs ConcurrentSkipListSet
  • How to create immutable class?
  • Implementing Stack in java
  • What is ReentrantLock?
  • What is Semaphore in java
  • Why AtomicInteger class
  • What is CyclicBarrier?
  • CountDownLatch in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.