You are on page 1of 33

Java: An introduction

1
Java
Contents

1 Overview
2 A simple program
3 object
4 Save the code
5 Compile the code
6 Execute the program
7 The inside story of compilation and execution
8 JRE

2
Java
Contents

9 JVM Interpreter
10 Just in time
11 Hot spot
12 Setting up JDK
13 Environmental variables
14 Setting path environment variable
15 Setting class path environment variable
16 Types of Errors
17 comments
18 Nesting comments
3
Java
Know

• The history of the Java language


• What “object” means in OOP
• How Java programs are compiled and executed
• How Java achieves platform-independence

4
Java
Be able to

• Write a simple java program


• Set up JDK
• Comment java programs

5
Java
Overview
Our focus
Java 1.0

Java 1.1
J2SE
J2ME Flavors of Java (Java1.2- 1.4)
J2EE Java 2 Java
Java 5.0 (Java1.5)
Also called Tiger
Java 6.0 (Java1.6)
Also called Mustang
back to
Java 7.0 (Java1.7)
Also called Dolphin
6
Java
A simple java program
public class College{ Italicized words are user
public String name; defined. Blue words are java
keywords.
public void display(){
System.out.print(“Welcome to”);
System.out.print(name);
}
public static void main(String args[]){
College collegeObject= new College();
collegeObject.name=“XYZ College”;
collegeObject.display();}}
7
Java
A bulb:
2. It’s a real-world thing.
3. A thing we manufacture which can be switched on to
generate light and switched off.
4. It has real features like the glass covering, filament and
holder. (each of these features could be, by themselves, be
viewed as other things)
5. It also has conceptual features like power.
6. A bulb manufacturing factory produces many bulbs of each
type.

8
Java
A bulb:
• It’s a real-world thing. called an object in OOP

• A thing we manufacture which can be switched on to generate


light and switched off. called methods in OOP
• It has real features like the glass covering, filament and holder.
• It also has conceptual features like power.

called attributes / member variables in OOP


• 5. A bulb manufacturing factory produces many bulbs based on
a basic description / pattern of what a bulb is.

called a class in OOP


9
Java
Object
• A thing in a real world can be either physical or
conceptual. An object in object oriented
programming can be real or conceptual.
• Bulb is a real object. While college is a conceptual
object.

10
Java
public data member
public class College{
public member function
public String name;
public void display(){ Special statement used for
printing. ‘println’ causes
System.out.print(“Welcome to “); the next print statement to
System.out.println(name); be printed in the next line.

} Special method from


where class begins to run.
public static void main(
String args[]){ Creating collegeObject
College collegeObject= new College();
Accessing members of
collegeObject.name=“XYZ College”; ‘collegeObject’.
collegeObject.display(); Within member function
}} current object is assumed
Save the code
• Type the code in a text editor
• Save it as “College.java” in any directory

12
Java
Compile the code
• In the command prompt, go to the directory
where the file is stored and compile as follows:
• -- javac College.java

13
Java
Execute the program
• Compilation generates a file called “College.class”
• -- java College
• Note that the extension (.class) is not used in the
execution command.

14
Java
The inside story of compilation
and execution

Source code Byte code

javac
College.java College.class

compilation

15
Java
Byte code

College.class java Platform specific code

execution

16
Java
JRE
• Java Runtime Environment:

Java Predefined Classes


……… Date
String
Math

JRE JVM

Java Executables javac, java, javadoc …

17
Java
JVM Interpreter

JVM Interpreter
…… ….. ……. Native code
converts executes
……. …..
convertsNative code
……… …..
executes
……… ……..
………… ….. Byte code

And so on
18
Java
Just In Time
JIT

…… ….. …….
……. ….. Native code
converts
……… …..
……… …….. executes

………… ….. Byte code

And so on

19
Java
HotSpot
Hotspot executes

…… ….. ……. Native code


converts
……. …..
Same
………method call
….. Profiler- keeps track of the
native code
……… ……..
………… ….. Byte code executes

And so on

