Professional Documents
Culture Documents
Languages
Why Java?
Java features.
Widely available.
Widely used.
Variety of automatic checks for mistakes in programs.
Embraces full set of modern abstractions.
!
Why Java?
Why Java?
Java features.
Widely available.
Widely used.
Variety of automatic checks for mistakes in programs.
Embraces full set of modern abstractions.
Java features.
Widely available.
Widely used.
Variety of automatic checks for mistakes in programs.
Embraces full set of modern abstractions.
Caveat.
Our approach.
Minimal subset of Java.
Develop general programming skills that are applicable to:
C, C++, C#, Python, Matlab, FORTRAN, . . . .
!
"I'm one of the few crazies... who believes it's very possible the
Internet has been underhyped instead of overhyped... I predict
over the next 90 days Java is going to be like a drug you rub over
venture capitalists and they go crazy." - John Doerr, May 1996
System
int
double
System.out.println()
long
String
char
Math.cos()
System.out.print()
Math.log()
Math.exp()
System.exit()
Math.sqrt()
Math.pow()
Math.min()
Math.max()
Math.abs()
Math.PI
Parsing
Integer.parseInt()
++
Double.parseDouble()
--
>
<
Long.parseLong()
<=
>=
==
<<
>>
&
Boolean
!=
String
Punctuation
Flow Control
true
false
if
else
||
&&
for
while
==
do
Arrays
Objects
""
a[i]
class
static
length()
compareTo()
new
public
private
a.length
toString()
equals()
new
main()
charAt()
Math Library
Math.sin()
Programming in Java
Programming in Java
Programming in Java.
Create the program by typing it into a text editor, and
save it as HelloWorld.java
Programming in Java.
Create the program by typing it into a text editor, and
save it as HelloWorld.java
Compile it by typing at the command line:
javac HelloWorld.java
/*******************************************
* Prints "Hello, World"
* Everyone's first program.
*******************************************/
command prompt
% javac HelloWorld.java
HelloWorld.java
10
Programming in Java
Hello, World
Programming in Java.
Create the program by typing it into a text editor, and
save it as HelloWorld.java
Compile it by typing at the command line:
A few remarks.
Name of class must match name of file.
Comments between /* and */ are ignored by computer.
Whitespace and indentation is for human readability.
Syntax coloration auto-generated by editor.
javac HelloWorld.java
!
/*******************************************
* Prints "Hello, World"
* Everyone's first program.
*******************************************/
% javac HelloWorld.java
% java HelloWorld
Hello, World
12
Data Type
Examples
Common Operations
character
'A'
'@'
compare
sequence of
characters
"Hello World"
"CS is fun"
concatenate,
compare
integer
17
12345
add, subtract,
multiply, remainder
double
floating point
number
3.1415
2.17
add, subtract,
multiply, divide
boolean
truth value
true
false
char
Description
String
int
14
Text
Ruler
'1' in Unicode
% javac Ruler.java
% java Ruler
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1
1 2 1
1 2 1 3 1 2 1
15
16
Integers
-231
Initializing Variables
231 - 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1410
18
Initializing Variables
roots =
read coefficients
from command line
calculate roots
}
19
20
false
true
x2 3x + 2
Logical Operators
x2 x - 1
x2 + x + 1
Comparison Operators
a && b
a || b
op
Description
true
false
false
false
false
false
==
equal
2 == 2
2 == 3
false
true
false
true
!=
not equal
2 != 3
2 != 2
true
false
false
true
<
less
2 < 3
3 < 2
true
true
true
true
<=
less or equal
2 <= 3
3 <= 2
>
greater
3 > 2
2 > 3
>=
greater or equal
3 >= 3
2 >= 3
!a
false
true
true
false
21
22
Type Conversion
// or divisible by 400
isLeapYear = isLeapYear || (year % 400 == 0);
System.out.println(isLeapYear);
}
}
23
int to String
(automatic)
24
Summary
Be aware.
Declare type of values.
Convert between types when necessary.
!
25
Flow-Of-Control
If-Else
Flow-of-control.
Sequence of statements that are actually executed in a program.
Conditionals and loops: enable us to harness power of the computer.
boolean 1
statement 1
true
statement 2
else
statement2
statement 1
statement 3
boolean 2
boolean
expression
if (boolean expression)
statement1
false
true
statement 2
can be a block
of statements
if-else syntax
false
true
statement1
false
statement2
statement 4
statement 3
straight-line flow-of-control
28
Oblivious Sorting
System.out.print(year);
if (isLeapYear) {
System.out.print(" is a ");
}
else {
System.out.print(" isn't a ");
}
read in
3 integers
swap A and B
if (B < A) { int t = B; B = A; A = t; }
if (C < B) { int t = C; C = B; B = t; }
if (B < A) { int t = B; B = A; A = t; }
System.out.println(A + " " + B + " " + C);
}
}
System.out.println("leap year");
% java Sort3 9 8 7
7 8 9
% java Sort3 2 1 7
1 2 7
30