Introduction To Java: Naveen Kumar
Introduction To Java: Naveen Kumar
Lecture 5
Naveen Kumar
Java Environment
1. JDK:
Appletviewer ( for viewing applets)
Javac (Compiler)
Java (Interpreter)
Javah (for C header files)
Javadoc ( for creating HTML description)
Java Environment
When the compiler sees //, it ignores all text after // in the
same line
When it sees /*, it scans for the next */ and ignores any text
between /* and */
Example
// package pack1;
// import java.lang.System;
class A
{
public static void main (String args[])
{
System.out.println("Hello World!");
}
}
Save program as A.java
Java Program Structure
Package Statement
Javac command compiles the source code A.java then,
generates A.class and store it under a directory which is
called as name of the package
package statement if used must be the first statement in
a compilation unit. Its syntax is:
package packageName;
For example:
package pack1;
Import Statement
import fullClassName;
class A
{
// program code
}
System.out.println("Hello World!");
// package pack1;
// import java.lang.System;
class A
{
public static void main (String args[])
{
System.out.println("Hello World!");
}
}
Class definition
class A
{
int i;
char ch;
void set()
{}
int get(int b)
{}
}
Method Declarations
Class A
How to access member of class A ?
{
A a= new A();
int i; a.i;
char ch; a.ch;
a.set();
void set()
{ i=20; }
stack Heap
int get()
A i
{return i; } ch
}
Types of Methods (4 basic types )
19
Example 3: two classes uses cons.
21
Introduction to Applets
22
Applet Example 1
/*
<APPLET CODE="app1.class" WIDTH=150 HEIGHT=100>
</APPLET>
*/
import java.applet.Applet;
import java.awt.Graphics;
Compile
javac app1.java
Execution
appletviewer app1.java
24
Execution through HTML file
<HTML>
<HEAD>
<TITLE> A simple Program</TITLE>
</HEAD>
<BODY> Here is the output:
<APPLET CODE="app1.class" WIDTH=150 HEIGHT=100>
</APPLET>
<BODY>
</HTML>