CIS525 - Lecture#11 - October 11, 2000

CIS 525 LECTURE 11 10/11/00 Java, continued CHAPTER 8 2 basic data types - primitives, (built in), and reference, (pointer based) Primitives: page 299 a) boolean flag1 = false; flag2 = (6 < 7); flag3 = !true; b) char 16 bit unsigned int char c0 = 3; char c1 = 'Q'; char c2 = '\u0000'; //smallest char c3 = '\uffff'; //largest char c4 = '\b'; //back space char c5 = '\n'; //new line char c6 = '\"'; //double quote c) byte 8 bit 2's compliment d) short 16 bit 2's compliment e) int 32 bit 2's compliment int i0 = 0; int i1 = -12345; int i2 = OxCafeBabe; //'magic numbers' for .class files f) long 64 bit 2's compliment g) float 32 bit IEEE 754 -1.23f 6.02E23f //exponential - can use either upper or lower case h) double 64 bit IEEE 754 all else is 'reference' Typecasts - explicit converting among various types: type2 type2Var = (type2)type1Var;
Arithmetic Operators: operators meaning +,- addition, subtraction *, /, % multiplication, division, modulo ++, -- prefix/postfix increment/decrement <<, >>, >>> signed and unsigned shift ~ bitwise compliment &, |, ^ bitwise and, or, not
Conditional Operators: a) if(expression) statement1; else statement2; b) expression ? val1 : val2; //val1 if true, val2 if false c) switch(someInt) { case val1: statement1; break; case val2: statement2; break; | | | default: statement; }
Boolean operators: operators meaning ==, != equal, not equal, same as C++ <, <=, >, >= less than, less or equal, greater than, greater or equal &&, || logical and, logical or ! logical negation
Loops, page 312: a) pre-test: // may not execute at all i = 0; while(i < bound) { statement; i++; } b) post-test: // will execute at least once i = 0; do { statement; }while( i < bound); c) for loop: for(i = 0; i < bound; i++) { statement; i++ }
See pages 313-318 for a litany of math operations Input and Output, page 319 a) print - standard out, (like 'cout' in C++) (class PrintStream) system.out.println(arg); //prints a new line each time primitive types to strings: string.valueOf(); non-string objects to strings toString(); // method system.out.flush(); // prints all data left in buffer system.err)(); // prints standard error message b) standard input, (like 'cin' in C++) DataInputStream in = new DataInputSteam(system.in); string urlString = in.ReadLine(); char urlChar = in.read Char();
Stand Alone Java application, pg 320 => CAN execute other programs 1) get a runtime object Runtime rt = Runtime.getRuntime(); 2) execute program Process proc = rt.exec("someProgram"); 3) OPTIONAL: wait for program to execute proc.waitFor(); 4) OPTIONAL: print the results, see page 321 BufferedInputStream = new BufferedInputStream(proc.getInputStream()); DataInputStream commandResult = new DataInputStream(buffer); string S = null; try{ while((s=commandResult.readLine())!=nul) system.out.println("output: " + s); commandResult.close(); }catch (Exception e) {/* ignore read errors*/}
Reference types, page 328 import java.awt.point 1) Point p1 = new Point(1,2); // (1,2) is x,y coordinates, p1.x, p1.y 2) Point p2 = p1; // this may result in aliasing problems, see below public static Point triple(Point p) { p = new Point(p.x * 3, p.y * 3); return p; //this would create memory leak in C++ } triple(p2); // in Java, no explicit 'delete' is needed here // as 'garbage collection' is used
Arrays, page 346: Like vectors, except vectors can grow in size - arrays cannot int[] values = new int[2]; Point[] points = new Point[5]; // an array with 5 elements values[0] = 10; values[1] = 100; for(int i = 0; i < point.length; i++) { points[i] = new point(i * 2, i * 4); // new values for // x & y coordinates } Multi-dimensional arrays, page 348: int[][] values = new int[12][14]; // init a 12 x 14 array for(int i = 0; i < 12; i++) { for(int j = 0; j < 14; j++) { values[i][j] = 0; } } String[][] name0={"John", "Q", "Public"}; name1={"Jane", "Doe"}; name2={"Pele"}; since Java is an interpretted language, this is allowable, not all array entries need to have the same dimensions