Professional Documents
Culture Documents
Topic 2 Java Basics
Topic 2 Java Basics
Java Basics
"On the other hand, Java has already been a big win
in academic circles, where it has taken the place of
Pascal as the preferred tool for teaching the basics of
good programming…"
-The New Hacker's Dictionary version 4.3.1
www.tuxedo.org/~esr/jargon/html/The-Jargon-Lexicon-framed.html
//dataType identifier;
int x;
int y = 10;
int z, zz;
double a = 12.0;
boolean done = false, prime = true;
char mi = 'D';
– stick with int for integers, double for real numbers
Classes and Objects
– pre defined or user defined data types consisting of constructors,
methods, and fields (constants and fields (variables) which may be
primitives or objects.)
1 0 0 0 0
2 0 0 0 0
row
This is our abstract picture of the 2D array and treating
it this way is fine.
mat[2][1] = 12;
CS 307 Fundamentals of 50
Computer Science 2D Arrays
The Real Picture
0 1 2 3
0 0 0 0 0
0 1 2 3
mat 1 0 0 0 0
0 1 2 3
2 0 0 0 0
CS 307 Fundamentals of 52
Computer Science 2D Arrays
Ragged Arrays
Ragged arrays are sometime useful, but
normally we deal with rectangular matrices
– each row has the same number of columns as
every other row
– use this a lot as precondition to methods that
work on matrices
working on matrices normally requires
nested loops
– why is this so hard?
CS 307 Fundamentals of 53
Computer Science 2D Arrays
Enhanced for loop
New in Java 5.0
a.k.a. the for-each loop
useful short hand for accessing all elements in an
array (or other types of structures) if no need to
alter values
alternative for iterating through a set of values
for(Type loop-variable : set-expression)
statement
logic error (not a syntax error) if try to modify an
element in array via enhanced for loop
12 12 12
y x y x