Professional Documents
Culture Documents
Advanced Web Technologies
Advanced Web Technologies
Unit – II
Java
Introduction to JAVA
History of JAVA
Java is a general-purpose, object-oriented
programming language developed by Sun
Microsystems of USA in 1991.Originally called Oak by
James Gosling (one of the inventor of the language).
Java was invented for the development of software for
consumer electronic devices like TVs, toasters, etc.
The main aim had to make java simple, portable and
reliable.
Java Authors: James , Arthur Van , and others .
Hello, world
Hello World
Hello.java
class Hello {
public static void main(String[] args) {
System.out.println(“Hello World !!!”); }
}
• http://www.netbeans.org
• http://www.sun.com/forte/ffj/ce/
Prerequisites
• J2SE SDK
• J2SE Documentation (recommended)
• Installer automatically detects the location of your
JDK and documentation during the installation
process.
Features of JAVA
Java is first programming language which is not attached with
any particular hardware or operating system. Program
developed in Java can be executed anywhere and on any
system. Features of Java are as follows:
myprog.c myprog.exe
gcc machine code
C source code
OS/Hardware
Platform Independent
myprog.java myprog.class
javac bytecode
Java source code
JVM
OS/Hardware
Java Environment
class firstprogram
{
public static void main(String args[])
{
System.out.println(“This is my first program”);
}
}
Out Put : -
5. The line ―< public static void main (String args [])
shows where the program will start running. The word
main means the main method .
Comments
class Input {
public static void main(String[] args) {
System.out.println(“Hello");
System.out.println(“How are u ");
System.out.print(“ Hello");
System.out.print(“How are u");
}
}
Output
Hello
How are u
Expressions:
( ), * ,/ , +, - , mod
Operators:
&& for AND
|| for OR
! for NOT
== for equal
!= for NOT equal
Declaring Array Variables
Option A:
Syntax: datatype[] arrayname;
Option B:
Syntax: datatype arrayname[];
Syntax:
Example:
Constant Importance
‘\b‘ Back space
‘\t‘ Tab
‘\n‘ New line
‘\\‘ Backslash
‘\’’ Single coute
‘\”’ Double coute
Conditional Statements – if , if … else
if statement ex: -
if (A<B) {
System.out.println(“
A<B”);
}
System.out.println(“A is <B”);}
}
Conditional Statements – Switch
switch (A) {
case 1: {
System.out.println(“A=1”);
break;
}
case 2: {
System.out.println(“A=2”);
break;
}
…
default: {
System.out.println(“A Undefined”);
}
}
Loop statements [ While, do while, & for ]
// Processing
while (count <= 10) {
sum = sum + count;
count = count +1; //same as count++
}
Loop statements - Do while
// Variable Declaration & Initialization
// Processing
do {
sum = sum + count;
count = count +1; //same as
count++
} while (count <=10);
System.out.println(“Sum:= “ + sum);
Loop statements - For
// Processing
for (int count =1; count <= 10;count++)
{
sum = sum +count;
}
System.out.println(“Sum: “ + sum);
Packages
There are five basic Java packages:
1. java.lang Classes that are part of the basic language (e.g. Strings)
For example:
import java.io.PrintStream;
An Applet is a small Internet-based program that has the Graphical User Interface (GUI),
written in the Java programming language.
import java.awt.*;
import java.applet.*;
/* <applet code="AppletTest" width=200 height= 100<
</applet<
*/
public class AppletTest extends Applet
{
public void init()
{
System.out.println("Applet Initialised...");
setBackground(Color.cyan);
}
Example to illustrate Applet Lifecycle
public void start()
{
System.out.println("Applet Started....");
}
public void stop()
{
System.out.println("Applet Stopped....");
}
public void destroy()
{
System.out.println("Applet Destroyed....");
}
public void paint(Graphics g)
{
g.drawString("Applet Text",200,400);
showStatus("This is shown in Status.");
}
Output
Java - Swing
Swing AWT
Swing component does not AWT component can be
need any native code to implementing with code.
implement.
A GUI offers graphical icons, and visual indicators, as
opposed to text-based interfaces. This helps to
develop more efficient programs that are easy to
work with. The user can interact with the application
without any problem.
Graphical User Interface (GUI)
Labels
Buttons
Check Boxes
Radio Button
Text Area
Text Field
Scrollbar
Panels
Layout managers
Graphical User Interface (GUI)
import java.awt.*;
import java.applet.Applet;
/*<applet code="LabelTest" width=200 height=100<
</applet<
*/
public class LabelTest extends Applet
{
public void init()
{
add(new Label("A label"));
// right justify next label
add(new Label("Another label", Label.RIGHT));
}
Output of the Label Program