Wrapper Classes|Java.Lang Package - Java
Wrapper classes provides the mechanism to convert primitive into object and object into primitive.
Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes, because they "wrap" the primitive data type into an object of that class. So, there is an Integer class that holds an int variable, there is a Double class that holds a double variable, and so on. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs
List of Wrapper Classes
Primitive data type |
Wrapper class |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Ex:
int x = 25;
Integer y = new Integer(33);
Integer Wrapper Class
Constructors:
- Integer(i) constructs an Integer object equivalent to the integer i
- Integer(s) constructs an Integer object equivalent to the string s
Static Methgods
- parseInt(s) returns a signed decimal integer value equivalent to string s
- toString(i) returns a new String object representing the integer i
Methods
- byteValue() returns the value of this Integer as a byte
- doubleValue() returns the value of this Integer as an double
- floatValue() returns the value of this Integer as a float
- intValue() returns the value of this Integer as an int
- longValue() returns the value of this Integer as a long
- shortValue() returns the value of this Integer as a short
- toString() returns a String object representing the value of this Integer
Programe on boxing and un boxing using wrapper classes
class vision
{
public static void main(String[] args)
{
Integer x=10; // auto boxing
int a=5;
x=x+a; //auto un boxing
System.out.println(x);
int c=x.intValue()+a;
// convert to integer object to int value sames as above auto unboxing
System.out.println(c);
}
}
WRITE A PROGRAM ON ISEQUAL.
import java.lang.*;
class Sample
{
public static void main(String args[])
{
Double d1=new Double(3.1459);
Double d2=new Double(3.145);
System.out.println(d1+"="+d2+"->"+d1.equals(d2));
Double d3=new Double(1/0.);
Double d4=new Double(0/0.);
System.out.println(d3+":"+d3.isInfinite()+","+d3.isNaN());
System.out.println(d4+":"+d4.isInfinite()+","+d4.isNaN());
}
}
WRITE A PROGRAM TO FIND THE SUM OF GIVEN NUMBERS USING PARSEINT.
import java.io.*;
class Sample
{
public static void main(String args[]) throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String s;
int i;
int sum=0;
System.out.println("Enter 0 to Exit");
do
{
s=b.readLine();
try
{
i=Integer.parseInt(s);
}
catch(NumberFormatException e)
{
System.out.println("invalid format");
i=2;
}
sum+=i;
System.out.println("Sum: "+sum);
}
while(i!=0);
}
}
WRITE A PROGRAM TO CONVERT THE INTEGER TO BINARY AND HEXA DECIMAL.
import java.io.*;
class Sample
{
public static void main(String args[])
{
int n=2563;
System.out.println(n+"in Binary: "+Integer.toBinaryString(n))
System.out.println(n+"in Octal: "+Integer.toOctalString(n));
System.out.println(n+"in Hexa: "+Integer.toHexString(n));
}
}
WRITE A PROGRAM ON IS METHODS.
class Sample
{
public static void main(String args[])
{
char a[]={'a','b','5','?','A',' '};
for(int i=0;i<a.length;i++)
{
if(Character.isDigit(a[i]))
System.out.println(a[i]+" Is Digit");
if(Character.isLetter(a[i]))
System.out.println(a[i]+" Is Letter");
if(Character.isWhitespace(a[i]))
System.out.println(a[i]+" Is whitesapce");
if(Character.isUpperCase(a[i]))
System.out.println(a[i]+" Is Uppercase");
}
}
}
WRITE A PROGRAM ON MEMORY MANAGEMENT.
class Sample
{
public static void main(String args[])
{
Runtime r=Runtime.getRuntime();
long m1,m2;
Integer Some[]=new Integer[1000];
System.out.println("TotalMemoey: "+r.totalMemory());
m1=r.freeMemory();
System.out.println("FreeMemory: "+m1);
r.gc();
m1=r.freeMemory();
System.out.println("After Garbage: "+m1);
for(int i=0;i<1000;i++)
Some[i]=new Integer(i);
m2=r.freeMemory();
System.out.println("FreeMemory: "+m2);
}
}
WRITE A PROGRAM RUN WINDOW BASED APPLICATIONS.
class Sample
{
public static void main(String args[])
{
Runtime r=Runtime.getRuntime();
{
Process p=null;
try
{
p=r.exec("calc");
}
catch(Exception e)
{
System.out.println("Error executing caluclater");
}
}
}
}
WRITE A PROGRAM ON TIME.
class Sample
{
public static void main(String args[])
{
long s,e;
s=System.currentTimeMillis();
for(int i=0;i<10000;i++)
{
e=System.currentTimeMillis();
}
System.out.println("C.Time: "+(e-s));
}
}
WRITE A PROGRAM ON CLONE.
class Abc implements Cloneable
{
int a;
double b;
Abc Abc()
{
try
{
return(Abc)super.clone();
}
catch(Exception e)
{
System.out.println(e);
return(this);
}
}
}
class Sample
{
public static void main(String args[])
{
Abc x1=new Abc();
Abc x2;
x1.a=10;
x1.b=20.56;
x2=x1.Abc();
System.out.println("x1 "+x1.a+" "+x1.b);
System.out.println("x2 "+x2.a+" "+x2.b);
}
}
WRITE A PROGRAM ON RUNTIME TIME INFORMATION.
class X
{
int a;
float b;
}
class Y extends X
{
double c;
}
class Sample
{
public static void main(String args[])
{
X x1=new X();
Y y1=new Y();
Class obj;
obj=x1.getClass();
System.out.println("x is object of type: "+obj.getName());
obj=y1.getClass();
System.out.println("y is object of type: "+obj.getName());
obj=obj.getSuperclass();
System.out.println("y super is: "+obj.getName());
}
}
WRITE A PROGRAM ON TO RADIANS AND TO DEGREES.
class Sample
{
public static void main(String args[])
{
double d=120.0;
System.out.println(d+" degrees "+Math.toRadians(d));
d=1.312;
System.out.println(d+" radians "+Math.toDegrees(d));
System.out.println(d+" sin "+Math.sin(d));
System.out.println(d+" cos "+Math.cos(d));
}
}
WRITE A PROGRAM ON CLASS PACKAGE.
class Sample
{
public static void main(String args[])
{
Package p[];
p=Package.getPackages();
for(int i=0;i<p.length;i++)
{
System.out.println("name: "+p[i].getName());
System.out.println("implementation title: "+p[i].getImplementationTitle());
System.out.println("implementation vendor: "+p[i].getImplementationVendor());
System.out.println("implementation version: "+p[i].getImplementationVersion());
}
}
}
For
More Explanation
&
Online Classes
More Explanation
&
Online Classes
Contact Us:
+919885348743
+919885348743