Java Programming March 2005 Java Programming 1 Why




![Instantiation basic types (space allocated automatically) int n; arrays (must allocate space explicitly) int[] Instantiation basic types (space allocated automatically) int n; arrays (must allocate space explicitly) int[]](https://slidetodoc.com/presentation_image_h2/c3195bd4d1084d50196842a6d6ba5fe4/image-5.jpg)
![arrays array of Objects = array of “references” n two-step allocation Gnome[] party = arrays array of Objects = array of “references” n two-step allocation Gnome[] party =](https://slidetodoc.com/presentation_image_h2/c3195bd4d1084d50196842a6d6ba5fe4/image-6.jpg)












- Slides: 18
Java Programming March 2005 Java Programming 1
Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception handling Garbage collection some drawbacks (speed, RAM) March 2005 Java Programming 2
Object Oriented Design software is built from objects each object is an instance of a classes are arranged in a hierarchy each class defines fields and methods March 2005 Java Programming 3
Types basic (primitive) types n n boolean (true/false) char (16 bits), byte (8 bits) short (16), int (32), long (64) float (32 bits), double (64 bits) Objects n n “structured” types must be “instantiated” March 2005 Java Programming 4
Instantiation basic types (space allocated automatically) int n; arrays (must allocate space explicitly) int[] col = new int[5]; Objects Gnome g; g = new Gnome(. . . ); n (allocates reference only) (calls constructor method) several constructors may be defined for the same class, with different parameters. March 2005 Java Programming 5
arrays array of Objects = array of “references” n two-step allocation Gnome[] party = new Gnome[5]; for( int i=0; i < party. length; i++ ) { party[i] = new Gnome(. . . ); } 2 -dimensional array = “array of arrays” int table[][] = new int[10][20]; triangular arrays (allocate each row separately) int[][] table = new int[10][]; for( int i=0; i < table. length; i++ ) { table[i] = new int[i]; } March 2005 Java Programming 6
Number Objects (“wrappers”) basic type “wrapped” inside an Object Integer N = new Integer(1045); int n = N. int. Value(); Double X = new Double( 3. 934 ); double x = X. double. Value(); these classes provide useful methods Integer. parse. Int( String s ) Double. is. Infinite( double v ) March 2005 Java Programming 7
String class concatenation String s = “kilo” + “meters” several useful methods int length() char. At( int index ) int compare. To( String other ) see also String. Buffer March 2005 Java Programming 8
Methods return types parameters constructor methods main method local variables March 2005 Java Programming 9
Point class public class Point { private double x; private double y; public double get. X() { return( x ); } public double get. Y() { return( y ); } public void translate( double dx, double dy ) { x += dx; y += dy; } } March 2005 Java Programming 10
Modifiers (class/field/method) Special purpose n n n static (all instances share same value) final (can’t reassign/override/subclass) abstract (can’t instantiate) Access control n n public (anyone can access) protected (same package or subclass) friendly (default - only same package) private (only same class can access) March 2005 Java Programming 11
Expressions Literals n null n true, false n integer n Floating point n Character n String March 2005 42, 176 L, -52 I 3. 14159 ’t’ “status quo ante” Java Programming 12
Operator Precedence ++ -- ~ ! (postfix, prefix, type conversion) * / % (multiply/divide) + (add/subtract) << >> >>> (shift) < <= > >= (comparison) [also instanceof] == != (equality) & (bitwise-and) ^ (bitwise-xor) | (bitwise-or) && (and) || (or) ? : (conditional) = += -= *= /= %= >>= <<= >>>= &= ^= |= (assignment) March 2005 Java Programming 13
Casting Numberical conversion int ifrac, i=3, j=4; double dfrac, d=3. 2; dfrac = i / d; // ( i automatically cast to double) dfrac = i / (double) j ; // ( i again cast to double) ifrac = i / d; // (will cause a compilation error) ifrac = i / (int) d; //(loss of precision made explicit) String conversion String s = “” + 22; // (correct, but ugly) String s = Integer. to. String(22); // (much better) March 2005 Java Programming 14
Control Flow if( ) {. . . } else {. . . } switch loops n n n for( ; ; ) {. . . } while( ) {. . . } do {. . . } while( ); return break / continue March 2005 Java Programming 15
Input / Output - very simple System. out. println(“size: t” + x ); Input - a bit more complicated Buffered. Reader Input. Stream. Reader System. in (see, for example Copy. java) March 2005 Java Programming 16
Packages classes can be collected in a package standard packages are provided, or you can create your own import a single class import java. io. Input. Stream; import an entire package import java. io. *; March 2005 Java Programming 17
Next Time Object Oriented design Inheritance/Polymorphism Exception handling Interfaces & abstract classes Casting Design Patterns Javadoc March 2005 Java Programming 18