Java Operators — Java Technical Interview Questions & Answers
Java Operators
Java Operators
- Operators do work on values. + - * / % for maths. % is remainder — handy for even/odd. 5 / 2 is 2 in int maths, not 2.5. That surprise fails many viva questions.
- > < >= <= == != compare and give true or false. && || ! join conditions. = assigns. == compares. x += 1 is x = x + 1.
- condition ? a : b is a short if-else. The classic bug is writing = inside if when you meant ==. In an exam, show both traps: = vs ==, and 5/2 becoming 2.
- Let's take this on the board with one tiny Main class — no extra files. Java Operators — output: 1 then true. 10 % 3 is 1. 10 > 3 && 3 > 0 is true. Start from main, go line by line, and stop at each print. That output is the proof for Operators.
Technical Interview Tip: Show two traps: = vs ==, and 5/2 becoming 2 (int division).
Code Examples for Java Operators
Example
public class Main {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println(a % b);
System.out.println(a > b && b > 0);
}
}Java Operators — output: 1 then true. 10 % 3 is 1. 10 > 3 && 3 > 0 is true.
How to Explain Java Operators to an Interviewer
What is % ?
Remainder Even/odd check
What is 5 / 2 in int maths?
2 Not 2.5
= vs == ?
= assigns == compares