Java is a pure object-oriented programming language developed by the chief programmer of Sun Microsystems James Gosling and his team-mates for controlling consumer electronic devices. It shares many superficial similarities with C, C++, and Objective C (for instance for loops have the same syntax in all four languages); but it is not based on any of those languages, nor have efforts been made to make it compatible with them. Java has following important features:
1. Simple – Java is simple to learn. If you already have a bit knowledge of object oriented programming like C++ then it will be some easier to learn Java because Java inherits the
C/C++ syntax and many of the object-oriented features of C++.
2. Object-Oriented – Java is a pure object-oriented programming language. Java believes – everything is an object. It provides the features of abstraction, encapsulation, inheritance and
polymorphism. Any thing in Java is embedded into a class.
3. Compiled and Interpreted – Java source programs are translated into bytecode by java compiler and this bytecode is interpreted by the Java interpreter, thus Java is both compiled
and interpreted. When a Java source program is compiled, it lists all the errors occurred in a program, if any. And when this program is error free, it is interpreted by the interpreter
which executes it line by line. Only when the execution reaches with an error is the error reported. Of course it takes some extra time for debugging a program.
4. Portable – The bytecode, produced by a Java compiler, can be run on any platform that has Java interpreter. Thus java code is portable.
5. Robust – Java is a strictly typed language. It checks your code at compile time as well as run-time. Thus java programs are less error prone.
6. Multithreaded – Java supports multithreading programming environment, which allows you to write programs that do many things simultaneously. Even the core of Java is
multithreaded.
7. Secure – Java does not allow a programmer to manipulate the memory of a system.
8. Distributed – Java programs can access data across a network, therefore it is distributed.
9. Architecture Neutral and Portable – Java first converts a source program into bytecode which can be run on a variety of computers having different operating systems.
10. A High Performance Programming Language – Java programs are faster as compared to programs written in other interpreter-based programming languages.
11. Dynamic – Java provides an efficient way for maintaining different versions of an application.
/* IMPLEMENT SIMPLE JAVA PROGRAM */
class lohit
{
public static void main(String arr[])
{
System.out.println("Welcome To My Java World:Lohit");
}
}
OUTPUT :
/* IMPLEMENT PROGRAM OF JAVA USING CONSTRUCTOR */
class nesting
{
int m,n;
nesting(int x,int y)
{
n=x;
n=y;
}
int largest()
{
if(m>=n)
return(m);
else
return(n);
}
void display()
{
int large=largest();
System.out.println("largest value="+large);
} }
class nestingtest
{
public static void main(String args[])
{
nesting nest=new nesting(50,40);
nest.display();
}
}
OUTPUT:
/* IMPLEMENT PROGRAM OF JAVA USING INHERITANCE */
class room
{
int length,breadth;
room(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
} }
class broom extends room
{
int height;
broom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
} }
class test
{
public static void main(String args[])
{
broom room1=new broom(15,10,20);
int area1=room1.area();
int volume1=room1.volume();
System.out.println("area1="+area1);
System.out.println("volume1="+volume1);
}
}
OUTPUT:
/* IMPLEMENT JAVA PROGRAM USING MULTITHREADING */
class A extends Thread
{
public void run()
{
for (int i=0; i<=2;i++)
{
System.out.println("\t From thread a"+i);
}}}
class B extends Thread
{
public void run(){
for (int j=0;j<=2;j++)
{
System.out.println("\t From thread b"+j);
}}}
class Thread1
{
public static void main(String args[])
{
new A().start();
new B().start();
}}
OUTPUT:
/* IMPLEMENT JAVA PROGRAM USING EXCEPTION HANDLING */
class Error4
{
public static void main(String args[])
{
int a[]={5,10};
int b=5;
try
{
int x=a[2]/b;
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(Exception e)
{
System.out.println("ArrayIndexOutofBound");
}
finally
{
System.out.println("Finally executed");
}}}
OUTPUT:
/* IMPLEMENT JAVA PROGRAM USING INTERFACES */
interface area
{
final static float pi=3.14f;
float compute(float x, float y);
}
class rectangle implements area
{
public float compute(float x, float y)
{
return(x*y);
}
}
class circle implements area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
class interfacetest
{
public static void main(String args[])
{
rectangle rect=new rectangle();
circle cir=new circle();
area are;
are=rect;
System.out.println("Area of rect="+are.compute(10,20));
are=cir;
System.out.println("Area of cir="+are.compute(10,0));
}
}
OUTPUT:
/* IMPLEMENT JAVA PROGRAM USING APPLETS */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**/
public class tools extends Applet implements ActionListener
{
String msg=" ";
Button b1,b2,b3;
TextField t1,t2,t3;
Label l1,l2,l3;
public void init()
{
b1=new Button("sum");
b2=new Button("multiply");
b3=new Button("divison");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
l1=new Label("enter ist no");
l2=new Label("enter 2nd no");
l3=new Label("result");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String msg =ae.getActionCommand();
int x,y,z;
x=Integer.parseInt(t1.getText());
y=Integer.parseInt(t2.getText());
if(msg.equals("sum"))
{
z=x+y;
}
else if (msg.equals("multiply"))
{
z=x*y;
}
else
{
z=x/y;
}
t3.setText(" "+z);
repaint();
}
public void paint (Graphics g)
{
g.drawString(msg,20,20);
}
}
OUTPUT: