+91 81900 98289

Java Operators

+ - * / and % do arithmetic. When both sides of / are ints, the result is an int. 7 / 2 is 3, not 3.5. Make one side a double when you want a fraction.

== compares primitive values. For text, use equals, not ==. && and || stop as soon as the answer is known. && runs the right side only when the left side is true.

Example

Integer division drops the fraction. The double division keeps it. % is the remainder, so 7 divided by 2 leaves 1.

public class Ops { public static void main(String[] args) { System.out.println(7 / 2); System.out.println(7 / 2.0); System.out.println(7 % 2); } }
Prefer < <= > >= for numbers. Do not write if (paid == true). Write if (paid). The boolean is already the condition.