public class HelloWorld { public static void main (String[] argv) { System.out.println ("Hello World!"); } }
javac HelloWorld.javato produce the bytecode file HelloWorld.class
cafe babe 0000 0034 001d 0a00 0600 0f09 0010 0011 0800 120a 0013 0014 0700 1507 0016 0100 063c 696e 6974 3e01 0003 2829 5601 0004 436f 6465 0100 0f4c 696e 654e 756d 6265 7254 6162 6c65 0100 046d 6169 6e01 0016 285b 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 2956 0100 0a53 6f75 7263 6546 696c 6501 000f 4865 6c6c 6f57 6f72 6c64 2e6a 6176 610c 0007 0008 0700 170c 0018 0019 0100 0c48 656c 6c6f 2057 6f72 6c64 2107 001a 0c00 1b00 1c01 000a 4865 6c6c 6f57 6f72 6c64 0100 106a 6176 612f 6c61 6e67 2f4f 626a 6563 7401 0010 6a61 7661 2f6c 616e 672f 5379 7374 656d 0100 036f 7574 0100 154c 6a61 7661 2f69 6f2f 5072 696e 7453 7472 6561 6d3b 0100 136a 6176 612f 696f 2f50 7269 6e74 5374 7265 616d 0100 0770 7269 6e74 6c6e 0100 1528 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 2956 0021 0005 0006 0000 0000 0002 0001 0007 0008 0001 0009 0000 001d 0001 0001 0000 0005 2ab7 0001 b100 0000 0100 0a00 0000 0600 0100 0000 0100 0900 0b00 0c00 0100 0900 0000 2500 0200 0100 0000 09b2 0002 1203 b600 04b1 0000 0001 000a 0000 000a 0002 0000 0005 0008 0006 0001 000d 0000 0002 000eTechnically, the bytecode is in binary ... it's just displayed in Hexadecimal format above.
java HelloWorld
When you "download" Java, you really download a development kit (usually called JDK) that contains:
In early 1990's hype, Java's designers described Java's features as:
Java: A simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, high-performance, multithreaded, dynamic language.
Let's examine some salient features (and the hype):
The source file is
a plain text file:
To compile at the command-line, use the compiler
javac
This will produce a file called
HelloWorld.class
(the executable bytecode to be interpreted)
in the same directory.
To execute (at the command line):
public class HelloWorld {
public static void main (String[] argv)
{
System.out.println ("Hello World!");
}
}
javac HelloWorld.java
java HelloWorld
where
java
is the Java interpreter).
Exercise 1.1:
Try it out!
We will now look at HelloWorld2.java,
which similar to HelloWorld.java but has
more explanation:
// File: HelloWorld2.java // Author: Rahul Simha // // Note how in-line comments start with // // I will follow the above convention of filename, author etc. public // Accessible outside this file. "public" is a reserved word. class // "class" is a reserved word. HelloWorld2 // The name of the class - same name as filename. { // Braces instead of begin/end. public // This function must be accessible. "public" is reserved. static // "static" is reserved. void // The return value - nothing. "void" is reserved. main // To start execution, a class must have a "main" function. // "main" is sort-of reserved. (String[] argv) // Command line parameters are placed by the run-time system // in argv[0], argv[1], etc. { System.out.println ("Hello World!"); // System.out is an object that contains a useful function // called println, which takes in a string and prints it out on // a new line. System.out.println ("Here's the list of arguments you gave me:"); for (int i=0; i < argv.length; i++) System.out.println ("Argument#" + i + ": " + argv[i]); // Notice how strings can be concatenated with the `+' operator. // Is `i' a string or a variable? System.out.println ("Bye!"); } }
java HelloWorld2and then as
java HelloWorld2 I dont know whats going on
Just to get a feel for a real Java program, let's
look at a more complex example:
(source file)
To compile: To execute: Exercise 1.3: Compile and execute
the above program.
// A simple application that uses the mouse to draw.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class MoreComplexExampleSwing extends JPanel implements ActionListener {
// Store current location of mouse:
int currentX=0, currentY=0;
public MoreComplexExampleSwing ()
{
this.addMouseListener (
// Unusual syntax for anonymous classes:
new MouseAdapter () {
public void mousePressed (MouseEvent m)
{
System.out.println ("mousePressed event: " + m.paramString());
int x = m.getX();
int y = m.getY();
Graphics g = MoreComplexExampleSwing.this.getGraphics();
g.drawLine (currentX, currentY, x, y);
currentX = x;
currentY = y;
}
}
);
this.addMouseMotionListener (
new MouseMotionAdapter () {
public void mouseDragged (MouseEvent m)
{
System.out.println ("mouseDragged event: " + m.paramString());
int x = m.getX();
int y = m.getY();
Graphics g = MoreComplexExampleSwing.this.getGraphics();
g.drawLine (currentX, currentY, x, y);
currentX = x;
currentY = y;
}
}
);
}
// Button-pressed events:
public void actionPerformed (ActionEvent a)
{
String eventDesc = a.getActionCommand();
Color c = Color.gray;
if (eventDesc.equalsIgnoreCase ("quit")) {
System.exit(0);
}
else if (eventDesc.equalsIgnoreCase ("red")) {
c = Color.red;
}
else if (eventDesc.equalsIgnoreCase ("blue")) {
c = Color.blue;
}
this.setBackground (c);
this.repaint();
}
void buildGUI ()
{
// Example of how a GUI is constructed:
JFrame frame = new JFrame ();
frame.setSize (500, 500);
Container cPane = frame.getContentPane();
cPane.add (this, BorderLayout.CENTER);
JPanel bottom = new JPanel ();
Button qb = new Button ("Quit");
qb.addActionListener (this);
bottom.add (qb);
Button rb = new Button ("Red");
rb.addActionListener (this);
bottom.add (rb);
Button bb = new Button ("Blue");
bb.addActionListener (this);
bottom.add (bb);
cPane.add (bottom, BorderLayout.SOUTH);
frame.setVisible (true);
}
// To start execution, "main" is needed
public static void main (String[] argv)
{
// Notice the unusual syntax:
new MoreComplexExampleSwing().buildGUI();
}
}
% javac MoreComplexExampleSwing.java
% java MoreComplexExampleSwing