You are on page 1of 22

Wrapper classes – 1

Wrapper classes
• A wrapper class is a class whose object
wraps or contains a primitive data type
value.

• It is used to convert to primitive data type


value to object.
• Boxing: converting a primitive data type
into an object.
• Unboxing: object to primitive value.
Wrapper classes
Primitive data type Wrapper class
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
Number class

• It is super class of Byte, Short,


Integer, Long, Float, and Double.

• It is abstract class.
Number class methods
• byte byteValue();
• short shortValue();
• abstract int intValue();
• abstract long longValue();
• abstract float floatValue();
• double doubleValue();
Character class
which wrapsor contain a char value in
an object.

Constructor:

Character(char value)
Constructs a newly allocated Character
object that represents the specified char value.
Note: it has only one constructor (does not contain
with String argument).
Contd..
Methods:
• Char charValue();
Character c=new Character(‘A’);
char ch=c.charValue();// ch contains ‘A’
• public int compareTo(Character obj2);
int x=obj1.compareTo(obj2);
Returns 0 if both are equal.
negative obj1<obj2
positive obj1>ob2.
Contd…
String toString();
static boolean isWhitespace(char ch);
static boolean isWhitespace(int code);
static boolean isWhitespace(char ch);
static boolean isUpperCase(int unicode) ;
static boolean isUpperCase(char ch) ;
static boolean isLowerCase(int codePoint);
static boolean isLowerCase(char ch) ;
static boolean isUpperCase(char ch) ;
static boolean isUpperCase(int codePoint);
static boolean isLetterOrDigit(char ch) ;
static boolean isLetterOrDigit(int codePoint) ;
static String toString(char c) ;
static char toUpperCase(char ch) ;
static char toLowerCase(char ch) ;
Byte class
• Which wraps or contain byte value in the
object.

Constructors:
• Byte( byte num); // Byte b=new Byte(123);
• Byte(String obj);// Byte b1=new Byte(“12”);
Contd….
String toString();
int compareTo(Byte anotherByte);
static byte parseByte(String s) ;
static byte parseByte(String s, int radix) ;
static Byte valueOf(String s) ;
static Byte valueOf(byte b) ;
short shortValue() ;
byte byteValue();
boolean equals(Byte obj);
Short
• Which wraps a value of primitive data type short
in its object.

Constructors:
• Short( short num); // Short b=new Short(14007);
• Short(String obj);// Short b1=new Short(“14007”);
Contd….
String toString();
int compareTo(Short anotherByte);
static short parseShort(String s) ;
static short parseShort (String s, int radix) ;
static Short valueOf(String s) ;
static Short valueOf(short b) ;
short shortValue() ;
byte byteValue();
boolean equals(Short obj);
Integer
• Which wraps a value of primitive data type int in
its object.

Constructors:
• Integer( int num); // Integer b=new
Integer(14007);
• Integer(String obj);// Integer b1=new
Integer(“14007”);
Contd…
String toString();
int compareTo(Short anotherByte);
static int parseInt(String s) ;
static int parseInt (String s, int radix) ;
static Integer valueOf(String s) ;
static Integer valueOf(byte b) ;
short shortValue() ;
byte byteValue();
int intValue();
boolean equals(Short obj);
static String toHexString(int i);
static String toOctString(int i);
static String toBinaryString(int i);
Long

• Which wraps a value of primitive data type long


in its object.

Constructors:
• Long( long num); // Long b=new Long(14007000);
• Long(String obj);// Long b1=new Long(“14007000”);
Contd…
String toString();
int compareTo(Short anotherByte);
static long parseLong(String s) ;
static long parseLong (String s, int radix) ;
static Long valueOf(String s) ;
static Long valueOf(long b) ;
short shortValue() ;
byte byteValue();
int intValue();
boolean equals(Short obj);
static String toHexString(long i);
static String toOctString(long i);
static String toBinaryString(long i);
Command line arguments
Command line arguments
• We can pass the arguments to main
method during the execution of the
program. These are called command line
arguments.

• The values passed are collected in the


variable 'args' which is infact an array of
'String' objects.
public static void main(String args[])
program
import java.lang.*;
output:
class App2
{
public static void main(String yck[]) Z:\>javac
App.java
{
Z:\>java App j1
for(int i=0;i<yck.length;i++) java2
System.out.println(yck[i]);
j1
}
} java2
Program 2
import java.lang.*;
class App Output:
{
public static void main(String yck[]) Java App2 10 20 30
{ Sum 60
int sum=0;
for(int i=0;i<yck.length;i++)
Sum+=Integer.parseInt(yck[i]);
System.out.println(“sum”+sum);
}
}
Any Queries…..?
Thank You…!

You might also like