Intro to Java
tan.tplt
Why Java?
Multi-platform
OOP to the core
Compiled AND interpreted
Client-server
Elegant
Extensible
The Basic Tools
JDK
javac
java (JRE)
text editor
IDEs
Java contexts
'standalone' app
client - side applet
server
servelet
jsp
The old standard - Hello world
Code:
//Hi
//Hello world on command line
public class Hi{
public static void main(String args[]){
System.out.println("Hello world!!");
} // end main
} // end Hi
discussion of hello world
// comment character
/* this is also a comment */
use in standard ways - especially when getting started
public static void main
public - this code will be callable from anywhere
static - It does not require a previous instance (ignore for now)
void - this procedure will NOT return a value
main - this is the function that should automatically be called
(String args[])
This program is capable of accepting a parameter
It is expecting an array of Strings
Capitalization matters.
We'll see an example shortly
System.out.println("Hello World!!");
There's no such thing as a command!!
println is a method
system is an object
out is another object
We are invoking the println method
...of the out object
...which is a property of the System object
} // end main
End the main function
note that in java, the } ends a lot of different things
comments are very helpful
indentation is also important
} // end class
this ends the entire class definition
Once this is written, we'll save the text file
compiling
The text is not runnable directly
We'll compile it into bytecode
This is a binary code for a virtual machine language
Not quite as efficient as native machine languages
use 'javac Hi.java' from command line
no news is good news
running
virtual machine is why java is platform independant
each platform has a java virtual machine
the vm is written in platform - specific code
it interprets from java bytecode to local machine language
use 'java Hi' to run the program
Hello world with command line input:
//Hi
//Hello world on command line
public class Hi{
public static void main(String args[]){
System.out.println("Hello world!!");
System.out.println("...and a special hello to " + args[0]);
} // end main
} // end Hi
how the parameter works...
args is an array of Strings
Strings are text values
an array is a list with a numeric index
counting starts at zero
so, args[0] is the first (well, zeroth) element
it will contain the command line parameters
(space delimeted, like in C)
String concatenation
"...and a special hello to " is a String LITERAL
args[0] is a String VALUE
the + sign can attach two strings
note the trailing space
using a condition
//Hi
//Hello world on command line
public class Hi{
public static void main(String args[]){
System.out.println("Hello world!!");
if (args.length >0){
System.out.println("...and a special hello to " + args[0]);
} // end if
} // end main
} // end Hi
Why a condition here?
if there is no parameter, we will get an error
we should only give a special hello if a paramater was entered
check the array's length
length is a property of arrays
if statement
if (condition){
...code...
...code...
...code...
} // end if
conditions
statement comparing value to variable
or variable to variable
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
more on if statements
condition must go in parentheses
true expression will go in { } pair
if - else structure
//Hi
//Hello world on command line
public class Hi{
public static void main(String args[]){
System.out.println("Hello world!!");
if (args.length >0){
System.out.println("...and a special hello to " + args[0]);
} else {
System.out.println("...Howdy, stranger!");
} // end if
} // end main
} // end Hi
Simple GUI
//Hi with a GUI interface
import java.awt.*;
public class GuiHi1 extends Frame{
public static void main(String args[]){
GuiHi1 gh = new GuiHi1();
} // end main
GuiHi1(){
super("Hi");
this.show();
this.setSize(200,200);
}// end constructor
} // end class def
Adding components
//Hi with a GUI interface
import java.awt.*;
public class GuiHi2 extends Frame{
public static void main(String args[]){
GuiHi2 gh = new GuiHi2();
} // end main
GuiHi2(){
super("Hi");
this.setLayout(new FlowLayout());
this.add(new Label("Hello there!"));
this.add(new Button("OK"));
this.show();
this.setSize(200,200);
}// end constructor
} // end class def
Event Handling
//Hi with a GUI interface
import java.awt.*;
import java.awt.event.*;
public class GuiHi3 extends Frame implements ActionListener{
public static void main(String args[]){
GuiHi3 gh = new GuiHi3();
} // end main
GuiHi3(){
super("Hi");
Button btnOk = new Button("OK");
Label lblHi = new Label("Hi there");
btnOk.addActionListener(this);
this.setLayout(new FlowLayout());
this.add(lblHi);
this.add(btnOk);
this.show();
this.setSize(200,200);
}// end constructor
public void actionPerformed(ActionEvent e){
System.exit(0);
} // end actionPerformed
} // end class def
GUI input and output
//Hi with a GUI interface
import java.awt.*;
import java.awt.event.*;
public class GuiHi4 extends Frame implements ActionListener{
Button btnHi = new Button("Say Hi");
Button btnExit = new Button("Exit");
Label lblHi = new Label("Hi there");
TextField txtIn = new TextField("");
public static void main(String args[]){
GuiHi4 gh = new GuiHi4();
} // end main
GuiHi4(){
super("Hi");
btnHi.addActionListener(this);
btnExit.addActionListener(this);
this.setLayout(new GridLayout(0,1));
this.add(txtIn);
this.add(btnHi);
this.add(lblHi);
this.add(btnExit);
this.setSize(200,200);
this.show();
}// end constructor
public void actionPerformed(ActionEvent e){
String theCommand = e.getActionCommand();
if (theCommand.equals("Say Hi")){
lblHi.setText("Hi, " + txtIn.getText() + "!");
} else {
System.exit(0);
} // end if
} // end actionPerformed
} // end class def