Monday, March 15, 2010

Casting for Primitives

Casting

int

float

float f = 6.1;

int i = (int)f;

double

double d = 25.0;

int i = (int)d;

long

long l = 25.0;

int i = (int)l;

String

String str = "25"; int i = Integer.valueOf(str).intValue();    or  int i = Integer.parseInt(str);



Casting

float

int

int i = 6;

float f= (float)i;

double

double d = 25.0;

float f = (float)d;

long

long l = 25.0;

float f = (float)l;

String

float f = Float.valueOf(str).floatValue();

Casting

double

int

int i = 6;

double d =(double)i;

float

float f = 6.1;

double d =(double)f;

long

long l = 25.0;

double d = (double)l;

String

double d = Double.valueOf(str).doubleValue();



Casting

long

int

int i = 6;

long l = (long)i;

float

float f = 6.1;

long l = (long)f;

double

double d =25.0;

long l= (long)d;

String

long l = Long.valueOf(str).longValue(); or   Long l = Long.parseLong(str);

Casting

String

int

int i = 42; String str = Integer.toString(i);  or  String str = "" + i;

float

String str = Float.toString(f);

double

String str = Double.toString(i);

long

String str = Long.toString(l);

Example Programs : Overloading and overriding

1. Example for Overloading :

class OverLoading
{
// Area for square
public double area(int a)
{
double sArea = a*a;
return sArea;
}
// Area for circle
public double area(double a)
{
double cArea = 3.14*a*a;
return cArea;
}
// Area for rectangle
public double area(double b, double h)
{
double rArea = b*h;
return rArea;
}
// Area for Eclipse
public double area(double b, int h)
{
double eArea = 0.5*b*h;
return eArea;
}
public static void main(String[] arg)
{
OverLoading oL = new OverLoading();
System.out.println(" Area for Square = "+oL.area(10));
System.out.println(" Area for Circle = "+oL.area(10.0));
System.out.println(" Area for Rectangle = "+oL.area(10.0,10.0));
System.out.println(" Area for Eclipse = "+oL.area(10.0,10));
}
}

2. Example for Overriding :

public class Overriding
{
public static void main(String arg[])
{
System.out.println(" ----------------------------------------");
System.out.println(" Animal a = new Animal(); ");
Animal a = new Animal();
System.out.println(" (non static mtd) a.eat() ");
a.eat();
System.out.println(" ( static mtd) a.showActualClassName() ");
a.showActualClassName();
System.out.println(" ----------------------------------------");
System.out.println(" ----------------------------------------");
System.out.println(" Tiger t = new Tiger(); ");
Tiger t = new Tiger();
System.out.println(" (non static mtd) t.eat() ");
t.eat();
System.out.println(" (static mtd) t.showActualClassName() ");
t.showActualClassName();
System.out.println(" ----------------------------------------");
System.out.println(" ----------------------------------------");
System.out.println(" Animal at = new Tiger(); ");
Animal at = new Tiger();
System.out.println(" (non static mtd) at.eat() ");
at.eat();
System.out.println(" ( static mtd) at.showActualClassName() ");
at.showActualClassName();
System.out.println(" ----------------------------------------");
}
}
class Animal
{
public Animal()
{
System.out.println("Animal constructor invoked");
}
public void eat()
{
System.out.println("Animal eat method called");
}
public static void showActualClassName()
{
System.out.println("This class name is : Animal");
}
}
class Tiger extends Animal
{
public Tiger()
{
System.out.println("Tiger constructor invoked");
}
public void eat()
{
System.out.println("Tiger eat method called");
}
public static void showActualClassName()
{
System.out.println("This class name is : Tiger");
}
}