Core JAVA: Fundamental Concepts Bootstrapping Basic Language Syntax Common Caveats Coding Conventions
Core JAVA: Fundamental Concepts Bootstrapping Basic Language Syntax Common Caveats Coding Conventions
Core JAVA: Fundamental Concepts Bootstrapping Basic Language Syntax Common Caveats Coding Conventions
nFundamental
Core JAVA
nFundamental
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Primary location for computations Devices and communication bus for user interaction, import/export of data and permanent storage High speed, volatile, scratchpad
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
RAM
Copyright 1999-2002 Simon Lok
I/O
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Programming a GPC
n
Copyright 1999-2002
Simon Lok
So we create high level languages and compilers for translating high level programs into assembly
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Multiuser/Multitasking
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Typical Topology
Most applications talk to APIs implemented by the OS kernel. n Most reasonable OS kernels talk to hardware through an HAL
n
Copyright 1999-2002 Simon Lok
HARDWARE
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
HARDWARE
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
HARDWARE
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Recompiled?
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Performance
Clearly, JAVA will always be slower than a natively coded application JIT JVM technology brings most applications within 30% of native code Latest HotSpot JVMs are within 5% of C++
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Core JAVA
nFundamental
This will have already been done for you in the computer lab
Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
For example, under Win95/98, add the following statement to the end of your AUTOEXEC.BAT file:
SET PATH=C:\JDK1.4.0_01\BIN;%PATH%
Under UNIX, edit your .profile and add the following statement:
EXPORT PATH=$PATH:/opt/jdk1.3/bin Substitute your install path for /opt
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
10
All JAVA modules begin with a class definition classes are objects n The POI (point-of-entry) of a class is the main method
n
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
11
Copyright 1999-2002
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
12
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
13
Copyright 1999-2002
Simon Lok
14
Prerequisites
J2SE SDK J2SE Documentation (recommended) Installer automatically detects the location of your JDK and documentation during the installation process
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
15
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
16
The template does most of the work, just add the System.out.println imperative
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
17
Features of Forte/NetBeans
n
Integrated debugger
Real time variable watches n Single click breakpoints
n
n n
Powerful templates
n
Copyright 1999-2002
Core JAVA
nFundamental
18
Inline Comments
public class HelloWorld { public static void main(String [] args) { // Next line prints out a message to the console System.out.println("Hello, world"); } }
Denoted by // (same as C++) n Everything between // and EOL is not compiled n Write short notes about what this particular piece of code is doing
n
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
JAVADOC Comments
n
JAVADOC comments are begun by the sequence /**, continued with a * at the beginning of each line and terminated by the */ sequence JAVADOC comments are official documentation of your code
Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
19
JAVADOC Comments
n
JAVADOC comments can be compiled into HTML files via Forte or via the JAVADOC command line tool
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Primitive Variables
n
Examples:
int x = 0; float f = 3.14159265;
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
20
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Used to represent data where the decimal point position stays constant
Example: money $18.45
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
21
Double
double d = 5.6243*Math.pow(10,250); 64-bits (max value ~10^308)
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
22
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Boolean
1-bit fixed point type Use the words true and false to assign and compare values
Char
Holds a single unicode character 16-bits (unlike the usual 8-bit ASCII)
Reference
Called pointer in C/C++ this holds an address in memory
Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
23
Literal Data
How can you tell if 12 is a byte, short, int or long? n By default, literals w/o a decimal point are int and with a decimal point are double
n You can use 12345L to make a long 12.3456F can be used for float Byte/Short dont have equivalents
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Something to Try
public class Test { public static void main(String args[]) { float f = 3.14159265; // this is okay. int x = 3.14159265; // is this valid? byte b = 32; // this is also okay. byte b2 = 130; // how about this? } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
24
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Syntax:
int[] myArray = new int[64]; myArray[15] = 9226; System.out.println(myArray[15]);
Copyright 1999-2002
25
Something to Try:
public class ArrayTest { public static void main(String [] args) { int [] myArray = new int[5]; for (int j = 0; j <= 5; j++) { // ??? myArray[j] = j*100; System.out.println(myArray[j]); } } }
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Type Casting
If you want to force one type into another, you have to cast it n This code will not compile:
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
26
Something to Try:
public class Test { public static void main(String [] args) { int x = 5000; byte smallFry = 64; long bigGuy = 1234567890; x = smallFry; // will this work? x = bigGuy; // how about this? x = (int)bigGuy; // or this? } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Scope
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
27
Something to Try:
public class Test { public static void main(String [] args) { int x = 32; System.out.println(x); { int x = 64; // this wont work int y = 74; System.out.println(x); System.out.println(y); } System.out.println(x); System.out.println(y); // wont work } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Constants
n
If you want to reserve a space in memory as being immutable, use the final keyword: final int x = 327; final double PI = 3.14159265;
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
28
Something to Try:
public class Test { public static void main(String [] args) { final int x = 32; int y = 64; System.out.println(x); System.out.println(y); x = 24; // this wont work y = 32; } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Infix Arithmetic
n
The + - / * operators work as you think that they would: int z = y + x; double fz = fx * fy + fw; In addition there is the % operator which is called modulo, it divides and takes the remainder
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
29
Something to Try:
public class Test { public static void main(String [] args) { int ix = 9; double fx = 9.0; int iy = 5; double fy = 5.0; System.out.println(ix/iy); System.out.println(fx/fy); } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Prefix/Postfix Arithmetic
n n
The operator negates a value: int y = -z; The + operator promotes: byte x = 32; int y = +x; The ++ and -- operators increment and decrement by 1 int z = x++; int y = ++x;
Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
30
the output?
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Relational Operators
Unlike arithmetic, these process numeric data into a boolean result n The common ones are:
n >, >=, <, <=, == and != n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
31
Conditional Combinations
&&, ||, ^ - implement the logical AND, OR and XOR functions boolean result = ((x > y) && (x < y));
Negation
The ! operator can prefix any boolean variable or expression It inverts the logical value of the variable or expression that it prefixes
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Something To Try:
public class Test { int x = 32, y = 32, z = 64; boolean a = (x > y); System.out.println(a); // boolean b = (x == y); System.out.println(b); // boolean c = ((y == x) && (z > System.out.println(c); // }
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
32
Bitwise Operations
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Bit Shifting
n
The >>, >>> and << operators move the bits around
int x = 16 >> 2; System.out.println(x);
Shifting can be used for quickly multiplying and dividing by two n >>> differs from >> in that >>> is unsigned >> simply pads zero
n
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
33
Hardware interaction
Most hardware provides a stream of data in the form of bytes that need to be sliced, shifted and otherwise massaged into usable form
Flags
Rather than having many boolean variables, you can have a fixed point flag variable with up to 64 flags
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Assignment Operations
n
You can assign with the = operator, but you can also combine most other operations
int x = 0; x += 5; // same as x = x + 5;
+=, -=, *=, /=, &=, >>=, etc. are all valid assignment operations n y += 6 bis faster than y = y + 6;
n
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
34
String Manipulation
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
String Comparison
n n
Syntax:
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
35
Conditional Execution
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Something to Try:
public class Test { public static void main(String [] args) { double x = 32; if(x < 0) { System.out.println(x less than zero); } else if (x > 0) { System.out.println(x greater than zero); boolean positiveNumberFlag = true; } else { System.out.println(x is zero); } System.out.println(positiveNumberFlag); // ??? } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
36
Conditional Execution
n
Another alternative:
switch(x) { case 0: System.out.println(border!); break; case 1: System.out.println(good); break; default: System.out.println(BAD!); }
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Something to Try:
public class test { public static void main(String [] args) { int x = 2; switch(x) { case 1: System.out.println(one); break; case 2: System.out.println(two); // whoops, forgot the break! case 3: System.out.println(three); break; default: System.out.println(unknown); } } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
37
Iteration
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
More Iteration
n
The do/while loop will always run the loop at least once n This is often used for user input
n
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
38
Something to Try:
public class Test { public static void main(String [] args) { int j = 0; // print out all even numbers up to 100 while (j != 99) { System.out.println(j); j += 2; } } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Break and continue can be used to stop/jump iteration blocks OUT: for (int j = 0; j < 100; j++) { for (k = 0; k < 100; k++) { if ((j % k)==0) continue OUT; System.out.println(j); } }
Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
39
Something to Try:
public class Test { public static void main(String [] args) { for (int w = 0; w < 4; w++) { MID: for (int y = 0; y < 5; y+= 2) { for (int k = 3; k > 0; k++) { if ((w + y + k) == 4) break; if ((w * y) > 6) continue MID; } } } } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
40
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Retrieving Arguments
n Recall n The
array args can be used to access the parameters n The scope of the args array is inside main n args.length gives us how many parameters were passed
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
41
Something to Try:
public class EchoArgs { public static void main(String [] args) { for (int j = 0; j < args.length; j++) { System.out.println(args[j]); } } }
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
42
Something to Try:
public class Test { public static void main(String [] args) { if (args.length < 2) { System.out.println(Must have two args); System.exit(-1); } double a0 = Double.parseDouble(args[0]); double a1 = Double.parseDouble(args[1]); System.out.println(args[0] + args[1]); System.out.println(a0 + a1); } }
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
43
Copyright 1999-2002
Simon Lok
Something to Try:
import java.io.*; public class Test { public static void main(String [] args) { InputStreamReader in = new InputStreamReader(System.in); BufferedReader con = new BufferedReader(in); boolean isGood = false; while(isGood != true) { try { System.out.print(Enter a number: ); double input = Double.parseDouble(con.readLine()); isGood = true; } catch (Exception e) { System.out.println(That was not a number!); } } if (input > 0) { System.out.println(positive); } else { System.out.println(not positive); } } Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
44
Core JAVA
nFundamental
You have not put the jdk/bin directory into your executable path
Under Win9X/ME, edit autoexec.bat Under WinNT/2K, modify system properties Under UNIX variants, edit .profile/.cshrc
Better yet, use Forte (or some other IDE) that has a built in compiler
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
45
Blah.java:14: ; expected
You forgot to end the line with the semicolon character n You forgot to match your curly braces for every { you need a }
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
46
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Infinite Loops
n
If you make a blunder on the condition, the loop may never terminate.
The while loop is prone to this particular problem n If you know how many times you are going to run a loop at compile time, use a for loop
n
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
47
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
48
Core JAVA
nFundamental
80% of the lifetime cost of a piece of software goes to maintenance. Hardly any software is maintained for its whole life by the original author. Code conventions improve the readability of the software, allowing engineers to understand new code easily. If you publish your source code, you need to make sure it is as well packaged and clean as any other product you create.
Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Copyright 1999-2002
49
Comments
We have already talked about this n JAVADOC comments at the beginning of every class, method and field n Inline comments every other line to describe what the following line of code does
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Line Length
No line of code should be > 80 characters in length n Line breaks should make sense
n
longName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // PREFER longName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // AVOID
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
50
Variables
Initialize all variables all of the time n Only declare one variable per line n For local variables, use an inline comment immediately after the variable declaration to describe what the variable is for n For fields, use a JAVADOC comment
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Indentation
All open curly braces imply that the next line should be indented n Indentation should be uniform across all files n Large indentations are a bad idea because you run out of room to nest blocks of code
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
51
Parentheses
Be explicit everywhere n Order of operations applies, but you should be explicit to make sure that anyone reading your code can easily understand what is going on
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
Identifiers
Class names should start with a capital letter and have an additional capital letter for each word in the noun phrase (MyClassName) n Methods and Variables names do not have a leading capital letter (myVar) n Constants all all caps with _ breaking the words (MY_CONSTANT)
n
Copyright 1999-2002 Simon Lok Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
52
The vast majority of software defects can be avoided through a combination of:
Thorough paper designs n Writing clean, standardized code n Proper unit testing while coding n Meticulous documentation
n
Copyright 1999-2002
Simon Lok
Reproduction and/or redistribution in whole or part without written authorization is expressively prohibited
53