D3: Assignment Statements, Type Casting, Booleans, and Operators

Variable Initialization and Assignment Statements

  • Assignment Operator (=):

    • In computer programming, the = symbol represents the assignment operator, meaning a value is assigned to a variable.

    • In the expression x=3x = 3, the = operator assigns the integer value 33 to the variable xx.

  • Variable Initialization Rules:

    • A variable must be initialized with a value before it can be used or referenced within a program.

    • Data Type Matching: The value assigned to a variable must match the declared data type of that variable.

  • Initialization via Expressions:

    • A variable can be initialized or updated using an expression rather than a simple literal value.

    • Example expression assignment: x=5×3x = 5 \times 3.

    • Variable Reassignment Trace Example:

    • x=5x = 5

    • y=3y = 3

    • x=x×yx = x \times y

    • Evaluation: xx evaluates to 5×35 \times 3, resulting in xx holding the final value of 1515.

Type Conversion and Casting

  • Definition of Casting:

    • Casting is defined as converting a value from one data type into another data type.

  • Implicit Conversion to Decimal:

    • When an intint value is assigned to a doubledouble variable, Java automatically converts the integer value into a decimal numerical format.

Integer Division and Rounding Techniques

  • Behavior of Integer Division:

    • When dividing two intint values, the resulting value retains only the integer portion of the quotient, truncating any fractional remainder.

    • Division of integers example: int x=104int\,x = \frac{10}{4} evaluates to and returns 22.

    • Division with floating-point values example: 10.04\frac{10.0}{4} evaluates to 2.52.5.

  • Explicit Type Casting Syntax:

    • Type casting can be performed explicitly during expression evaluation: (double)x/y(double) x / y.

    • Casting double to int: int z=(int)x/yint\,z = (int) x / y converts a floating-point result into an integer data type by discarding the decimal portion.

  • Rounding to the Nearest Integer:

    • Casting can be utilized to round floating-point numbers to the nearest whole integer.

    • Procedure to round a positive floating-point number to the nearest integer:

    1. Add 0.50.5 to the doubledouble value.

    2. Explicitly cast the resulting doubledouble value as an intint.

Boolean Logic and Relational Operators

  • Boolean Fundamentals:

    • Booleans (booleanboolean) store simple binary values, specifically truetrue or falsefalse.

    • When a boolean variable is output or printed, it displays the exact binary value (truetrue or falsefalse) stored within it.

  • Relational Operators:

    • Relational operators compare two values and yield a booleanboolean result (truetrue or falsefalse).

    • Expressions using relational operators require evaluated values on both sides (e.g., int≤intint \le int evaluates to a booleanboolean).

    • List of Relational Operators:

    • <: Less than

    • >: Greater than

    • ==: Equal to

    • >=: Greater than or equal to

    • <=: Less than or equal to

    • !=: Not equal to

  • Relational Operator Code Trace Example:

    • Code snippet: java boolean timeToEat; int time = 5; timeToEat = time >= 5; &nbsp;&nbsp;&nbsp;&nbsp;

    • Evaluation: The expression time≥5time \ge 5 compares 5≥55 \ge 5, which evaluates to truetrue. Consequently, timeToEattimeToEat is assigned truetrue.

Shortcut Operators and Compound Assignment

  • Increment and Decrement Shortcut Operators:

    • x++x++: Increments the value stored in xx by 11.

    • x−−x--: Decrements the value stored in xx by 11.

  • Compound Assignment Shortcut Operators:

    • x+=1x += 1: Increments xx by 11 (equivalent to x=x+1x = x + 1).

    • x−=2x -= 2: Decrements xx by 22 (equivalent to x=x−2x = x - 2).

    • x∗=5x *= 5: Multiplies xx by 55 (equivalent to x=x×5x = x \times 5).