This document discusses Java wrapper classes. Wrapper classes represent primitives as objects and provide utility methods for primitives. The document covers how to create wrapper class objects using constructors and static methods, how to get primitive values from wrapper objects, and how to get a string from a wrapper object.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
88 views18 pages
Java Wrapper Classes Guide
This document discusses Java wrapper classes. Wrapper classes represent primitives as objects and provide utility methods for primitives. The document covers how to create wrapper class objects using constructors and static methods, how to get primitive values from wrapper objects, and how to get a string from a wrapper object.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18
{
Wrapper Classes In Java
< R.K.SUSHMITA LAKSHME >
... } Contents Introduction Summarized table How to create wrapper class object
How to get primitive values from wrapper
objects
How to get string object from wrapper class
Points to remember
Example program 01 { .. Introduction < What is Wrapper Class and its purpose >
} .. What is Wrapper classes? ➔ Representing Primitives as objects ➔ Provide utility methods for primitives. ➔ Each primitive type in Java has a ➔ Primitives are not objects -> so no method can be corresponding wrapper class. invoked ➔ Defined in java.lang package. ➔ Wrapper class defines lot of utility methods that ➔ Wrap(encapsulate) single immutable does common conversion jobs primitive value.
} .. Summarized Table
* How to create Wrapper Class Object
{ Using Class Using valueOf()
} constructor method 1. Using wrapper class constructor
● Find out corresponding wrapper class for this primitive.
... EX: int primitive ->> Integer class ● Create an instance of wrapper class by passing the int value as:
Integer intobj = new Integer(100);
● Intobj will always hold the value of 100.
Cont… 2. Creating wrapper objects using valueOf() method
● Import a static method valueOf() -> takes a string as argument
valueOf(String s)
● If we want to get wrapper obj for int value 100:
->Find out corresponding wrapper class(Integer here.) ->Invoke Integer.valueOf() by passing string containing int value as:
Integer intObj = Integer.valueOf(“100”);
NOTE: this method throws a runtime exception (NumberFormatException) when string passes inappropriate arg. Ex: Integer.valueOf(“hundred”) //error Cont… { How to get primitive values from the wrapper objects? ..
typeValue()
Primitive
} .. How to get String object from wrapper objects?
● All wrapper class have a instance method toString().
● It returns a string representing primitive value wrapped by wrapper. ● Wrapper class inherit this method from object class.
SYNTAX: public String toString()
} .. { ..
Points to Remember } .. Points to remember ★ Java.lang package provides grp of classes to represent primitives as objects-> wrapper classes. ★ Cannot be sub-classed ★ 2 ways of constructing wrapper class Pass string argument Pass primitive as argument. ★ Wrapper objects are immutable. { .. Example Program… } .. { ..
} .. Thanks! CREDITS: This presentation template was created by Slidesgo, and includes icons by Flaticon, and infographics & images by Freepik