29
Controlling Program Flow

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

Embed Size (px)

Citation preview

Page 1: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

ControllingProgram Flow

Page 2: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

Data Types and Variable

Declarations Controlling Program Flow

Page 3: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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";

Page 4: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

Data Types

Object type ready-made types your own types : class

Page 5: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.

Page 6: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow
Page 7: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.

Page 8: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

“ 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)

Page 9: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

Using Java operators

Page 10: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.

Page 11: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

Assignment “=” Assignment of primitives is quite

straightforward.

when you assign primitives you copy the contents from one place to another.

A=B

Page 12: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

Assignment“=” assign objects

When you assign “from one object to another” you’re actually copying a reference from oneplace to another.

Assignment.java

Page 13: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.

Page 14: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

Aliasing during method calls Aliasing will also occur when you

pass an object into a method:

PassObject.java

Page 15: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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( ).

Page 16: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.

Page 17: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

Relational operators

< 、 > 、 <= 、 >= 、 == 、 != Equivalence (==) and

nonequivalence works with all built-in data types, but the other comparisons won’t work with type boolean.

Page 18: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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); }}

Page 19: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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

Page 20: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.

Page 21: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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)); }}

Page 22: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.

Page 23: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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

Page 24: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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);

Page 25: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.

Page 26: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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

Page 27: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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

step) statement

Page 28: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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

Page 29: Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow

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.