JAVA
JAVA is a general purpose , object oriented programming language developed by Sun Microsystems of USA in 1991. Originally called oak by Jamesh Goslins ( one of the inventors of the languge. Java was designed for the development of software for computer electronic devices like TV, VCRs, tosters and many other electronic devices.
Java features
Java ensures portability in two ways. First java compiler generates the byte code instructions that can be implemented on any machine. Secondly the size of the primitive data types are machine independent.
Java is a true object oriented language. Almost everything in java is an object. All program code and data reside with object and classes. Java comes with an extensive sets of classes , arranged in packages that we can use in our programs by inheritance.
Security becomes one of the most important issue for a programming language that is used over internet. Threat of viruses and abuse of resources are everywhere. Java systems not only verify all memory access but also ensure that no viruses are communicated with an applet. The absence of pointers in java ensures that programs cannot gain access to memory locations without proper authorization.
Java programs support functions written in other languages such as c / c ++. These functions are known as native methods.
Java and C
Java is a lot like C but the major difference between java and C is that Java is an Object Oriented Language and has mechanisms to define classes and objects where as Language C is procedure Oriented. Java Team did not include some of the features of C in Java to make it simple and safe :
Java and c Plus Plus
Java is a true object oriented language while C ++ is basically C with object oriented extension. That is what the increment operator indicates. C ++ has maintained the drawback compatibility with C . Listed below are some major C Plus Plus features that were intentionally omitted from Java
C Plus Plus is a super set of C while Java is neither subset nor super set of C / C ++.
Java environment
Java Environment includes a large number of development tools and hundreds of classes and methods . The development tools are part of the system known as JDK ( Java Development Kit ) and classes and methods are part of Java Standard Library ( JSL ) , also known as the Application Programming Interface ( API ) .
Java Development Kit ( JDK )
The Java Development Kit comes with a collection of tools used for developing and running Java Programs. They include :
Application Programming Interface ( API )
The Java Standard Library or ( API ) includes hundreds of classes and methods grouped into several functional packages. Most commonly used packages are :
Java is a general purpose , object oriented programing language. We can develop to types of java programs :
Stand Alone applications are programs written in Java to carry out certain tasks on a stand alone local computer. Java can be used to develop programs for all kinds of application, which earlier were developed using languages like c and c Plus plus.
HotJava itself us a Java application program. Executing a stand alone java programs involves two steps :
Applets are small java programs developed for Internet applications. An applet located on distant computer ( server ) can downloaded via internet and executed on local computer ( Client ) using a java capable browser. We can develop applets for doing anything from simple animated graphics to complex games and untilities. Since applets are embedded in an HTML ( Hyper Text Mark Up Language ) document and run inside a web page, creating and running applets are more complex than creating an application.
Stand alone programs can read and write files and perform specific certain operations that applets cannot do. An applet can only run with in a web browser.
Simple Java Programs
Class Sampleone
{
Public static void main ( String srgs[ ])
{
System.out.println(“ Java is better than C Plus Plus “);
}
}
Class declaration
The first line
Class Sampleone
Declare a class which is an object oriented construct. Java is a true object oriented language and therefore everything must be placed inside a class . class is a keyword and declares that a new class definition follows. Sampleone is a java identifier that specifies the name of the class to be defined.
Opening Brace :
Every class in java begins with an opening brace “{“ and ends with an matching closing brace “}”appearing in the last line in the example.
The Main Line
The third line
Public static void main ( String args[])
Defines a method name main. Every java application must include the main method. This is the starting point of interpreter to be gin the execution og the program. A java application can have any number of classes but only one of them must include main method to initiate the execution.
Public : The keyword public is an acces specifier that declares the main method as unprotected and there fore making it accessible to the other classes . This is similar to the C ++ public modifier.
Static : Next appears the keyword static which declares this method as one that belongs to the entire class and not a part if any objects of the class. The main must always be declared as static since the interpreter uses this method before any objects are created.
Void : The type modifier void states that the main method does not return any value .
The output line :
The only executable statement in the rogram is
System.out.println( “ Java is better than C ++ “ ) ;
Since java us a true object oriented language , every method must be a part of an object. The println method is a member of the out object, which is static data member of System class. The line prints the string
Java is better than C ++
Java program example with multiple statements
Import java.lang.math;
Class squareroot
{
Public static voida main(String args[ ] ))
{
Double x = 5; // declaration and intitialization
Double y ;
Y = Math.sqrt(x);
System.out.ptintln (“y = “ + y );
}
}
Use of math functions
Import java.lang.Math :
The purpose of this statement is to instruct the interpreter to load the Math class from the package lang. ( This language is similar to inclue language in C ) . Math class contain the sqrt method required in the program.
Comments
Java permits both the single line comments and multiple line comments . The single line comments begin with // and ends at the end of the line . For longer comments we create comments by starting with a /* and ending with a */
An application with two classes
Class Room
{
float length;
float breadth;
void getdata( float a , float b)
{ length = a;
Breadth = b;
}
}
Class roomarea
{
Public static void main ( String args[ ])
{
Float area ;
Room room1 = new room ();// creates an object room 1
Room1.getdata ( 14, 10 ); Assigns value to length and breadth
Area = room1. Length * room1. Breadth;
System.out.ptintln( “ Area = “ + area);
}
}
Java tokens :
A java program is basically a collection of classes. A class is defined by a set of declaration statements and methods containing executable statements. Smallest individual units in a program are known as tokens. The compiler recognizes them for building up expressions and statements. Java language includes fine types of tokens:
Identifiers: Identifiers are program designed tokens . They are used for naming classes, methods, variables, objects, labels, packages and interfaces in a program. Java identifiers views the following rules:
For example
Average
Sum
Literals : Literals in java are sequence of characters ( digits , letters and other characters) that represent constant values to be stored in variables. Java specifies five major types of literals :
Implementing a Java Program
Implementation of a java application program involves a series of steps. They include :
Class test
{
Public static void main ( String args[ ] )
{
System.out.println(“Hellow ! “);
System.out.println(welcome to the world of java “);
System.out.println(“ Let us leran java “);
}
}
We must save this program file as test.java ensuring that the file name contains the class name properly. This file is calles the source file . note that all java source files will have the extension java. Note that if a program contains multiple classes, the file name must be the classname of the class containing the main method.
Compiling the program : to compile the program we must run the java compiler javac, with the name of the source file on the command line as shown
Javac test.java
If everything is okay, the javac compiler creates a file called test.class containing the bytecodes of the program. Note that the compiler automatically names the bytecode file as
<classname>.class
Running the Program :
We need to use the java interpreter to run a stand alone program. At the command promt, type
Java test
Now, the interpreter looks for the main method in the program and begins execution from there . When executed , our program displays the following :
Hello!
Welcome to the world of java
Let us learn java