Java Programming Review

Java Review

Variables in Java

  • Definition of a Variable: A variable is a piece of data in memory characterized by:

    • Identifier: Name of the variable

    • Type: Data type that informs what kind of data the variable holds and what operations can be performed on it. Types are a foundational component in programming languages.

  • Primitive Types in Java:Java defines eight primitive data types:

    • int

    • double

    • char

    • boolean

    • byte

    • short

    • long

    • float

  • Examples of Primitive Types:

    • int: 12

    • double: 2.6

    • char: 'r'

    • boolean: true

    • Each primitive type can hold a single value.

Declaration and Initialization of Variables
  • Declaring a Variable: To declare a variable means to state that it exists and to assign a type and a name to it. Example:

    • boolean areWeThereYet;

  • Initializing a Variable: To initialize a variable means to give it an initial value. This is frequently combined with variable declaration. Example:

    • boolean areWeThereYet = false;

  • Final Variables: Variables declared as final are constants and cannot be altered after initialization. Example:

    • final int theMeaningOfLife = 42;

Assignment of Variables
  • Assignment Operator: After declaration, a variable can be assigned a new value using the assignment operator =. Example:

    • areWeThereYet = true;

  • Arithmetic Assignment Operations: Arithmetic expressions can also be used with assignments. Example:

    • age = currentYear - birthYear;

Arithmetic Operations

  • Operations Supported: Arithmetic operations are executed on primitive types and include:

    • Binary Operators: +, -, *, /, %

    • Unary Operators: ++ (increment), -- (decrement)

  • Order of Operations in Java: Java follows common order-of-operation rules, which prioritize unary operations, followed by multiplicative and then additive. The lowest precedence is for the assignment operator.

Operator Precedence
  • Precedence Levels (from highest to lowest):

    • Access array element, access object member, invoke a method, post-increment, and post-decrement

    • Pre-increment, unary plus, unary minus, logical NOT, bitwise NOT

    • Multiplicative operators (*, /, %)

    • Additive operators (+ for string concatenation, -)

Type Conversion in Java
  • Widening Conversion: When operators are applied to operands of different types, Java uses widening conversion (also known as promotion). This converts data to a type that has the same or greater storage size, e.g., short -> int, int -> long.

  • Narrowing Conversion: This converts data to a type with fewer bits of storage, potentially losing information, for example, double -> float, float -> int.

Mixing Data Types
  • Order of Operations: Conversions occur on one operator at a time, in the order the operators are evaluated. Example:

    • 3 / 2 * 3.0 + 8 / 3 evaluates to 5.0

  • String Concatenation: Has the same precedence as additive operations and is evaluated from left to right. Example:

    • 1 + "x" + 4 evaluates to "1x4", and "2+3=" + 2 + 3 evaluates to "2+3=23".

Type Casting in Java
  • Definition: Type casting is a method to convert one type to another. Examples include:

    • Convert int to double for floating-point division: Double average = (double) 12 / 5;

    • Truncate a double to an int: int feet = (int) (25.0 / 12.0);

Assignment Operators
  • Basic Assignment: The assignment operator is =.

  • Combined Assignment Operators: Include +=, -=, *=, =/.

  • Increment/Decrement Operators: Can function as standalone statements. Examples:

    • i++;, i--;, ++i;, --i;

Control Flow in Java
  • Purpose: Control flow structures determine how programs make decisions about actions and the number of repetitions of tasks.

  • Types of Control Flow Structures:

    • Loops: for, while, do-while

    • Decision-making: if-else, switch-case

    • Jumping Statements: break, continue, return

Loops in Java

  • Includes:

    • Useful in automating repetitive tasks. Example:

    • Generating random numbers using a for loop:
      java for (int i = 1; i <= 5; i++) { System.out.println(Math.random()); }

Switch Statements
  • Functionality: Similar to an if-else-if construct;

    • Execution starts at the case matching the switch variable and continues until a break is reached.

    • Example:

  int place = 13;
  switch (place) {
      case 1: System.out.println("gold medal"); break;
      case 2: System.out.println("silver medal"); break;
      case 3: System.out.println("bronze medal"); break;
      case 4: System.out.println("no medal! go enjoy Rio"); break;
      default: System.out.println("not even participated..."); break;
  }