Lecture 2-PRG1-11-10-final
Lecture 2-PRG1-11-10-final
FCDS
Programming I
Source Running
Editor Compiler
File Byte code JVM Program
(IDE) (javac)
(.java)
MyProgram.java MyProgram.class
Output
Basic Java programs with
println statements
A Java program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
• Its output:
Hello, world!
Compile it. .2
javac:translates the program from Java to bytecode –
bytecode: runs on many computer types (any computer with JVM) –
• System.out.println();
Prints a blank line of output.
Another Java program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println();
System.out.println("This program produces");
System.out.println("four lines of output");
}
}
• Its output:
Hello, world!
This program produces
four lines of output
Names and identifiers
• You must give your program a name.
public class MyClass {
– Naming convention: capitalize each word (e.g. MyClass)
– Your program's file must match exactly (MyClass.java)
• includes capitalization (Java is "case-sensitive")
• Compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:3: ';' expected
}
^
2 errors
– Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");
– Output:
\hello
how are "you"?\\
Questions
• What is the output of the following println
statements?
System.out.println("\ta\tb\tc");
System.out.println("\\\\");
System.out.println("'");
System.out.println("\"\"\"");
System.out.println("C:\nin\the downward
spiral");
// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}