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

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...

Regarding class files

Class files:- Regarding class file or dot class file also known as byte code of class, in a java programming language we can use any number of classes in our program. After compilation all classes convert into individual   .class   file. Example:- class A { } class B { } class C { } class Student { public static void main(String[]args) { System.out.println("Well-come to my java blog"); } } We can save above program to any   classname.java   or we can use any other   name.java,   there is no compulsion for java file name. But recommended to save by that class name which having main method. I saved the above file to   Student.java   name .   Before compilation my   C:\JAVATECH   folder contain only one java file. Student.java For  compilation. C:\JAVATECH>javac  Student.java After compilation   my   C:\JAVATECH   folder contains five files, on...

inheritance

INHERITANCE:- Inheritance is the process of code re-usability, using this property software or applications can develop in less time or can develop rapidly. Inheritance increases the readability of code and decrease redundancy or repetition of code or reduces size of code. Use of previously developed code in future any number of times is called inheritance. Through inheritance variables and methods of class can inherited. Types of Inheritance:- 1)         Single or Single level inheritance 2)         Multilevel inheritance 3)         Hierarchical inheritance Java does not support to multiple inheritance. Single or single level inheritance:- In a single inheritance a class is inherited by a single class. In a single or single level inheritance a sub class having only one super class directly or indirectly. Syntax: -        class Subclass ex...