1 / 14

3: Controlling Program Flow

3: Controlling Program Flow. Using Java operators Mathematical operators Relational operators Logical operators Primitive type: ALL (the same with C) String: + += All Objects: = == != Assignment/Aliasing during method calls Primitive type: byvalue (copy) Objects: byRef

nasnan
Download Presentation

3: Controlling Program Flow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3: Controlling Program Flow • Using Java operators • Mathematical operators • Relational operators • Logical operators • Primitive type: ALL (the same with C) • String: + += • All Objects: = == != • Assignment/Aliasing during method calls • Primitive type: byvalue (copy) • Objects: byRef • Sample-1 sample-2

  2. 9 n1 47 n2 9 n1 47 n2 class Number { int i; } public class Assignment { public static void main(String[] args) { Number n1 = new Number(); Number n2 = new Number(); n1.i = 9; n2.i = 47; System.out.println("1: n1.i: " + n1.i + ", n2.i: " + n2.i); n1 = n2; System.out.println("2: n1.i: " + n1.i + ", n2.i: " + n2.i); n1.i = 27; System.out.println("3: n1.i: " + n1.i + ", n2.i: " + n2.i); } } ///:~ • 1: n1.i: 9, n2.i: 47 • 2: n1.i: 47, n2.i: 47 • 3: n1.i: 27, n2.i: 27

  3. public class PassPrimitive { static void f(char y) { y = 'z'; } public static void main(String[] args) { char x ; x = 'a'; System.out.println("1: x: " + x); f(x); System.out.println("2: x: " + x); } } ///:~ 1: x: a 2: x: a class Letter { char c; } public class PassObject { static void f(Letter y) { y.c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; System.out.println("1: x.c: " + x.c); f(x); System.out.println("2: x.c: " + x.c); } } ///:~ 1: x.c: a 2: x.c: z

  4. Relational operators • All type (including boolean): == != • All primitive type but boolean: All Relational operators • < > <= >= == != • Logical Operators • AND : && • OR : || • NOT : !

  5. Testing object equivalence • Primitive type: compare value(== !=) • int i=2; if ( i= =2) … • Objects: compare reference(handle) == != • String s=“AAA”; String ss=s; if(s = = ss)… • String s=“AAA”; if(s = = new String(“AAA”) )… • SPECIAL CASE: String s=“AAA”; if(s = = “AAA”)… • Objects: compare value xxx.equals • String s=“AAA”; if(s .equals( “AAA”) )… • (For user-defined class,) method equals must be defined beforehand • If haven’t redefined, default behavior of base class: compare byref

  6. //: c03:EqualsMethod.java import com.bruceeckel.simpletest.*; public class EqualsMethod { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2)); } } ///:~ //: c03:Equivalence.java import com.bruceeckel.simpletest.*; public class Equivalence { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); } } ///:~

  7. class Number { int i; } public class EqualsMethod { public static void main(String[] args) { Number n1 = new Number (); n1.i=47; Number n2 = new Number (); n2.i=47; System.out.println( n1 ==n2 ); System.out.println(n1.equals(n2)); } } ///:~

  8. Short-circuiting • The result of a logical expression may be determined without evaluation of the latter operands. • if(test1(0) && test2(2) && test3(2))

  9. import com.bruceeckel.simpletest.*; public class ShortCircuit { static boolean test1(int val) { System.out.println("test1(" + val + ")"); System.out.println("result: " + (val < 1)); return val < 1; } static boolean test2(int val) { System.out.println("test2(" + val + ")"); System.out.println("result: " + (val < 2)); return val < 2; } static boolean test3(int val) { System.out.println("test3(" + val + ")"); System.out.println("result: " + (val < 3)); return val < 3; } public static void main(String[] args) { if(test1(0) && test2(2) && test3(2)) System.out.println("expression is true"); else System.out.println("expression is false"); } } ///:~ test1(0) result: true test2(2) result: false expression is false

  10. Bitwise operators • All primitive are signed, no unsigned type • Java has also added the unsigned right shift >>>, whichuses zero extension

  11. String operator + • There’s one special usage of an operator in Java: the + operator can be used to concatenate strings String sString = "x, y, z "; System.out.println(sString + “abc”); • If an expression begins with a String, then all operands that follow must be Strings • If needed, the compiler will turn a quoted sequence of characters into a String

  12. int x = 0, y = 1, z = 2; String sString = "x, y, z "; //String sString = new String("x, y, z “); System.out.println(sString + x + y + z); Here, the Java compiler will convert x, y, and z into their String representations instead of adding them together first. And if you say: System.out.println(x + sString); Java will turn x into a String. Q1:System.out.println(x + y + z+sString); What will be outputed? class Letter { char c; } Q2:Letter object = new Letter(); System.out.println(sString+object); What will be outputed? sString+object.toString() x, y, z Letter@xxxxx

  13. Casting operators • Java allow you to cast any primitive type to any other primitive type except for boolean. while(x = y) { // .... } int i=10; long L=(long) i; int k=(int)L • Java has no “sizeof” • Reason: all the data types are the same size on all machines. • Execution control • break and continue (label) • The infamous “goto” is not supported

  14. Exercise 6.Write a function that takes two String arguments, and uses all the boolean comparisons to compare the two Strings and print the results. For the == and !=, also perform the equals() test. In main(), call your function with some different String objects.

More Related