HISTORY OF JAVA
Java was conceived by james gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike
Sheridan at Sun Microsystems, Inc. in 1991.It took 18 months to develop the first
working version. This language was initially called “OAK,” but was renamed “java” in
1995.The original impetus for java was not the Internet! Instead, the primary
motivation was the need for a platform independent language that could be used to create
software to be embedded in various consumer electronic devices.thi s effort ultimately led
to creation of java. The emergence of the World Wide Web, java was propelled to the
forefront of the computer language design, because the Web too demanded portable
programs.
INTRODUCTION TO JAVA
To fully understand java, one must understand the reasons behind its creation, the forces
that shaped it, and legacy that it inherits. Like the successful computer language that came
before, java is blend of the best elements of its rich heritage combined with the innovative
concepts required by its unique mission. Much of the character of the java is inherited
from C and C++.From C, java derives its syntax. Many of java’s OOPs features were
influenced by C++. Creation of java was deeply rooted in the process of refinement and
adaptation that has been occurring in computer programming language for the past
several decades.
FEATURES OF JAVA
JAVA APPLETS
An applet is a special kind of java program that is designed to be transmitted over the
Internet and automatically executed by a java-compatible web browser. Furthermore, an
applet is downloaded on the demand, without further interaction with user. If the user
click a link that contain the applet, the applet will automatically downloaded and run in
the browser. The creation of the applet changed Internet programming because it
expanded the universe of objects that can move about freely in the cyberspace.
Java’s Magic: The Bytecode
The output of the java compiler is not the executable code Rather, it is bytecode.
Bytecode is a highly optimized set of instructions designed to be executed by the java run
time system, which is called the Java Virtual Machine(JVM).Translating a java program
into the bytecode makes it much easier to run a program in a wide variety of
environments because only the JVM needs to be implemented for each platform.
Although the details of the JVM will differ from platform to platform, all understand the
same bytecode.
Servlets: java on the server side
A servlet is a small program that executes on the server. A servlet is a small program that
executes on the server. Just as applets dynamically extend the functionality of a web
browsers, servlets dynamically extend the functionality of a web server. Thus, with the
advent of the servlet, Java spanned both sides of the client/server connection. Servlent are
used to create dynamically generated content that is then served to the client. The servlent
offers several advantages, including increased performance. Because servlents(like all
Java programs) are compiled into bytecode and executed by the JVM, they are highly
portable.
Instead of above main features there are several other important features are as
following:-
· Simple
· Secure
· Portable
· Object-oriented
· Robust
· Multithreaded
· Architecture-neutral
· Interpreted
· High performance
· Distributed
· Dynamic
The Evolution of java
The initial release of java was nothing short of revolutionary, but it did not mark the end
of java’s era of rapid innovation. The java 1.0 was the first release of java. Soon after the
release of Java 1.0, the designers of Java had already created Java 1.1.The next major
release of Java was Java 2. Where the “2”indicates “second generation.” The creation of
Java 2 was watershed event, marking the beginning of Java’s “modern age.” The first
release of Java 2 carried the version number 1.2.
Java 2 added support for a number of new features, such as Swing and the Collections
Framework, and it enhanced the Java Virtual Machine and various programming tools.
J2SE 1.3 was the first major upgrade to the original Java 2 release. For the most part, it
added to existing functionality and “tightened up” the development environment.
The release of J2SE 1.4 further enhanced Java. The release contained several important
upgrades, enhancements, and additions. For example, it added the new keyword assert.
The next release of Java was J2SE 5, and it was revolutionary. J2SE 5 fundamentally
expanded the scope, power and range of language.
· Generics
· Annotations
· Autoboxing and auto-unboxing
· Enumerations
· Enhanced, for-each style for loop
· Variable-length arguments (varargs)
· Static import
· Formatted I/O
· Concurrency utilities
JAVA SE 6
The newest release of Java is called Java SE 6. With the release of Java SE 6, Sun once
again decided to change the name of Java platform. First, notice that the “2” has been
dropped. Thus, the platform now has the name Java SE, and the official product name
is Java Platform, Standard Edition 6. As with J2SE5, the 6 in the Java SE 6 is the product
version number is 1.6.
1# :-> Write a program to illustrate the use of various Data Types of java.
class chap3basics {
public static void main(String[] args)
{
int lightspeed;
long days;
long seconds;
long distance;
lightspeed =186000;
days=1000;
seconds= days*24*60*60;
distance=lightspeed*seconds;
System.out .println("in"+ days+"days light will travel
about"+distance +"miles.");
char ch1,ch2;
ch1=88;
ch2='y';
System.out .println("ch1 $ch2"+ch1+" "+ch2);
ch1++;
System.out .println(ch1);
}
}
The output of the above program is as follows:-
In1000days light will travel about1607040000000miles.
Ch1 $ch2X y
Y
2# :-> Write a program to illustrate the use of boolean Typeof java.
class chap3booltest {
public static void main(String[]args)
{
boolean b;
b=false;
System.out.println("b is" +b);
b=true;
System.out .println("b is "+b);
if(b)
System.out .println("this is executed coz b is true");
b= false;
if(b)
System.out .println("this will not executed coz b is false");
System.out .println("10>9"+(10>9));
System.out .println("9>10"+(9>10));
}
}
The out put of the above program is as follow:-
b is false
b is true
this is executed coz b is true
10>9true
9>10false
3# :-> Write a program to illustrate some type conversion that required casts.
class chap3conversion{
public static void main(String[]args){
byte b;
int i=257;
double d=323.134;
{System.out.println("int to byte");
b=(byte) i;
System.out.println("i $ b"+i+" "+b);
System.out.println("double to int");
i= (int) d;
System.out.println("d $ i"+d+" "+i);
System.out.println("double to byte");
b= (byte)d;
System.out.println("d $ b"+d+" "+b);
}
}}
The output of the above program is as follow:-
int to byte
i $ b 257 1
double to int
d $ I 323.134 323
double to byte
d $ b 323.134 67
4# :-> Write a program to illustrate a two-dimensional array in java.
class chap3twodarray{
public static void main(String[]args){
int twodarray[] []=new int[7][7];
int i,j,k=0,l;
for(i=0;i Write a program to illustrate the bitwise logical operators.
class chap4bitlogic {
public static void main(String[] args){
String binary[]={"0000" ,"0001","0010","0011","0100","0101","0110","0111","1000",
"1001","1010","1011","1100","1101","1110","1111"};
int a=3, b=6; /** here the values of a and b are not taken from string*/
int c=a|b;
int d=a &b;
int e=a^b;
int f=(~a&b) | (a & ~b);
int g=~a &0x0f;
System.out.println("a="+"\t"+binary[a]);
System.out.println("b="+"\t"+binary[b]);
System.out.println("c="+"\t"+binary[c]);
System.out.println("d="+"\t"+binary[d]);
System.out.println("e="+"\t"+binary[e]);
System.out.println("f="+"\t"+binary[f]);
System.out.println("g="+"\t"+binary[g]);
}
}
The output of the above program is as follows:-
a= 0011
b= 0110
c= 0111
d= 0010
e= 0101
f= 0101
g= 1100
6# :-> Write a program to illustrate the switch statement.
class ch5sampleswitch{
public static void main(String[]args) {
for(int i=0;i Write a program to illustrate the for-each style for loop in java.
class ch5foreach {
public static void main(String args[] ) {
int num[] = {1,2,3,4,5,6,7,8,9,10 };
int sum=0;
for (int x : num) {
System.out.println("value is="+x);
sum += x;
if(x==5) break;
x= x*10; // this statement will not have any effect on for-each statement
}
System.out.println("summation:"+sum);
}
}
}
The output of the above program is as follows:-
Value is=1
Value is=2
Value is=3
Value is=4
Value is=5
Summation:15
8# :-> Write a program to illustrate the concept of classes with the help of
constructor.
class box {
double width;
double height;
double depth;
box() {
width=height=depth=10;
}
box(double w,double h,double d)
{this.width=w;
height=h;
depth=d;
}
double vol(){
return width*height*depth;
}
}
class ch6boxdemo6 {
public static void main(String[] args) {
box mybox=new box(5,6,4);
box mybox2=new box();
System.out.println("the vol is " +mybox.vol());
System.out.println("the vol is " +mybox2.vol());
}
}
The output of the above program is as follows:-
The vol is 120.0
The vol is 1000.0
9# :-> Write a program to illustrate static variables, methods and blocks in java.
class ch7usestatic {
static int a = 3;
static int b;
static void meth(int x){
System.out.println("x=" +x);
System.out.println("a=" +a);
System.out.println("b=" +b);
}
static {
System.out.println("static block initilized" );
b=a*4;
}
public static void main(String[] args) {
meth(42);
}
}
The output of the above program is as follows:-
Static block initialized
X=42
a=3
b=12
10# :-> Write a program to illustrate the variable-length arguments.
class ch7varargs2 {
static void vatest(String msg, int ... v) {
System.out.print(msg+ v.length +"contents:");
for(int x: v)
System.out.print(x+" ");
System.out.println();
}
public static void main(String args[])
{
vatest("one varargs:",10);
vatest("three varargs:",1,2,3);
vatest("no varargs");
}
}
The output of the above program is as follow:-
one vararges :1contents :10
three varargs :3contents :1 2 3
no varargs 0contents:
11# :-> Write a program to illustrate the concept of inheritance using super in java.
class box{
private double width,height,depth;
box(box ob) {
width=ob.width;
height=ob.height;
depth=ob.depth;
}
box(double w,double h,double d) {
width =w;
height=h;
depth=d;
}
box() {
width = -1;
height=-1;
depth=-1;
}
box(double len) {
width=height=depth=len;
}
double volume() {
return width*height*depth;
}
}
class boxweight extends box {
double weight;
boxweight(double w,double h,double d, double m)
{super(w,h,d);
weight =m;
}
boxweight(double h,double m)
{super(h);
weight =m;
}
boxweight()
{super();
weight =-1;
}
boxweight(boxweight ob)
{super(ob);
weight =ob.weight;
}
}
class ch8demoboxweight {
public static void main(String[]args) {
boxweight ob1=new boxweight(10,12,14,19);
boxweight ob2=new boxweight(1,2,3,4);
boxweight ob3=new boxweight();
boxweight ob4=new boxweight(3,4);
boxweight ob5=new boxweight(ob2);
box boxob=new box(4);
double vol;
vol=boxob.volume();
System.out.println(" \nthe volume is:" +vol);
vol=ob1.volume();
System.out.println("\n the volume is:" +vol);
System.out.println(" \nthe weight is:" +ob1.weight);
vol=ob2.volume();
System.out.println("\n the volume is:" +vol);
System.out.println();
boxob=ob2;//a refrence variable of SUPER class can be//assigned a refrence to any SUB
class
vol=boxob.volume();
System.out.println(" the volume is:" +vol);
vol=ob3.volume();
System.out.println("\n the volume is:" +vol);
vol=ob4.volume();
System.out.println("\n the volume is:" +vol);
vol=ob5.volume();
System.out.println("\n the volume is:" +vol);
}
}
The output to the above program is as follows:-
the volume is:64.0
the volume is:1680.0
the weight is:19.0
the volume is:6.0
the volume is:6.0
the volume is: -1.0
the volume is:27.0
the volume is:6.0
12# :-> Write a program to illustrate the concept of
Overriding in java.
//method overrriding.
class A {
int i,j;
A (int a, int b) {
i=a;
j=b;
}
//display i and j
void show() {
System.out.println("i and j"+i +j);
}
}
class B extends A{
int k;
B(int a,int b,int c) {
super(a,b);
k=c;
}
// display k-this overrides show() in A
void show() {
System.out.println("k ="+" "+k);
}
}
class ch8override {
public static void main(String[] args) {
B subob =new B(1,2,3);
subob.show();
}
}
The output of the above program is as follows :-
K=3
13# :-> Write a program to illustrate the concept of
packages in java.
package mypack;
class balance {
String name;
double bal;
balance(String n, double b) {
name =n;
bal =b;
}
void show() {
if(bal ");
System.out.println(name +":$"+bal);
}}
class ch9accountbalance {
public static void main(String args[]) {
balance current[] =new balance[3];
current[0] =new balance("k.j.fielding", 123.23);
current[1] =new balance("will tell", 157.02);
current[2] =new balance("tom jackson", -12.33);
for(int i=0;i tom Jackson:$-12.33
14# :-> Write a program to illustrate the concept of
interfaces in java.
interface callback {
void callback(int param);
}
class client implements callback{
//implements callback interface
public void callback(int p) {
System.out.println("callback called with " +p);
}
}
class ch9testface {
public static void main(String args[]) {
callback c=new client();
c.callback(42);
}
}
The output of the above program is as follow:-
Callback called with 42
15# :-> Write a program to illustrate the concept of
Exception handling in java.
class ch10exc2 {
public static void main(String args[]) {
int d,a;
try{
d=0;
a=42/d;
System.out.println("this will not be printed.");
}
catch(ArithmeticException e) {
System.out.println("division by zero.");
}
System.out.println("after catch statement.");
}
}
The output of the above program is as follows :-
division by zero.
After catch statement.
16# :-> Write a program to illustrate the concept of
Exception handling by using throw keyword in java.
class ch10throwdemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
}
catch(NullPointerException e) {
System.out.println("caught inside demoproc.");
throw e;
}
}
public static void main(String args[]) {
try {
demoproc();
}
catch(NullPointerException e) {
System.out.println("Recaught: "+e);
}
}
}
The output of the above program is as follow :-
Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo
17# :-> Write a program to illustrate the concept of
Multiple threads in java.
//Create multiple threads.
class NewThread implements Runnable {
String name; // name of thread
Thread t ;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name) ;
System.out.println("New thread: "+ t) ;
t.start () ; // Start the thread
}
// This is the entry point for thread.
public void run () {
try {
for(int i = 5; i > 0; i--) {
System.out. println(name + ": " + i) ;
Thread.sleep(1000) ;
}
} catch (InterruptedException e) {
System.out.println(name + " Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThreadDemo {
public static void main (String args [ ] ) {
new NewThread ("One") ; // start threads
new NewThread ("Two") ;
new NewThread ("Three") ;
try {
// wait for other threads to end
Thread.sleep(10000) ;
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted") ;
}
System.out.println("Main thread exiting.");
}
}
The output from this program is shown here:
New thread: Thread[One, 5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Three: 3
Two: 3
One: 2
Three: 2
Two: 2
One: 1
Three: 1
Two: 1
One exiting.
Two exiting.
Three exiting.
Main thread exiting.
18# :-> Write a program to illustrate the concept of enumerated data type in java.
// An enumeration of apple varieties.
enum Apple {
Jonathan, GoldenDel, RedDel, Winesap, Cortland
}
class EnumDemo {
public static void main(String args[ ])
{
Apple ap;
ap= Apple.RedDel;
// Output an enum value.
System.out.println("Value of ap: " + ap);
System.out.println();
ap = Apple.GoldenDel;
// Compare two enum values.
if(ap ==Apple.GoldenDel)
System.out.println("ap contains GoldenDel. \n");
// Use an enum to control a switch statement.
switch(ap) {
case Jonathan:
System.out.println("Jonathan is red .");
break;
case GoldenDel:
System.out.println("Golden Delicious is yellow.");
break;
case RedDel:
System.out.println("Red Delicious is red.");
break;
case Winesap:
System.out.println("Winesap is red.");
break;
case Cortland:
System.out.println("Cortland is red.");
break;
}
}
}
The output of the above program is as follows:-
Value of ap: RedDel
ap contains GoldenDel.
Golden Delicious is yellow.
19# :-> Write a program to illustrate the concept of applet in java.
import java.awt.*;
import java.applet.*;
/*
*/
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString(" A Simple Applet" , 200, 20);
}
}
To run the above code a html code is also required which is as follows:-
After running the above to codes the applet run in thewindow produced by
SimpleApplet. The following is the content of the applet.
A Simple Applet
20# :-> Write a program to illustrate the concept of
Generics in java.
//A simple genric class
//here, t is a type parameter that
//will be replace by a real type
// when an object of type gen is created.
class Gen {
T ob; // declare an object of type T
// pass the constructor a reference to an object of type T
Gen(T o) {
ob=o;
}
//return ob.
T getob() {
return ob;
}
// show type of T.
void showtype() {
System.out.println("Type of T is " + ob.getClass().getName());
}
}
class ch14gendemo {
public static void main( String[] args) {
// create a Gen reference for the integers.
Gen iob;
/* create s Gen object and assign its reference
to iob. notice the use of autoboxing to encapsulate the value
88 within an Integer object.
*/
iob= new Gen(88);
// show the type of data used by iob.
iob.showtype();
//get the value in iob.Notice that no cast is needed
int v=iob.getob();
System.out.println("value:" +v);
System.out.println();
// create a Gen object for strings.
Gen strob = new Gen("Generics Test");
// show te type of data used by strob.
strob.showtype();
// get the value of strob.Again, notice that no cast is neede.
String str =strob.getob();
System.out.println("value: "+ str);
}
}
The output of the above program is as follows:-
Type of T is java.lang.Integer
Value:88
Type of T is java.Lang.String
Value: Generics Test