Wrapper class autoboxing examples
Autoboxing example
Autoboxing another example
Wrapper class methods example
Implict and Explict Casting example
Explict Casting example
Autoboxing example
class AutoboxingDemo
{
public static void main(String s1[])
{
Integer a=new Integer(10);
int b=a; //autoboxing
System.out.println(b);
}
}
Output
10
Autoboxing another example
Autoboxing another example
class AutoboxingDemo
{
public static void main(String s1[])
{
float a=100.10f;
System.out.println(a);
Float r=new Float (a);
float c=r.floatValue();
System.out.println(c);
}
}
Output
100.1 100.1
Wrapper class methods example
Wrapper class methods example
class WrapperDemo
{
public static void main(String s1[])
{
Integer i=new Integer(42);
//first method
int b=i.intValue(); //showing intValue() method
byte c=i.byteValue(); //showing byteValue() method
float d=i.floatValue(); //showing floatValue() method
System.out.println(b);
System.out.println(c);
System.out.println(d);
//second method
int x=Integer.parseInt("100");
double y=Double.parseDouble("90.10");
System.out.println(x);
System.out.println(y);
//third method
String s=Integer.toHexString(254);
String s2=Long.toOctalString(254);
String s3=Integer.toBinaryString(254);
}
}
Output
42 42 42.0 100 90.1
Implict and Explict Casting example
Implict and Explict Casting example
class CastingDemo
{
public static void main(String s1[])
{
byte a= 10;
int b=a; //implicit casting
float x=b; //implicit casting
System.out.println(b);
System.out.println(x);
int c=100;
//explicit cast (convert bigger type into smaller type),
//and programmer has to do explicitly
byte d=(byte)c;
System.out.println(d);
}
}
Output
10 10.0 100
Explict Casting example
Explict Casting example
class CastingDemo
{
public static void main(String s1[])
{
byte a=10;
byte b=20;
//explicit casting
byte c=(byte)(a+b);
System.out.println(c);
}
}
Output
30


