Java review
Organized by Java: How to Program chapter.
- About Java - history, compilation, etc.
- Applications, first program, arithmetic, memory.
- Applets, drawing, floating point.
- Conditional statements - if, else, while ++, --
- Conditional statements - for, do...while, switch, break, continue.
- Methods
- Arrays
- Object-Oriented Programming
- Graphical User Interface Components I
Typical Java Compilation/Execution Steps
- Edit source (.java) program (eg, your IDE)
- Compile source program to byte-codes (.class).
- Load into RAM using the Java Class Loader.
- Verify correctness of byte codes.
- Allocate stack, heap (classes and statics).
- Start JVM (Java Virtual Machine) at main (application).
- JIT. JVM may translate a method from byte code to machine code the first time it's called (Just In Time compilation).
- Execute the machine code.
- The server version of the Java run-time system may recompile from byte code to machine instructions after analyzing the performance (HotSpot).
Implications of Java Byte Code
Java Byte Code (JBC) is interpreted/translated by the Java Virtual Machine (JVM).
- Why compile to Java Byte Code instead of machine code?
- Why use JIT compilation instead of an interpreter?
- Is Java slower, same, or faster than eg C++ because of Java Byte Code.
- How does JBC affect portability?
- How does JBC affect distribution as eg Applets or WebStart?
- Can other languages be compiled into Java Byte Code?
See www.robert-tolksdorf.de/vmlanguages.html.
Design Patterns
Design patterns are "proven architectures for constructing flexible and maintainable object-oriented software". They:
- Promote design reuse.
- Help solve common problems.
- Help avoid common mistakes.
- Establish a common vocabulary among developers.
A First Program with Console Output
Similar to JHTP p 34 without comments
public class Welcome1 { public static void main(String[] args) { System.out.println("Welcome Earthlings"); } }
- What file is this in?
- What is the alternative way to declare the array parameter?
Program using Dialog Box
// Similar to JHTP p 43 without comments. import javax.swing.JOptionPane; public class Welcome2 { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Welcome Earthlings"); System.exit(0); } }
I don't expect you to know the answers to these - this time!
- Give two alternatives to the
import
. - Why is
System.exit(0)
necessary here, but not in previous example? - What does the zero mean in
System.exit(0)
? - What would often replace the
null
in this example? - Is
JOptionPane
a class or an object? - Methods that are are qualified with a class are called which? static or instance?
Converting Strings to Integers
// Similar to JHTP p 47 without comments. import javax.swing.JOptionPane; public class Add2 { public static void main(String[] args) { String s1, s2; int n1, n2; s1 = JOptionPane.showInputDialog(null, "Enter 1st number"); n1 = Integer.parseInt(s1); s2 = JOptionPane.showInputDialog(null, "Enter 2nd number"); n2 = Integer.parseInt(s2); JOptionPane.showMessageDialog(null, "Sum is " + (n1 + n2)); System.exit(0); } }
- What happens if the user enters something that's not an int?
- Are the parentheses around
(n1 + n2)
necessary?
Arithmetic Operators
Basic arithmetic operators: +, -, *, /, %
Precedence: *, /, and % are done before + or -.
If either operand of + is a string, the other operand is converted to string and the two strings are concatenated.
Arithmetic Comparison Operators
Comparison operators: <, <=, ==, !=, >=, >
Precedence: All are lower precedence than the arithmetic operators.
<, <=, >=, >
are higher than ==, !=
,
but there is no apparent reason for this except compatibility with C and C++.
The result of any comparison is boolean
true or false.
Logical Operators
- boolean true or false is the result of all logical operators.
- "Short circuit" operators (
&&, ||
): They do not need to evaluate both operands when result is known. Best choice most of the time. - And -
&&
,&
- true only if both operands are true. - Or -
||
,|
- true if either of the operands is true. - Not -
!
- true if operand is false, false if operand is true. - Xor (exclusive or) -
^
- true if the operands are different, false if they are the same. - Precedence: comparisons > & > ^ > | > && > ||
In practice, remember they are lower than comparison, but always use parentheses when mixing logical operators.