Lec 1
Lec 1
1. Write it.
code or source code: The set of instructions in a
program.
2. Compile it.
• compile: Translate a program from one language to
another.
byte code: The Java compiler converts your code into
a format named byte code that runs on many computer
types.
1
A 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!
2
Structure of a Java
program
class: a program
public class name {
public static void main(String[] args) {
statement;
statement; method: a named group
... of statements
statement;
}
} statement: a command to be executed
3
System.out.println
A statement that prints a line of output on the
console.
pronounced "print-linn"
sometimes called a "println statement" for short
• System.out.println("text");
• System.out.println();
4
Names and identifiers
You must give your program a name.
7
Strings
string: A sequence of characters to be printed.
Starts and ends with a quote character: "
The quotes do not appear in the output.
Examples:
"hello"
"This is a string. It's very long!"
Restrictions:
May not span multiple lines.
"This is not
a legal String."
May not contain a " character.
"This is not a "legal" String either."
8
Escape sequences
escape sequence: A special sequence of characters
used to represent certain special characters in a
string.
\t tab character
\n new line character
\" quotation mark character
\\ backslash character
Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");
Output:
\hello
how are "you"?\\
9
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");
10
Answers
Output of each println statement:
a b c
\\
'
"""
C:
in he downward spiral
11
Questions
What println statements will generate this output?
This program prints a
quote from the Gettysburg Address.
12
Answers
println statements to generate the output:
System.out.println("This program prints a");
System.out.println("quote from the Gettysburg Address.");
System.out.println();
System.out.println("\"Four score and seven years ago,");
System.out.println("our 'fore fathers' brought forth on");
System.out.println("this continent a new nation.\"");
13
Some concepts
Writing your first Java program involves a lot of
details
public static void main (String [] args) {…}
where to put semicolons
Escape sequences
etc.
14