Variable Scope:
public class VarScopeProg
{
public static void main(String[] args)
{
int A=10;
{
int B=20;
System.out.println("Block A: "+A);
System.out.println("Block B: "+B);
}//scope of B=20 ends here
{
int B=30;//scope of B=30 starts here
System.out.println("Block A: "+A);
System.out.println("Block B: "+B);
}//scope of B=30 ends here
int B=40;//scope of B=40 starts here
{
System.out.println("Block A: "+A);
System.out.println("Block B: "+B);
}
}//scope of all the variables end here
}
Use Of Class:
/*This is a Java program which shows theuse of class in the program
*/
class Rectangle
{
int height;
int width;
void area()
{
int result=height*width;
System.out.println(“The area is ”+result);
}
}
class classDemo
{
public static void main(String args[])
{
Rectangle obj=new Rectangle();
obj.height=10;//setting the attribute values
obj.width=20;//from outside of class
obj.area();//using object method of class is called
}
}
Type Casting
public class TypeCastProg
{
public static void main(String[] args)
{
double x;
int y;
x=25.50;
System.out.println("Value of [double] x: "+x);
System.out.println("Conversion of double to integer");
y=(int)x;
System.out.println("Value of [integer] y: "+y);
int m;
double n;
m=10;
n=(double)m;
System.out.println("Value of [integer] m: "+m);
System.out.println("Conversion of integer to double");
System.out.println("Value of [double] n: "+n);
}
}
Exception Handling
class ExceptionDemo
{
public static void main(String args[])
{
try
{
int a,b;
a=5;
b=a/0;
}
catch(ArithmeticException e)
{
System.out.println("Divide by Zero\n");
}
System.out.println("...Executed catch statement");
}
}
No comments:
Post a Comment