0 index
1 Why Java?
2 The Basic Tools
3 Java contexts
4 The old standard - Hello world
5 discussion of hello world
6 public static void main
7 (String args[])
8 System.out.println("Hello World!!");
9 } // end main
10 } // end class
11 compiling
12 running
13 Hello world with command line input:
14 how the parameter works...
15 String concatenation
16 using a condition
17 Why a condition here?
18 if statement
19 conditions
20 more on if statements
21 if - else structure
22 Simple GUI
23 Adding components
24 Event Handling
25 GUI input and output

outline
created using slideshow.cgi by Andy Harris















IUPUI Computer Science: Intro to Java
1. Why Java?
  • Multi-platform
  • OOP to the core
  • Compiled AND interpreted
  • Client-server
  • Elegant
  • Extensible



































IUPUI Computer Science: Intro to Java
2. The Basic Tools
  • JDK
  • javac
  • java (JRE)
  • text editor
  • IDEs



































IUPUI Computer Science: Intro to Java
3. Java contexts
  • 'standalone' app
  • client - side applet
  • server
  • servelet
  • jsp



































IUPUI Computer Science: Intro to Java
4. 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



































IUPUI Computer Science: Intro to Java
5. discussion of hello world
  • // comment character
  • /* this is also a comment */
  • use in standard ways - especially when getting started



































IUPUI Computer Science: Intro to Java
6. 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



































IUPUI Computer Science: Intro to Java
7. (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



































IUPUI Computer Science: Intro to Java
8. 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



































IUPUI Computer Science: Intro to Java
9. } // 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



































IUPUI Computer Science: Intro to Java
10. } // end class
  • this ends the entire class definition
  • Once this is written, we'll save the text file



































IUPUI Computer Science: Intro to Java
11. 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



































IUPUI Computer Science: Intro to Java
12. 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



































IUPUI Computer Science: Intro to Java
13. 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



































IUPUI Computer Science: Intro to Java
14. 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)



































IUPUI Computer Science: Intro to Java
15. 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



































IUPUI Computer Science: Intro to Java
16. 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



































IUPUI Computer Science: Intro to Java
17. 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



































IUPUI Computer Science: Intro to Java
18. if statement

  • if (condition){
    ...code...
    ...code...
    ...code...
    } // end if



































IUPUI Computer Science: Intro to Java
19. 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



































IUPUI Computer Science: Intro to Java
20. more on if statements
  • condition must go in parentheses
  • true expression will go in { } pair



































IUPUI Computer Science: Intro to Java
21. 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



































IUPUI Computer Science: Intro to Java
22. 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



































IUPUI Computer Science: Intro to Java
23. 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



































IUPUI Computer Science: Intro to Java
24. 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



































IUPUI Computer Science: Intro to Java
25. 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



































outline

Why Java?

The Basic Tools

Java contexts

The old standard - Hello world

discussion of hello world

public static void main

(String args[])

System.out.println("Hello World!!");

} // end main

} // end class

compiling

running

Hello world with command line input:

how the parameter works...

String concatenation

using a condition

Why a condition here?

if statement

conditions

more on if statements

if - else structure

Simple GUI

Adding components

Event Handling

GUI input and output