20
Java
Setting up JDK
• Java Development Kit (JDK) is the set of files
that provides us with the environment to install,
develop and execute Java applications.
• This can be download from Sun’s site.
• It is available for many operating systems
• After the JDK is installed we can develop java
applications.

21
Java
Environment variables

 PATH
 CLASSPATH

 Set up modes can be


 Temporary
 Permanent

22
Java
Setting path environment variable
Permanent setup
• If your installation directory is d:\jsdk1.5.0 in Window
95 or 98, edit autoexec.bat and enter
SET PATH=d:\jsdk1.5.0 \bin;%path%
• If you are working on Win NT /2000/XP, go to control
panel, select System A dvanced (tab) Environment
Variable (button)NEW (In the User Variable)enter
‘PATH’ as variable name and enter ‘d:\jsdk1.5.0 \bin’ as
variable value and click ok. ( Note that if there is a
variable already named ‘PATH’, then select it, click edit
and append ‘d:\jsdk1.5.0 \bin’ to the value.)
23
Java
Setting path environment variable
Assuming the installation directory is /usr/local/jdk
B. In Unix C shell( default shell in Solaris) add at the end
of ~/.cshrc file
set path=(/usr/local/jdk/bin $path)

E. For Linux( Bourne shell), add in ~/.bashrc or


~/.bash_profile
export PATH=/usr/local/jdk/bin:$PATH

24
Java
Temporary setup so that the command is available in
the command prompt:
For Windows OS ,
Enter the same command what we have seen before
in the command prompt of MS-DOS.
C:\> SET PATH=d:\jsdk1.5.0 \bin;%path%
Similarly for Linux
\usr\xyz> export
PATH=/usr/local/jdk/bin:$path
And similarly for solaris
\usr\xyz> set path=(/usr/local/jdk/bin
25
$path) Java
Setting class path environment
variable
• D:\j2sdk1.5.0\jre\lib\rt.jar is an
archive that contains all the predefined java classes
(such as String).
• These classes are automatically available to our java
programs.

26
Java
public class College{
public String name;
public void display(){
System.out.print(“Welcome to “);
System.out.println(name);}
C:\MyJava\College.java
}}
public class CollegeTest{
public static void main(String args[]){
College collegeObject= new College();
collegeObject.name=“XYZ College”;
collegeObject.display();} }
D:\MyJava\CollegeTest.java
27
Java
To set the classpath temporarily
2. Open MS-DOS prompt
3. Enter the command
SET CLASSPATH=C:/MyJava;%CLASSPATH%
3. Go to D:\MyJava and compile CollegeTest.java.
4. Execute CollegeTest class (because that is
where the main method is !)

28
Java
Types of Errors
• Compilation error : generated by the compiler.
Examples of situation when it arises:
– incorrect syntax
– if keywords are used as variables
– Bracket mismatch
– Unreachable code: while(false){…}
• Run-time error : generated by the JVM. Examples of
situation when it arises:
– Attempt to divide an integral value (not double) by
0.
– Trying to access a class that does not exist at
runtime. (What happens if you have delete
College.class and then run CollegeTest.class. 29
Java
Comments
• 3 kinds
• Single line comment : //
• Multi-line comment: /* */
• Documentation Comment : /** */

For all predefined Java classes,


Sun supplies HTML
documentation which you can
download from the sun’s site.

30
Java
/** This class is used to represent a college.
Author: XXX
Date of creation: ddd */
public class College{
/** Member variable representing the name of
the college */
public String name;
/** This method is used to display welcome
message. */
public void display(){
System.out.print(“Welcome to “);
System.out.println(name); }
/** This is where the program execution
begins.*/
public static void main(String args[]){
College collegeObject= new College();
collegeObject.name=“XYZ College”;
collegeObject.display();}
}
Use the following command to generate java
documentation.
javadoc College.java

Check out the files that are generated.


Did you notice where the commented
lines are inserted ?
Nesting comments
• Valid Nesting
/* // */ /** // */
///* */ // /** */
• Invalid Nesting
/* */ and /** */ nested with itself and
nested with each other gives error.
/* /* */ */
/** /* */ */

33
Java

You might also like