Presentation is loading. Please wait.

Presentation is loading. Please wait.

Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.

Similar presentations


Presentation on theme: "Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow."— Presentation transcript:

1 Controlling Program Flow

2 Data Types and Variable Declarations Controlling Program Flow

3 Everything is an Object You manipulate objects with references : television\remote control Reference can stand on its own : String s A safer practice, then, is always to initialize a reference when you create it: String s = "asdf";

4 Data Types Object type ready-made types your own types : class

5 Special case: primitive types For these types Java falls back on the approach taken by C and C++. That is, instead of creating the variable using new, an “ automatic ” variable is created that is not a reference. The variable holds the value, and it ’ s placed on the stack so it ’ s much more efficient.

6

7 More All numeric types are signed, so don ’ t go looking for unsigned types. The size of the boolean type is not explicitly defined; it is only specified to be able to take the literal values true or false.

8 “ wrapper ” classes if you want to make a nonprimitive object on the heap to represent that primitive type, you use the associated wrapper : char c = ‘ x ’ ; ( primitive ) Character C = new Character('x'); ( nonprimiteve )

9 Using Java operators

10 Precedence Operator precedence defines how an expression evaluates when several operators are present : The easiest one to remember is that multiplication and division happen before addition and subtraction.

11 Assignment “ = ” Assignment of primitives is quite straightforward. when you assign primitives you copy the contents from one place to another. A=B

12 Assignment “ = ” assign objects When you assign “ from one object to another ” you ’ re actually copying a reference from one place to another. Assignment.java

13 This phenomenon is often called aliasing. If you don ’ t want aliasing to occur in this case, You could forgot the assignment and say: n1.i = n2.i; It goes against good object-oriented design principles.

14 Aliasing during method calls Aliasing will also occur when you pass an object into a method: PassObject.java

15 In many programming languages, the method f( ) would appear to be making a copy of its argument Letter y inside the scope of the method. But once again a reference is being passed so the line y.c = 'z'; is actually changing the object outside of f( ).

16 Mathematical operators The basic mathematical operators are the same as the ones available in most programming languages: addition (+), subtraction (-), division (/), multiplication (*) and modulus (%, which produces the remainder from integer division). Integer division truncates, rather than rounds, the result.

17 Relational operators 、 = 、 == 、 != Equivalence (==) and nonequivalence works with all built-in data types, but the other comparisons won ’ t work with type boolean.

18 Testing object equivalence The relational operators == and != also work with all objects, but their meaning often confuses the first-time Java programmer : public class Equivalence { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2); System.out.println(n1 != n2); }}

19 the output is actually false and then true. Naturally, this surprises people at first. What if you want to compare the actual contents of an object for equivalence? You must use the special method equals( ) that exists for all objects (not primitives, which work fine with == and !=. More

20 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)); } The result will be true, as you would expect.

21 But it ’ s not as simple as that. If you create your own class, like this: class Value { int i;} public class EqualsMethod2 { public static void main(String[] args) { Value v1 = new Value(); Value v2 = new Value(); v1.i = v2.i = 100; System.out.println(v1.equals(v2)); }}

22 the result is false ! This is because the default behavior of equals( ) is to compare references. So unless you override equals( ) in your new class you won ’ t get the desired behavior.

23 Logical operators Bitwise operators Shift operators Ternary if-else operator The comma operator

24 String operator + The + operator can be used to concatenate strings If an expression begins with a String, then all operands that follow must be Strings : int x = 0, y = 1, z = 2; String sString = "x, y, z "; System.out.println(sString + x + y + z);

25 Java uses all of C ’ s execution control statements. In Java, the keywords include if-else, while, do-while, for, and a selection statement called switch.

26 if-else The if-else statement is probably the most basic way to control program flow. The else is optional, so you can use if in two forms: if(Boolean-expression) statement or if(Boolean-expression) statement else statement

27 Iteration while(Boolean-expression) Statement do statement while(Boolean-expression); for(initialization; Boolean-expression; step) statement

28 public class WhileTest { public static void main(String[] args) { double r = 0; while(r < 0.99d) { r = Math.random(); System.out.println(r); }

29 This uses the static method random( ) in the Math library, which generates a double value between 0 and 1. (It includes 0, but not 1.) The conditional expression for the while says “ keep doing this loop until the number is 0.99 or greater. ” Each time you run this program you ’ ll get a different-sized list of numbers.


Download ppt "Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow."

Similar presentations


Ads by Google