You are on page 1of 10

Date: 21 Aug 2010

Session No: 1
Name of the Instructor: Shakeel
Group No: 1

Software Engineering – SE101

1. JDK Documentation
The on-line JavaTM Platform, Standard Edition (Java SE) Documentation contains API
specifications, feature descriptions, developer guides, reference pages for JDK tools and utilities,
demos, and links to related information. This documentation is also available in a download
bundle which you can install on your machine.

2. Compatibility

See Compatibility with Previous Releases on the Java Software web site for the list of known
compatibility issues. Every effort has been made to support programs written for previous
versions of the Java platform. Although some incompatible changes were necessary, most
software should migrate to the current version with no reprogramming. Any failure to do so is
considered a bug, except for a small number of cases where compatibility was deliberately
broken, as described on our compatibility web page. Some compatibility-breaking changes were
required to close potential security holes or to fix implementation or design bugs.

3. Contents of the JDK

This section contains a general summary of the files and directories in the JDKTM. For details on
the files and directories, see the JDK File Structure section of the Java SE documentation for
your platform.

Development Tools
(In the bin/ subdirectory) Tools and utilities that will help you develop, execute, debug,
and document programs written in the JavaTM programming language. For further
information, see the tool documentation.

Runtime Environment
(In the jre/ subdirectory) An implementation of the Java Runtime Environment for use
by the JDK. The JRE includes a Java Virtual Machine , class libraries, and other files that
support the execution of programs written in the Java programming language.

Additional Libraries
(In the lib/ subdirectory) Additional class libraries and support files required by the
development tools.

Demo Applets and Applications


(In the demo/ subdirectory) Examples, with source code, of programming for the Java
platform. These include examples that use Swing and other Java Foundation Classes, and
the Java Platform Debugger Architecture.

1
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

Sample Code
(In the sample subdirectory) Samples, with source code, of programming for certain Java
API's.

C header Files
(In the include/ subdirectory) Header files that support native-code programming using
the Java Native Interface, the JVM Tool Interface, and other functionality of the Java
platform.

Source Code
(In src.zip) Java programming language source files for all classes that make up the
Java core API (that is, sources files for the java.*, javax.* and some org.* packages, but
not for com.sun.* packages). This source code is provided for informational purposes
only, to help developers learn and use the Java programming language. These files do not
include platform-specific implementation code and cannot be used to rebuild the class
libraries. To extract these file, use any common zip utility. Or, you may use the Jar utility
in the JDK's bin/ directory:

jar xvf src.zip

4. The Java Runtime Environment (JRETM)


The JRE allows you to run applications written in the Java programming language. Like the JDK
, it contains the Java Virtual Machine , classes comprising the Java platform API, and supporting
files. Unlike the JDK, it does not contain development tools such as compilers and debuggers.
You can freely redistribute the JRE with your application, according to the terms of the JRE
license. Once you have developed your application using the JDK, you can ship it with the JRE
so your end-users will have a Java platform on which to run your software

5. Stack and Heap blocks


Consider the code as shown below.

class Point{
int x,y;
void offset(int ox,int oy){
x = x + ox;
y = y + oy;
}
}

class Program{
public static void main(String[] args){
Point p1 = new Point();

2
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

p1.x = 10;
p1.y = 10;
p1.offset(2,2);

Point p2 = new Point();


p2.x = 20;
p2.y = 20;

}
}

When this program runs, the reference variables ‘p1’ and ‘p2’ are present on the stack whereas
the actual objects of type ‘Point’ are created on the heap by the JVM. This is as shown in the
figure.

The instruction new Point() tells JVM to create an object of type Point. JVM always
creates these objects on the heap, and returns the reference to the reference variable (p1 and p2 in
this example). The members of the createde objects can now be accessed using the reference
variable. The referecne variable p1 and p2 are created on the stack because they part of the
main() function.

3
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

