170 likes | 270 Views
Java. Getting Started. "Hello, World" Application. public class Hello { public static void main (String args[ ]) { System.out.println ("Hello, World!"); } }. Memorize this: public static void main (String args[ ]) Use System.out.println for output.
E N D
Java Getting Started
"Hello, World" Application public class Hello { public static void main (String args[ ]) { System.out.println ("Hello, World!"); } } • Memorize this: public static void main (String args[ ]) • Use System.out.println for output
Compiling a Java program • The program defined public class Hello • Therefore, the file must be named Hello.java • Compile the program with javac Hello.java • This creates a file named Hello.class • Hello.class contains Java byte code • Run the program with java Hello • This runs the Java virtual machine (interpreter)
"Hi, World" Applet import java.applet.*; import java.awt.*; public class HiWorld extends Applet { public void paint (Graphics g) { g.drawString ("Hi, world!", 50, 100); } }
No main method • Yes, applets have a main method... • ...but it's in the browser, not in your code!
import Statements • import java.applet.*; makes available all the classes and objects in the java.applet package. • import is not #include -- it doesn't make the program bigger, it just makes things easier to reference. • Without importjava.applet.*; you could still say java.applet.paint (...)instead of paint (...)
The paint method • public void paint (Graphics g) { g.drawString ("Hi, world!", 50, 100); } • This is a method you supply in class HiWorld • It overrides the predefined paint (Graphics) method in Applet • If you don't define this method, you get the inherited method (which does nothing!)
"Hi, World" Applet (repeat) import java.applet.*; import java.awt.*; public class HiWorld extends Applet { public void paint (Graphics g) { g.drawString ("Hi, world!", 50, 100); } }
The Person class class Person { String name; int age = 20; void birthday ( ) { age++; System.out.println (name + " is now "+ age); } }
The People program public class People { public static void main (String args [ ]) { Person john; // "People" uses "Person" john = new Person ( ); Person mary = new Person ( ); john.name = "John Doe"; john.birthday ( ); } }
Sending a message • You don't "call a function," you send a message to an object • The object may execute one of its own methods • The object may execute an inherited method • You generally need a pretty clear idea of what methods are being inherited
toString • toString( ) is defined at Object, and is therefore inherited by every object • System.out.println, when given any object, automatically calls that object's toString method • The resultant output is better than nothing • It's a good idea to define toString( ) for every class you write
An example toString( ) method • Returns something like "John, age 34" class Person { String name; int age; public String toString ( ) { return name + ", age " + age;}
Parameter transmission • In Java, • primitives (int, double, char, boolean, etc.) are passed by value • objects (Person, String, arrays, etc.) are passed by reference • This rule is simple and easy to work with • You never accidentally get a copy of an object • If you need to copy an object, do it "by hand"
Procedural thinking • Procedural programs use functional decomposition • Decide what program is to do • Write a top level procedure, inventing new lower-level procedures as you go • Write lower-level procedures • Continue until program is fully coded • Goal: small, relatively independent parts
Object thinking • Decide what program is to do • Decide what objects you need • For each object, decide • what are its responsibilities • with which other objects it must collaborate • Goal: small, relatively independent parts