Skip to content

Interesting

Interesting Facts

num != 0 vs num > 0

  • num != 0: checks if the number is not equal to zero evaluate to true for both positive and negative numbers.
  • num > 0: checks if the number is greater than zero only positive numbers.

Arithmetic Operators

  • Modulo for Even/Odd Checks: (number % 2 == 0) - to find even number and else will be odd.
  • Swapping Without a Temporary Variable:
    1
    2
    3
    4
    5
      int a = 5;
      int b = 10;
      a = a + b; // a now becomes 15
      b = a - b; // b becomes 5
      a = a - b; // a becomes 10
    
  • index++: if index is zero and if you print this it will be 0 and increment after this statment / line.

  • Negation with Logical Operators: if (!(isWeekend || isHoliday)) { - check neither weekend nor holiday.

In Logical Operators

  • Short-Circuit Evaluation: boolean condition2 = (condition1 && someMethod()); - someMethod will not called when condition1 is false.
  • Combining Conditions: (age > 18 && hasLicense) - you can easily combine conditions.
  • Negation with Logical Operators: if (!(isWeekend || isHoliday)) { - check neither weekend nor holiday.