"source" code in Java
--> compile -->
|
bytecode --> start
up JVM -->
|
Program output-
problem solution
|
.java file
.class file
- The compiler creates a new document - in machine
independent bytecode.
- Bytecode is like machine code, but will run
on any chip - PowerPC, Pentium, etc.
- To run a program, the bytecode in the
.class file is given to an interpreter (the Java Virtual Machine, or
JVM) which converts the bytecode into machine language for a specific
chip
- The Java Virtual Machine (JVM) is
software (a program) that runs on the computer, and translates the
bytecode into low level code the computer can understand.
- Here's what you do:
- Type your source code - your Java program
- in a file.
- Compile it (either using the javac
compiler or bluej) - translate your java program into bytecode.
- Run the resulting bytecode on a
JVM.
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");
}
}
- This class is stored in a file called Hello.java
Hello, world!
This program produces
four lines of output
Java
Program Structure
public
class SomeJavaClass
{
public static void main(String[] args)
{
statement;
statement;
...
statement;
}
}
This class is stored in a source file called SomeJavaClass.java.
An application program: one or
more classes + (a single main)
method (in one of the classes).
- The body of the class is contained
in curly
braces.
- The class
header indicates the accessibility of the class and its name.
A Java class - one way to
think of it: a unit of code that is the basic building block of Java
programs.
A method: a named segment of
code
that can be called or invoked to carry
out some operations or actions.
- Methods, like classes, have a
header
and a body.
- The header:
- accessibility (usually public)
- type of data returned by method
(void
--> no data returned)
- parameter list, indicating what
information will be passed to the method
A statement:
a command to be executed.
The main method is where program execution begins.
One method is called in our main method: System.out.println.
System.out.println()
- A statement that prints a line of output on the
console.
- pronounced "print-linn" or "print-line"
- sometimes called a "println statement" for short
- Two ways to use System.out.println :
System.out.println("text");
- Prints the given message as output.
- Prints a blank line of output.
Names and identifiers
- You must give your program a name.
public class GangstaRap {
- Naming convention:
capitalize each word (e.g. MyClassName)
- Your program's file must match exactly
(GangstaRap.java)
- includes capitalization (Java is "case-sensitive")
- identifier: A
name given to an item in your program.
- must start with a letter or _ or $
- subsequent characters can be any of those or a
number
- legal: _myName TheCure
ANSWER_IS_42 $bling$
- illegal: me+u 49ers
side-swipe Ph.D's
- Use meaningful identifier names:
- Use max instead
of m
- Use currentItem
instead of c
Keywords
keyword: An identifier
that you cannot use because it already has a reserved meaning in Java.
abstract default if
private
this
boolean do
implements protected
throw
break double
import
public
throws
byte else
instanceof
return
transient
case extends
int
short
try
catch final
interface
static
void
char finally
long
strictfp
volatile
class float
native
super
while
const for
new
switch
continue goto
package
synchronized
- You cannot name a class short or const, because
those are keywords.
- Keywords are always all lowercase.
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 }
- syntax error
(compiler error): A problem in the structure of a program that causes
the compiler to fail.
- Examples:
- Missing semicolon
- Too many or too few { } braces
- Illegal identifier for class name
- Class and file names do not match
Syntax Error Example
1 public class Hello {
2 pooblic
static void main(String[] args) {
3 System.owt.println("Hello,
world!")_
4 }
5 }
Hello.java:2:
<identifier> expected
pooblic static void main(String[]
args) {
^
Hello.java:3: ';' expected
}
^
2 errors
- The compiler displays the line number where it
found an error.
- The error messages can be difficult to understand.
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:
- "This is one crazy looonnnnggg string!!"
Escape
Sequences
Two
character sequences that represent other
characters
- \n : the new line escape sequence
- \t : the tab escape sequence
- \" : double quote
- \\ : backslash
Example:
System.out.println("\\hello\nhow\tare
\"you\"?\\\\");
Output:
\hello
how are "you"?\\
Practice 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");
Write a println statement to produce this
output:
/ \ // \\ /// \\\
Question 1: What is an example of secondary storage?
A. monitor
B. CPU
C. USB flash drive
Question 2: Every Java statement must end in:
A. }
B. )
C. ;
D. .
Question 3: What is the output?
System.out.println("Hello\nWorld");
A.
Hello
World
B.
Hello World
C. Hello\nWorld
D. Hello World