JAVA PROGRAM EXAMPLES
JAVA PROGRAM EXAMPLES
class preventerror
{
public static void main(String args[])
{
Integer iOb = 1000; // autobox the value 1000
float i = iOb.floatValue(); // manually unbox as byte !!!
System.out.println(i); // does not display 1000 !
float i2=iOb;
System.out.println(i2); // does not display 1000 !
double id=iOb;
System.out.println(id); // does not display 1000 !
byte b = Byte.parseByte("25");
System.out.println(b);//Output : 25-byte
short s = Short.parseShort("45");
System.out.println(s);//Output : 45-short
int ii = Integer.parseInt("123");
System.out.println(ii);//Output : 123int x=iOb;
}
}
******PROGRAM ON BOXING/ UNBOXING *****
public class wrapperdemo
{
public static void main (String args[])
{
byte grade = 2;
int marks = 50;
float price = 8.6f;
double rate = 50.5;
char x='A';
boolean b=false;
String s="12345";
if(s == iv1)
System.out.println("both the strings are equal");
if(iv1 == m2)
System.out.println("both the strings are equal");
}
******PROGRAMS ENUMERATION CONCEPTS *****
enum enum1
{
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class EnumDemo
{
public static void main(String args[])
{
enum1 ap;
ap = enum1.RedDel;
//Overloaded constructor
Apple()
{
price = -1;
}
int getPrice()
{
return price;
}
}
class enumdemo3
{
public static void main(String args[])
{
Apple ap;
//Display price of Winesap.
System.out.println("Winesap costs " +Apple.Winesap.getPrice() +" cents.\n");
class enummethods
{
public static void main(String args[])
{
Appletypes ap, ap2, ap3;
if(ap.compareTo(ap2) > 0)
System.out.println(ap2 + " comes before " + ap);
if(ap.compareTo(ap3) == 0)
System.out.println(ap + " equals " + ap3);
if(ap.equals(ap2))
System.out.println("Error!");
if(ap.equals(ap3))
System.out.println(ap + " equals " + ap3);
if(ap == ap3)
System.out.println("ap not equal to ap3");
}
}
}
}