Getting Started
Our Java Programming Environment: DrJava Home Page: http://drjava.org DrJava requires Java 2, version 1.4 or later. It is recommended to download and use Sun's JDK 1.5.0, currently in beta releases, (from http://java.sun.com)
Starting to Program in Java /* & */ surround a >commented= area - this part of the program is NOT run, but is for some else to understand what you did /* Name Assignment Number Date Description in your own words of what the assignment does */ LIBRARY / PACKAGE attachment ... this allows the use of commands found in within the >attached= packages - java.awt.* for standard Java commands import java.awt.*;
Your First Java Program Comment line. Any line that starts with // is a comment line. // The "HelloWorld" class. public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World!"); } // main method } // HelloWorld class Defines the class name. The class name must agree with the file name, this class must be saved as HelloWorld.java. It must match exactly as Java is case sensitive Defines the start of the class. void -> means the method does not return a value. static -> loads the method into memory and keeps it there. public -> means that other classes can call this method. main -> is the method name, this is the name used for the method that starts the program. String [] args - allows for use of words / Strings { } surrounds all parts of the main block Instructs the program to print "Hello World!" ... all programming code in the main block Defines the end of the class.
Printing Techniques To print a blank line, we can simply write: System.out.println(); To print a VERY LONG LINE, we write the following: System.out.println(“This will print a Verrrrrrrrry long” + “on one line by joining” + “these strings into one line”); The process of joining strings is called (+ Sign) Concatenation
If we don’t wish to jump to a new line after printing a string, we can use the method instead of Println print System.out.print(“Hello”); System.out.print(“There!”); This will produce: HelloThere! If we Want to print a string that contains escape sequences such as double quotes, backslash, java ‘s solution is to use Immediately infront of the escape sequence. \ System.out.println(“A double quote: \””); System.out.println(“A backslash: \\”); This will produce: A double quote: ” A backslash: \
Assignment #1 P. 12 # 5, 6, 7 Due Friday 7th, 2008
Assignment # 1 Rubric