📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
In this chapter, we will learn about Equality, Relational, and Conditional Operators in Java programming language with examples.
1. The Equality and Relational Operators
class ComparisonDemo {
public static void main(String[] args) {
int value1 = 1;
int value2 = 2;
if (value1 == value2)
System.out.println("value1 == value2");
if (value1 != value2)
System.out.println("value1 != value2");
if (value1 > value2)
System.out.println("value1 > value2");
if (value1 < value2)
System.out.println("value1 < value2");
if (value1 <= value2)
System.out.println("value1 <= value2");
}
}
value1 != value2
value1 < value2
value1 <= value2
2. The Conditional Operators
&& Conditional-AND
|| Conditional-OR
Example:
class ConditionalDemo1 {
public static void main(String[] args) {
int value1 = 1;
int value2 = 2;
if ((value1 == 1) && (value2 == 2))
System.out.println("value1 is 1 AND value2 is 2");
if ((value1 == 1) || (value2 == 1))
System.out.println("value1 is 1 OR value2 is 1");
}
}
3. Ternary Operator
expression1 ? expression2 : expression3
package net.javaguides.corejava.operators.ternary;
public class Ternary {
public static void main(String args[]) {
int i, k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
i = -10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
}
}
Absolute value of 10 is 10
Absolute value of -10 is 10
4. The Type Comparison Operator instanceof
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: " +
(obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: " +
(obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: " +
(obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: " +
(obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: " +
(obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: " +
(obj2 instanceof MyInterface));
}
}
class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}
obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true
❮ Previous Chapter Next Chapter ❯
Comments
Post a Comment
Leave Comment