Beruflich Dokumente
Kultur Dokumente
Lecture Note #1
Introduction to Java
Objectives
C
Java Basic
Java Translate C
Java programs in
features CS1010
into Java
programs
Compilation Error
Runtime Error
Logic Error
extension
Executing Binary
Type name of executable file a.out
output
Compiling Program
Use a Java compiler, e.g.: javac javac HelloWorld.java
Compiled binary has .class extension
The binary is also known as Java
HelloWorld.class
Executable Bytecode
Executing Binary
Run on a Java Virtual Machine (JVM) java HelloWorld
e.g.: java HelloWorld
(leave out the .class extension)
output
Note the difference here compared to C
executable
[CS1020 Lecture 1: Intro to Java] 10
2. Run Cycle Java Execution Illustration
a.out Normal executable (e.g.: C programs) are
tied to a specific platform (OS + Hardware)
This a.out cannot work in a machine of
Windows 7 on Core 2 different architecture.
Windows 7 on Core 2
They are the same
HelloWorld.class portable file.
MacOS on PowerPC
[CS1020 Lecture 1: Intro to Java] 11
3. Basic Java Program Structure
Today: just the basic language components:
Basic Program Structure
Primitive data types and simple variables
Control flow (selection and repetition statements)
Input/output statements
Purpose: ease you into the language
You can attempt to “translate” some simple C
programs done in CS1010 into Java
We will gradually cover many other Java features over
the next few weeks
Types
Q: What is assigned to d?
The (int) d expression is
Type casting: known as type casting
double d; Syntax:
int i;
(datatype) value
d = 3.14159;
i = (int) d; // i is assigned 3
Effect:
Q: What is assigned to i if d contains value is converted explicitly to
3.987 instead? the data type stated if possible.
Notes:
5.0/9 is necessary to get the correct result (what will 5/9 give?)
“+” in the printing statement
Concatenate operator, to combine strings into a single string
Variable values will be converted to string automatically
There is another printing statement, System.out.print(), which does
not include newline at the end of line (more in section 4.3)
boolean variable;
boolean isEven;
int input;
// code to read input from user omitted
if (input % 2 == 0)
Example
Operators Description
< less than
Operands are variables /
> larger than values that can be
Operators
Relational
compared directly.
<= less than or equal
>= larger than or equal Examples:
== Equal X < Y
1 >= 4
!= not equal
&& and Operands are boolean
Operators
variables/expressions.
Logical
|| or
! Examples:
not
(X < Y) && (Y < Z)
^ exclusive-or (!isEven)
A: initialization (e.g. i = 0)
for (A; B; C) { B: condition (e.g. i < 10)
... //body C: update (e.g. i++)
}
Any of the above can be empty
Execution order:
A, B, body, C, B, body, C, …
[CS1020 Lecture 1: Intro to Java] 31
4.2 Repetition Statements (2/2)
In ANSI C, the loop variable must be declared before it is
used in a ‘for’ loop
int i;
for (i=0; i<10; i++) {
...
}
import java.util.Scanner;
//Functionality provided
SYNTAX
}
TemperatureInteractive.java
//Functionality provided
System.out.print( output_string );
SYNTAX
System.out.println( output_string );
Output:
System.out.print("ABC"); ABCDEF
System.out.println("DEF"); GHI
System.out.println("GHI"); Very C-like 3.142
SYNTAX
%s for string
-: For left alignment
%b for boolean value W: For width
%c for character value P: For precision
// Returns n!
// Pre-cond: n >= 0
public static int factorial (int n) { If n is too big, say
if (n == 0) return 1; 40, what will
else return n * factorial(n-1);
} happen? Why?
public static void main(String[] args) {
int n = 5; // You can change it to interactive input
System.out.printf("Factorial(%d) = %d\n", n, factorial(n));
}
}
Expressions:
- Arithmetic Expression
- Boolean Expression
Control Flow Statements:
- Selection Statements: if-else, switch-case
- Repetition Statements: while, do-while, for
Classes:
- Scanner
- Math