Skip to main content

Wrapper Classes

Wrapper Classes:-
In a java wrapper classes are used to convert primitive data type to object type and object to primitive data type. Sometimes we have object but that time we need variable and sometime data (variable) is available but that time we need object to execute method of the particular class.
For example:-we have to create object of Integer class.
Integer ob=new Integer(100);   or
Integer ob=new Integer(“100”);
Every wrapper class contains two constructor one common String argument constructor except Character class, it contains only one constructor.



type
Wrapper classes
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean

Wrapper classes
             Constructor
Byte
new Byte(byte)      ,    new Byte(String)
Short
new Short (short)   ,    new Short(String)
Integer
new Integer(int)      ,    new Integer(String)
Long
new Long(long)      ,    new Long(String)
Float
new Float(float)      ,    new Float(String)
Double
new Double (double)      ,    new Double(String)
Character
new Character (char)
Boolean
new Boolean(boolean)     ,     new Boolean(String)
      
Example1:- TestWrapper.java
class TestWrapper
{
public static void main(String[]args)
{
Integer ob=new Integer(100);
int i=ob.intValue();
System.out.println(i);
}
}
Output:-100
Example2:- TestWrapper.java
class TestWrapper
{
public static void main(String[]args)
{
Integer ob=new Integer(100);
int i=i.intValue();
System.out.println(i);
}
}
Output:- C:\JAVATECH>javac TestWrapper.java
TestWrapper.java:6: error: int cannot be dereferenced
int i=i.intValue();
       ^1 error
Example3:- TestWrapper.java
class TestWrapper
{
public static void main(String[]args)
{
Integer ob=new Integer("100");
System.out.println("Object "+ob);
int i=ob.intValue();
System.out.println("int "+i);
}
}
Output:-
C:\JAVATECH>javac TestWrapper.java
C:\JAVATECH>java TestWrapper
Object 100
int 100
Example4:- TestWrapper.java
class TestWrapper
{
public static void main(String[]args)
{
Integer ob=new Integer("100");
int i=ob.intValue();
System.out.println(i+100);
System.out.println("integer object to String object conversion with proof");
String s=ob.toString();
System.out.println(s+100);
}}
Output:- C:\JAVATECH>javac TestWrapper.java
C:\JAVATECH>java TestWrapper
200
integer object to String object conversion with proof
100100
Example5:- TestWrapper.java
class TestWrapper
{
public static void main(String[]args)
{
Integer ob=new Integer("100");
int i=ob.intValue();
System.out.println(i+100);
System.out.println("integer object to String object conversion with proof");
String s=ob.toString();
System.out.println(s+100);
System.out.println("String object to int conversion with proof");
Integer ob1=new Integer(s);
int p=ob1.intValue();
System.out.println(p+100);
}
}
Output:- C:\JAVATECH>javac TestWrapper.java
C:\JAVATECH>java TestWrapper
200
integer object to String object conversion with proof
100100
String object to int conversion with proof
200
Autoboxing & Autounboxing:-java support autoboxing that means normal variable automatically convert into related object and object convert into related variable called autounboxing. .
Example1:-Test.java
class Test
{
public static void main(String[]args)
{
int i=100;
Integer ob=i;
System.out.println(ob);
int p=ob;
System.out.println(p);
}
}
Output:-
C:\JAVATECH>javac Test.java
C:\JAVATECH>java Test
100

100

Comments

Popular posts from this blog

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Small concept about OOPs

Some small concept about OOPs:- In many books and website write about OOPs concepts as hundred percent object oriented, pure object oriented and partial object oriented language. Hundred percent object oriented:- In the hundred percent object oriented movie hero is object, without object program is incomplete that means can not execute program without object. Without class and object programming or application development is not possible. Example of hundred percent object oriented language is  Smalltalk . Pure object oriented:- In the object oriented cinema obviously object is the hero. In pure object oriented language object having same importance but in this having some relax that is without creating object we can successfully execute our application. So in the object oriented programming without class we cannot create or run our application, class is must. Example of pure object oriented language is  Java . Partial object oriented:- In this type of la...