Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Announcements
Announcements II
Recall: get to the course Web site through
my personal home page:
http://ifsc.ualr.edu/jdberleant
Lecture notes, supplementary notes,
assignments, etc. are on the course site
Links to lecture materials from previous semesters
are present, but unbolded
Note that the Sun Web site has an
online Java reference Web site
Why Java?
We practiced using a Hello World program
Now let’s understand the theory…
But first, why do a unit on Java at all?
Java is one of the better languages to
know about
The results of information systems
analysis will be implemented
History of Java
Long, long ago, there was BCPL (from Bell Labs)
Then, there was B
Then, there was C (approx. 1978)
Unix was written in C
C++ added the important object paradigm to C
It certainly can’t hurt
History of Java (cont.)
Java keeps the good things in C++ and
changes the bad
C++ is not a particularly nice language
...pointers and other complexities abound
…so…
May 1995 - Sun announces Java
Java is "C++ ++"
…maybe in Java
So… understanding implementation
leads to wiser analysis
HW2 depends on your success with HW1
Items updated for this semester are bolded
Today’s Topic: Intro to Java
http://java.sun.com/javase/7/docs/api/
HW1 due today
HW2 assigned today
Example: Java hides pointers
Example: Java has a String data structure
Example: Java defines graphics
components
"What's next?"
1
"What's next?"
Aspect-oriented programming
A radical redesign … a “design language”?
Rate of increase in software productivity = 6%/year
(e.g. AspectJ)? Java++?
A "Hello World" Java Program
(Schach)
This is much less than many hardware advances!
Java won't go away any time soon
Java will go away (to a degree) eventually
(might take a long time)
A "Hello World" Java Program
1. Analysis phase
2. Design phase
3. Implementation phase
The actual code
Let’s take a look
Compile then run the code
First, save it in a file HelloWorld.java
File name must correspond to a class name in
code!
javac for "java compiler"
You must type the .java suffix
Since javac knows other suffixes, so you must say which
Compilation produces HelloWorld.class
It will be small
It will have one "method" ("function")
Not much to say about a “Hello World program!
3. Implementation phase
Hello World - code
public class HelloWorld {
public static void main (String [ ]
args) {
System.out.println("Welcome”+
“to IFSC 7310.");
}
}
(we’ll analyze the code in a bit)
Compile then run the code
To run, type
Don't type the suffix this time
Compile, using the command
javac HelloWorld.java
Print a simple string to the screen in a Java
program
2. Design phase
I predict that the advantages of Java mean...
1. Analysis phase:
C:…blah\blah> java HelloWorld
(Because then it won't work)
Reason: You're supposed to give the class name,
not the file name
Java adds the .class suffix to get the file
name
..because it knows the class is in a file
(If you typed HelloWorld.class, Java would then look
for HelloWorld.class.class – bad news!)
2
Analyzing HelloWorld.java
/*1*/ import java.lang.*;
/*2*/
/*3*/ class HelloWorld
/*4*/ {
/*5*/ public static void main (String args[])
/*6*/ {
/*7*/ System.out.println ("Welcome!");
/*8*/ }
/*9*/ }
(what’s different this time?)
Let’s start analyzing it…
/*1*/ import java.lang.*
Many calls are in libraries
To use something in a library, import it
One library is called java.lang
System.out.println() is in java.lang
What happens if you forget to import it?
Usually:
But for java.lang only:
Java won't be able to find a method you are using
it will still be imported automagically anyway
Let’s continue analyzing it…
/*2*/
(A blank line)
Like most languages…
Let’s continue analyzing it…
Java ignores most blanks
Which ones could we remove?
Where could we add more?
/*3*/ class HelloWorld
A class bundles data and instructions into a
named chunk
Let’s continue analyzing it…
/*3*/ class HelloWorld
Example:
A Java class is very like a C++ class
A .java source file is named after the main
class in it
/*3*/ class HelloWorld
Note name of class is "mnemonic"
(What does "mnemonic" mean?)
Mnemonic names:
Give java.exe the class name
C:…blah\blah> java HelloWorld
(A struct type in C only bundles data)
(A struct type in C++ bundles data and
instructions)
(A C++ class bundles data and instructions like a
Java class)
Let’s continue analyzing it…
What happens if you put it in another file?
Won't work! Error message. Can't find class
Why? To run a java program:
Class HelloWorld is stored in file
HelloWorld.java
(why? Why only?)
A simple but important software technique!
They typify a principle: self-documenting code
Self-documenting code has major advantages over
comments...
(like what?)
C tradition: underbars (e.g. hello_world)
Java tradition: use caps (e.g. HelloWorld)
Which is better? Why?
3
/*4*/
{
Opens a block of code
Like in C/C++, Pascal, etc.
Can have variables and methods/functions in
this block
Best to indent 3 spaces???
2 & 4 spaces were found best for Pascal
3 was not tested
3 is between 2 and 4
... why indent at all?
public means visible outside the class
static pre-creates it automatically
{
Opening curly brace starts a block of code
...in this case, the code of the main( )
method/function
There is a System module
System has a locally defined data structure
out
out contains a println( ) routine
(cont.)
/*8*/
the moment of program start until the program ends
…void main (String args[])
/*7*/ System.out.println
("Hello World!");
The program need not create it dynamically
“static”is the opposite of “dynamic”
It will exist from
Spaces tell the computer nothing
Does good spacing affect the information quality?
/*6*/
/*5*/ public static void
main(String args[])
Routines are called "methods" in Java, "functiosn"
in C
println(“hi” ) simply prints hi to the screen
(cont.)
}
Closing curly brace matches opening curly brace
. . . in this case, ends the block
. . . also the body of the method/function
/*9*/
}
. . . ends the class definition
. . . this class is very simple
. . . it only has
one method/function, and
no variables
4
A HelloWorld.java with a GUI
import java.lang.*;
import javax.swing.JOptionPane;
public class Hello{
public static void main(String [ ] args){
JOptionPane.showMessageDialog
(null, "Welcome!");
System.exit(0);
}
}
A HelloWorld.java with a GUI
Some key differences:
import javax.swing.JOptionPane;
gets an object, JOptionPane, from the
javax.swing library
(in contrast, we got everything from the java.lang
library)
JOptionPane.showMessageDialog(null,
"Welcome!");
call showMessageDialog(...), a method of
object JOptionPane
when run, this brings up a dialog box
(you click "ok" to get rid of it)
A HelloWorld.java with a GUI
More key differences:
System.exit(0);
Causes the program to exit
(Because the GUI sits there with life of its own, and
should be explicitly exited)
...main() may end, but the GUI still runs
5