2.DataTypes Control Statements Operators

Embed Size (px)

Citation preview

  • 8/4/2019 2.DataTypes Control Statements Operators

    1/42

    I Career Craft

    [email protected]

    Data Types and Control Statements

  • 8/4/2019 2.DataTypes Control Statements Operators

    2/42

    Data Types

  • 8/4/2019 2.DataTypes Control Statements Operators

    3/42

    Built-in Data Types

    Java primitives (whole numbers)

    byte

    short

    intlong

    Java primitives (real numbers)

    float

    double

  • 8/4/2019 2.DataTypes Control Statements Operators

    4/42

    Built-in Data Types

    Java primitives (text)

    char (character, use single quotes: b)

    String *

    Java primitives (True/False)boolean

    * String is not a primitive

  • 8/4/2019 2.DataTypes Control Statements Operators

    5/42

    Variable Declarations

    Simple form

    ;

    Example

    int total;Optional initialization at declaration

    = ;

    Exampleint total = 0;

  • 8/4/2019 2.DataTypes Control Statements Operators

    6/42

    Examples

    int counter;

    int numStudents = 583;

    float gpa;

    double batAvg = .406;

    char gender;

    char gender = f;

    boolean isSafe;

    boolean isEmpty = true;

    String personName;

    String streetName = North Avenue;

  • 8/4/2019 2.DataTypes Control Statements Operators

    7/42

    Primitive Type Facts

    Type Size Min Defaultboolean false

    Max1bit false* true*

    char '\u0000' (null)2

    byte (byte) 01 -128 127

    short (short) 02 -32,768 32,767

    int 04 -2,147,483,648 2,147,483,647

    long 0L8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

    float 0.0F4 Approx3.4E+38 with 7 significant digits

    double 0.0D8 Approx1.7E+308 with 15 significant digits

    void

    * Not truly min and max. Note: Size is in Bytes

  • 8/4/2019 2.DataTypes Control Statements Operators

    8/42

    Conditionals & Iteration

  • 8/4/2019 2.DataTypes Control Statements Operators

    9/42

    Decision Statements

    if statement omitting else branch:

    if (condition)

    statement1;

    if statement omitting else branch:

    if (condition) {

    statements;

    } // if

  • 8/4/2019 2.DataTypes Control Statements Operators

    10/42

    Decision Statements

    if statement - single statement bodies:if (condition)

    statement1;

    else

    statement2;

    if statement - multiple statement blocks:if (condition) {

    statements;

    }

    else {statements;

    } // if

  • 8/4/2019 2.DataTypes Control Statements Operators

    11/42

    Multiple Selections via switch

    1. Use if construct for single selection.

    2. Use if/else construct for double selection.

    3. Use switch construct for multiple selection.

    Notes about switch:

    Useful when making a selection amongmultiple values of the same variable.

    Not useful when selecting among valuesof different variables.

  • 8/4/2019 2.DataTypes Control Statements Operators

    12/42

    Switch syntax

    switch (){

    case :

    // whatever code you want

    break; // usually need this

    case :

    // whatever code you want

    break; // usually need this

    default:

    // whatever code you wantbreak; // optional

    }

  • 8/4/2019 2.DataTypes Control Statements Operators

    13/42

    Multiple Selections via switch

    Note the "optional" default case at the end of the switch statement.It is technically optional onlyin terms of syntax.

    switch (number) {

    case 1:

    System.out.println ("One");

    break;

    case 2:

    System.out.println ("Two");

    break;

    case 3:

    System.out.println ("Three");

    break;

    default:System.out.println("Not 1, 2, or 3");

    break; // Needed???

    } // switch

    For safety and good programming practice,alwaysinclude a 'default' case.

    This mightwork without

    the defaultcase, but

    would bepoor

    technique

  • 8/4/2019 2.DataTypes Control Statements Operators

    14/42

    How many days?

    if (month == 4 || month == 6 ||

    month == 9 || month == 11)

    numdays = 30;

    else if (month = 2)

    {

    numdays = 28;

    if (leap)

    numdays = 29;

    }

    else

    numdays = 31;

  • 8/4/2019 2.DataTypes Control Statements Operators

    15/42

    Switch

    switch (month)

    {

    case 4:

    case 6:

    case 9:

    case 11:numdays = 30;

    break;

    case 2:

    numdays = 28;

    if(leap)numdays = 29;

    break;

    default: /* Good idea? */

    numdays = 31;

    }

  • 8/4/2019 2.DataTypes Control Statements Operators

    16/42

    Java example:

    while (condition){

    }

    Java Iteration Constructs: "While Loops"

    Note: Check is made before entering loop thus it is possible that loop may notexecute

  • 8/4/2019 2.DataTypes Control Statements Operators

    17/42

    Java example:

    do

    {

    statement 1;

    ...

    statement N;

    } while (condition);

    Java Iteration Constructs: "Do While Loops"

    Test Last Loop: Body of loop is guaranteed to be executed at least once.Useful when termination condition is developed inside loop.

  • 8/4/2019 2.DataTypes Control Statements Operators

    18/42

    Java Iteration Constructs: For Loop

    Java example:

    int count;

    for (count = 1; count

  • 8/4/2019 2.DataTypes Control Statements Operators

    19/42

    Secret!A for Loop can always be converted to awhile loop

    i = 0;

    while (i < 10)

    {

    System.out.println(i);

    i++;

    }

    for(i = 0; i < 10; i++)

    {

    System.out.println(i);

    }

    This will help you understand the sequence of operationsof a for loop

  • 8/4/2019 2.DataTypes Control Statements Operators

    20/42

    Operators

  • 8/4/2019 2.DataTypes Control Statements Operators

    21/42

    Operators based on the number ofoperators:

    Unary Operators

    Binary Operators

    Ternary Operators

  • 8/4/2019 2.DataTypes Control Statements Operators

    22/42

    Arithmetic Operators

    Sign Unary Operators + -

    Increment and Decrement Operators ++ --

    Basic Arithmetic Operators + - * / %

  • 8/4/2019 2.DataTypes Control Statements Operators

    23/42

    Relational Operators

    Greater than (>)

    Less than (=)

    Less than or equal to (

  • 8/4/2019 2.DataTypes Control Statements Operators

    24/42

    Logical Operators

    Bitwise Operators

    Short-circuit operators

  • 8/4/2019 2.DataTypes Control Statements Operators

    25/42

    Bitwise Logical Operators

    AND (&)

    OR (|)

    XOR (^)

    Bitwise Inversion (~)`

  • 8/4/2019 2.DataTypes Control Statements Operators

    26/42

    Short-Circuit Logical Operators

    Short-circuit Logical AND (&&)

    Short-circuit Logical OR OR (||)

    Boolean Inversion-NOT (!)

  • 8/4/2019 2.DataTypes Control Statements Operators

    27/42

    Assignment Operators

    = +=

    -=

    *=

    /=

    %=

    &=

    |=

    ^=

  • 8/4/2019 2.DataTypes Control Statements Operators

    28/42

    Advanced Operators

    Shortcut if-else statement (?:)

    Dot operator (.)

    Cast operator ()

    new

    instanceof

  • 8/4/2019 2.DataTypes Control Statements Operators

    29/42

    Equality

    Equality of primitives (==)

    Equality of references (==)

    Equality of object (equals)

  • 8/4/2019 2.DataTypes Control Statements Operators

    30/42

    Arrays

  • 8/4/2019 2.DataTypes Control Statements Operators

    31/42

    A problem with simple variables

    One variable holds one valueThe value may change over time, but at anygiven time, a variable holds a single value

    If you want to keep track of many values, youneed many variables

    All of these variables need to have names

    What if you need to keep track of hundreds orthousands of values?

  • 8/4/2019 2.DataTypes Control Statements Operators

    32/42

    Multiple values

    An array lets you associate one name with a fixed (butpossibly large) number of values

    All values must have the same type

    The values are distinguished by a numerical index between 0and array size minus 1

    12 43 6 83 14 -57 109 12 0 60 1 2 3 4 5 6 7 8 9

    myArray

  • 8/4/2019 2.DataTypes Control Statements Operators

    33/42

    Indexing into arrays

    To reference a single array element, usearray-name[ index]

    Indexed elements can be used just like simple variables

    You can access their values

    You can modify their valuesAn array index is sometimes called a subscript

    12 43 6 83 14 -57 109 12 0 6

    0 1 2 3 4 5 6 7 8 9

    myArray

    myArray[0] myArray[5] myArray[9]

  • 8/4/2019 2.DataTypes Control Statements Operators

    34/42

    Using array elements

    Examples:x = myArray[1]; // sets x to 43

    myArray[4] = 99; // replaces 14 with 99

    m = 5;

    y = myArray[m]; // sets y to -57

    z = myArray[myArray[9]];// sets z to 109

    12 43 6 83 14 -57 109 12 0 6

    0 1 2 3 4 5 6 7 8 9

    myArray

  • 8/4/2019 2.DataTypes Control Statements Operators

    35/42

    Array values

    An array may hold anytype of value

    All values in an array must be the sametype

    For example, you can have:

    An array of integers

    An array of StringsAn array of PersonAn array of arrays of StringsAn array of Object

  • 8/4/2019 2.DataTypes Control Statements Operators

    36/42

    Two ways to declare arrays

    You can declare more than one variable in the same declaration:int a[ ], b, c[ ], d; // notice position of brackets

    a and c are int arrays

    b and d are justints

    Another syntax:int [ ] a, b, c, d; // notice position of brackets

    a, b, c anddare int arrays

    When the brackets come before the first variable, they apply toallvariables in the list

  • 8/4/2019 2.DataTypes Control Statements Operators

    37/42

    Different ways of Creating the array

    int myarray [ ]= {1,2,3}; Only Values

    int myarray [ ]= new int[3]; Only size of array

    myarray[0]=1;

    myarray[1]=2;myarray[2]=3;

    int myarray [ ]= new int[] {1,2,3}; No size, only values

  • 8/4/2019 2.DataTypes Control Statements Operators

    38/42

    How to retrieve the values

    To retrieve the values from an array

    for (int i=0; i < myarray.length; i++)

    {

    System.out.println(Value is: + myarray[i]);}

    Here lengthindicates the size of an array

  • 8/4/2019 2.DataTypes Control Statements Operators

    39/42

    Arrays of objects

    Suppose you declare and define an arrayof objects:

    Person[ ] people = new Person[20];

    There is nothing wrong with this array, butit has 20 referencesto Persons in it

    all of these references are initially null

    you have not yetdefined 20 PersonsFor example, people[12].name will give

    you a nullPointerException

  • 8/4/2019 2.DataTypes Control Statements Operators

    40/42

    Arrays of arrays

    The elements of an array can be arrays

    Once again, there is a special syntax

    Declaration: int[ ][ ] table; (or int table[ ][ ];)

    Definition: table = new int[10][15];

    Combined: int[ ][ ] table = new int[10][15];The first index (10) is usually called the row index; the secondindex (15) is the column index

    An array like this is called a two-dimensional array

  • 8/4/2019 2.DataTypes Control Statements Operators

    41/42

    Different ways of creating array of arrays

    int[ ][ ] table = { {1, 2}, {3, 6}, {7, 8} };

    int[ ][ ] table = new int[3][2];

    Int[ ][ ] table = new int[ ][ ] { {1, 2}, {3, 6}, {7, 8} };

    1 2

    3 6

    7 8

    0 1

    0

    1

    2

    For example, table[1][1] contains 6

    table[2][1] contains8,andtable[1][2]is array out of bounds

    To retrieve the values:

    for (int i = 0; i < 3; i++)

    { for (int j = 0; j < 2; j++)

    {System.out.println ( table[i][j] );

    }

    }

  • 8/4/2019 2.DataTypes Control Statements Operators

    42/42

    Size of these arrays

    int[ ][ ] table = new int[3][2];

    The length of this array is the number of rows:table.length is3

    Each row contains an array

    To get the number of columns, pick a row and ask forits length:

    table[0].lengthis 2

    But remember, rows may not all be the samelength

    1 2

    3 6

    7 8

    0 1

    0

    1

    2