Lecture 2 (Chapter 2)
Lecture 2 (Chapter 2)
Lecture 2
Chapter 2:
Introduction to Java Programming
Output:
2
A Java Program:
• Package:
Group of classes.
• Class:
1. Data items
2. Methods ( or functions or operations)
• The main method is the entry point to the program.
• Java is case sensitive language.
• Console window:
Text box into which the program's output is printed.
3
Structure of a Java program
class: a program
public class name {
public static void main(String[] args) {
Body of
statement;
statement;
main method: a named group
method ...
statement; of statements
}
}
statement: a command to be executed
• System.out.println();
Prints a blank line of output.
5
Example:
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");
}
}
Output:
Hello, world!
This program produces
four lines of output
6
Compile/run a program
1. Write it.
• code or source code: The set of instructions in a program.
2. Compile it.
• javac: translates the program from Java to bytecode
• bytecode: runs on many computer types (any computer
with JVM)
3. Run (execute) it.
• output: whatever the programmer instructs the program
to do
source code byte code output
compile run Hello, World!
javac Hello.java java Hello
Identifier:
A name given to an item in your program.
• Must start with a letter or _ or $
• Do not use space or any special character.
• Do not use any reserved name like (void, class, int)
• Do not start with a number.
• legal:
_myName TheCure ANSWER_IS_42 $bling$
• illegal:
me+u 49ers side-swipe Ph.D’s
Example:
public class MyClass {
8
Syntax:
• Syntax: The set of legal structures and commands that can
be used in a particular language.
• Every basic Java statement ends with a semicolon ;
• The contents of a class or method occur between { and }
9
Programming Errors:
1- Syntax Errors:
• Detected by the compiler.
2- Runtime Errors:
• Causes the program to abort.
10
Programming Errors:
3- Logic Errors:
• Produces incorrect results.
11
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."
\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"?\\
13
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");
Answer:
a b c
\\
'
"""
C:
in he downward spiral
Questions
• Write a println statement to produce this output:
/ \ // \\ /// \\\
Answer:
Answer:
System.out.println("A \"quoted\" String is");
System.out.println("'much' better if you
learn");
System.out.println("the rules of \"escape
sequences.\"");
System.out.println();
System.out.println("Also, \"\" represents an
empty String.");
System.out.println("Don't forget: use \\\"
instead of \" !");
System.out.println("'' is not the same as \"");
Comments:
• Comment: A note written in source code by the
programmer to describe or clarify the code.
• Comments are not executed when your program runs.
• Syntax:
// comment text, on one line
or,
/* comment text; may span multiple lines */
• Examples:
// This is a one-line comment.
// second verse
System.out.println("diggy said the
boogy");
System.out.println("said up jump the
boogy");
}
}
print vs println:
println