(c) Copyright 2000, 2001, 2002, 2003, 2004 by Daniel Berleant
![]()
![]()
Announcements:
Today's topic: introduction
to Java
History of Java
Long, long ago, there was
BCPL (created at Bell Labs)
Then, there was B
Then, there was C
(approx. 1978)
C++ added the important
object paradigm to C
C++ is not a particularly
nice language
May 1995 - Sun announces
Java
Java is "C++ ++"
Java keeps the good
things and changes the bad in C++
Example: Java hides pointers
Example: Java has a
String data structure
Example: Java defines
graphics components
"What's next?"
JavaScript?
Java continues to
increase in popularity
I predict that the
advantages of Java mean...
Java will go away (mostly) eventually
![]()
A "Hello World"
Java Program
1. Requirements
phase:
2. Specifications
phase:
(what else can you say about the design of a "Hello World" program?!)
4. Implementation
phase:
import java.lang.*;
public class HelloWorld
{
public static void main (String [ ] args)
{
System.out.println ("Welcome to CPRE 486.");
}
}
First, save it in
a file HelloWorld.java
File name must correspond to a class name in the file!
Compile, using commandjdb HelloWorld to run it in the debugger
Analyzing HelloWorld.java
Here it is again:
/*1*/ import java.lang.*;
/*2*/
/*3*/ class HelloWorld
/*4*/ {
/*5*/ public static void main (String args[])
/*6*/ {
/*7*/ System.out.println ("Welcome to CPRE 486");
/*8*/ }
/*9*/ }
/*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: java won't be able to find a call you are using
- for java.lang only:
it will load automagically
/*2*/
(A blank line)
Like C, Java ignores blanks whenever possible
(Which ones could we remove? Where could be add
more?)
/*3*/ class
HelloWorld
Note: OOPS
knowledge is assumed in this course
class bundles
data and instructions
(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)
A Java class is very like a C++ class
A .java source file is
named after the main class in it
Example: the class name HelloWorld is stored in the file HelloWorld.java
What happens if
you put it in another file?
Why? To run a java program, say the class name
HelloWorld
Note it is "mnemonic"
(What does "mnemonic" mean?)
Note famous book, Mind of a Mnemonist
Mnemonic variable names
are an important software engineering technique!
They typify the concept of self-documenting code
Self-documenting code has at least one major advantage over
comments...
C tradition: underbars (e.g. hello_world)
Java "tradition": use
caps (e.g. HelloWorld)
Which is better? Why?
/*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???
/*5*/ public static void main(String
args[])
public means visible
outside the class
static makes it present
in memory during the entire time the program is running, so it is there when
the computer wants to run it
/*6*/ {
/*7*/ System.out.println("Hello
World!");
/*9*/ }
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 to CPRE 486");
System.exit(0);
}
}
Some key differences:
import javax.swing.JOptionPane;
JOptionPane.showMessageDialog(null, "Welcome to CPRE
486");
System.exit(0);