LearnFrenzy
Sign in Take a free test

Java Programming · Topic 2 of 28

Operators and Expressions

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Coding Techniques

Operators and Expressions · Study material

Overflow-safe arithmetic

Widen before computing, use the Math.*Exact methods to detect overflow, and compute mid-points as lo + (hi - lo) / 2 (or with >>> 1) – the bug that once broke binary search in the JDK itself.

public class Main {
    public static void main(String[] args) {
        int big = 1_500_000_000;
        System.out.println(big + big);                          // wraps around
        System.out.println((long) big + big);                   // widen first
        try {
            System.out.println(Math.addExact(big, big));
        } catch (ArithmeticException e) {
            System.out.println("overflow detected: " + e.getMessage());
        }
        // Average without overflow
        int lo = 2_000_000_000, hi = 2_100_000_000;
        System.out.println((lo + hi) / 2 + " " + (lo + (hi - lo) / 2) + " " + ((lo + hi) >>> 1));
    }
}

Output

-1294967296
3000000000
overflow detected: integer overflow
-97483648 2050000000 2050000000

Writing readable conditions

// ✘ Hard to read: precedence and negations mixed together
if (!(age < 18 || !hasId) && score > 60 & attempts < 3) { … }

// ✔ Name the parts and use && / || only
boolean adult = age >= 18;
boolean eligible = adult && hasId && score > 60 && attempts < 3;
if (eligible) { … }

// ✔ Null-safe comparisons
if ("admin".equals(role)) { … }          // literal first: no NullPointerException if role is null
if (Objects.equals(a, b)) { … }         // works when either side is null

Handy bit manipulation idioms

TaskExpression
Is n even?(n & 1) == 0
Is n a power of two?n > 0 && (n & (n - 1)) == 0
Set bit kx |= 1 << k
Clear bit kx &= ~(1 << k)
Toggle bit kx ^= 1 << k
Test bit k(x >> k & 1) == 1
Count set bitsInteger.bitCount(x)
Multiply / divide by 2ⁿx << n / x >> n

Operator checklist

  • Use parentheses in any expression mixing operator families.
  • Keep ++ and -- as standalone statements.
  • Use && and || for conditions; &, |, ^ for bits.
  • Compare objects with equals() / Objects.equals().
  • Watch for overflow in sums, products and mid-points.
  • Prefer if/else to nested ternaries beyond two levels.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Java Programming

Java is one of the most used languages in the world – for enterprise software, Android apps, banking systems and backend services. This section covers core Java from syntax and data types to OOP, strings, arrays, exceptions, collections and multithreading, with clear explanations and practice questions for every topic.

Why it matters

  • Java is the main language in service-company placement tests and many product companies
  • OOP, String, collections and exception questions appear in nearly every Java interview
  • Strong core Java is the base for Spring Boot, Android and backend development
  • Java questions are common in university exams and certification tests

Who it is for

  • School students (Class 11–12 / ICSE computer applications) learning Java
  • College students (B.Tech, BCA, MCA) preparing for exams, labs and projects
  • Freshers preparing for placements, Java developer roles and technical interviews
  • Developers revising core Java before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Java developer interviews
  • Service company tests
  • University exams
  • Certification exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links