Java Syntax World – Examples & Explanation of Top Java Operators II

Okay, so fellas we won’t like to hang you in the middle. As we have discussed a few cool JavaScript syntaxes in our previous post that’s because it’s hard to compile all the java syntax in one post. Thus we will take forward our previous topic and throw light on the syntax which was not able to discuss in the previous article. All the java operators will be explained throughout with the suitable examples.

Equality and Relational Operators

As you might have guessed from the name that equality and relational operators are used to determine the relationship between two operators. They are even used to determine the equality in two operators like is the operand greater or less than equal or not equal to and so on. Depending on the equation of the relationship, it gives a result either true or false. This chart will show the java equality and relational operators better;

– The operator “==“ Description ‘equal to’ Example ‘5 == 3 is evaluated to false’
– The operator “!=“ Description ‘not equal to’ Example ‘5 != 3 is evaluated to true’
– Operator “>“ Description ‘greater than’ Example ‘5 > 3 is evaluated to true’
– The operator “<“ Description ‘less than’ Example ‘5 < 3 is evaluated to false’
– The operator “>=“ Description ‘greater than or equal to’ Example ‘5 >= 5 is evaluated to true’
– The operator “<=“ Description ‘less than or equal to’ Example ‘5 <= 5 is evaluated to true’

Equality and relational operators are frequently used in decision making and loops. Let us check out its simple example.

Example

class RelationalOperator {
public static void main(String[] args) {

int number1 = 5, number2 = 6;

if (number1 > number2)
{
System.out.println(“number1 is greater than number2.”);
}
else
{
System.out.println(“number2 is greater than number1.”);
}
}
}

Output

number2 is greater than number1.

In this example, we have used > operator to check the number1 if it’s greater then number2 or not. Since number2 is greater than number1, the expression number1 > number2 is evaluated to false.

Instanceof Operator

Apart from the relational operators, there is another type of comparison operator in the form of instanceof available. This operator compares an object to the specific field. You can better understand the instanceof operator with an example.

Example

class instanceofOperator {
public static void main(String[] args) {

String test = “asdf”;
boolean result;

result = test instanceof String;
System.out.println(result);
}
}

Output

When you run this program, then the output will be true. That’s because the test is the instance of String class.

Logical Operators

The logical operators || (conditional-OR) and && (conditional-AND) operates on boolean expressions. Here’s how they work.
– Operator ‘||’ Description ‘conditional-OR; true if either of the boolean expression is true’ Example ‘false || true is evaluated to true’
– Operator ‘&&’ Description ‘conditional-AND; true if all boolean expressions are true’ Example ‘false && true is evaluated to false’

Example

class LogicalOperator {
public static void main(String[] args) {

int number1 = 1, number2 = 2, number3 = 9;
boolean result;

// At least one expression needs to be true for result to be true
result = (number1 > number2) || (number3 > number1);
// result will be true because (number1 > number2) is true
System.out.println(result);

// All expression must be true from result to be true
result = (number1 > number2) && (number3 > number1);
// result will be false because (number3 > number1) is false
System.out.println(result);
}
}

Output

true
false

Well, these were just a few commonly used Java operators, if you have some java operators to share with us, then please do in the comment box. We hope, you enjoyed this series as much as we enjoyed it sharing with you all!

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href=""> <abbr> <acronym> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Send a Message