6. –classpath option
Whenever the java command is invoked the JVM searches for the class files using the
CLASSPATH environment variable .By default this is set to the current directory .If the –
classpath is given explicitly in the command line then we need to type the current directory as
“.”” .The following is the syntax for the java command

java –classpath
classpath <classpath1>;<classpath2>

where <classpath1> can be a directory or an archive(.jar or .zip files).If it is not then it is


ignored

7. Example of –classpath
classpath usage
Lets consider our simple example Example1.java

Source code:

4
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

class Point {

int x,y;

void offset(int ox,int oy){

x = x+ox;

y=y+oy;

Class Program{

public static void main(String args[]){

Point p1= new Point();

System.out.println("x= "+p1.x+", y= "+p1.y );

Lets say this code is in the directory C:\JavaProjects\

javac Example1.java creates two class files Program.class and Point.class in the same
directory.

java Program.java

gives …

x= 0, y= 0

Lets create a directory called Lib and put the Point.class in it. So the final structure of our
directories looks like this.

5
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

C:\JavaProjects

Program.class

Lib

Point.class

Invoking java Program gives the following error.


Exception in thread "main" java.lang.NoClassDefFoundError: Point
at Program.main(Example1.java:11)
Caused by: java.lang.ClassNotFoundException: Point
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

This is because the java command is not able to find the Point.class in the current directory. So
what we can do to correct this error is to add -classpath option with the Lib as well as the
current directory “.”

java -classpath Lib ; . Program

Note that we did not have to give the classpath for the class System which prints the

output because the command ,by default adds the rt.jar into the classpath.

8. Method referencing by the JIT compiler


In the example given above

6
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

Ø The method offset() will be there in the memory only once and all the refernces uses this
method only.

Ø Now the problem is when we have more than one reference objects how do the offset()
method know on which reference it is performing operations.

Ø To remove this conflict what JIT compiler do is it will introduce a new argument in the
method which holds the information of the reference which is calling the method.

Ø This is done without the knowledge of the programmer.

Ø Hence the method offset() is converted as follows:

void offset(//this pointer//,int ox,int oy){

this.x+=ox;

this.y+=oy;

now there will be no confusion and the single copy of method works on all the objects of that

7
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

8. Constructor
When an object is created, compiler performs 2 main functions
functions-
Ø memory allocation.
Ø initialization of object.(done by calling constructors).
JAVA virtual machine(JVM) allocates memory to the object, and then the constructors are
called. In JAVA, objects are created on heap, as they are allocated dynamically. Constructors are
used to instantiate
stantiate and possibly initializ
initialize an object.

For eg.

class Area
{
int radius;

Area(int radius)
{
this.radius = radius;
}
}

Some properties of constructors are


are-
Ø It has no return type.
Ø Its name is same as the class name.

In JAVA, if you do not define a constructor when you define a new class, a default constructor
that takes no parameters is defined on your behalf by source code compiler.

8
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

class Circle
{
private int radius;
Circle(int radius)
{
this.radius = radius;
}
int area(void)
{
return (22/7)*radius*radius;
}

void setRadius(int radius){


if(radius >=0){
this.radius = radius;
}

}
int getRadius(){
return radius;

}
class Program
{
public static void main(String a[])
{
Circle circle1=new Circle(5);
Circle circle2=new Circle(15);
int area1 = circle1.area();
//at the run time compiler will pass it as: circle.area(/*value of
circle1*/).
int area2 = circle2.area();
}
}
In case, we have not made any constructor, the compiler makes an inbuilt constuctor.
Area(/*radius this*/,int radius)
{
this.radius=radius;// this is used to tell to use 'this ' object
value.
}

Get and Set methods:


Ø These methods are used to increase the level of abstraction of data.

9
Date: 21 Aug 2010
Session No: 1
Name of the Instructor: Shakeel
Group No: 1

Ø Here the radius is accessible only using getRadius and the setRadius method. This helps
in adding any new constraints in the future and also prevents direct access of the data
members.

10