Give the meaning of each of the following java operators: a) ++ b) && c) !=

First Midterm Java Programming Language

THIS PAGE CONTAINS A SAMPLE quiz on material from Sun�son-line �The Java Tutuorial�. You should be able to answer these questions after studying that notes. Sample answers to all the quiz questions can be found at http://mail.baskent.edu.tr/~tkaracay/ders/java/

Question 1: Briefly explain what is meant by the syntax and the semantics of a programming language. Give an example to illustrate the difference between a syntax error and a semantics error.

Question 2: What does the computer do when it executes a variable declaration statement. Give an example.

Question 3: What is a type, as this term relates to programming?

Question 4: One of the primitive types in Java is boolean. What is the boolean type? Where are boolean values used? What are its possible values?

Question 5: Give the meaning of each of the following Java operators:

a) ++

b) &&

c) !=

Question 6: Explain what is meant by an assignment statement, and give an example. What are assignment statements used for?

Question 7: What is meant by precedence of operators?

Question 8: What is a literal?

Question 9: In Java, classes have two fundamentally different purposes. What are they?

Question 10: What is the main difference between a while loop and a do..while loop?

Now that you've learned how to declare and initialize variables, you probably want to know how to do something with them. Learning the operators of the Java programming language is a good place to start. Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.

As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence. The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

Operator Precedence
OperatorsPrecedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

In general-purpose programming, certain operators tend to appear more frequently than others; for example, the assignment operator "=" is far more common than the unsigned right shift operator ">>>". With that in mind, the following discussion focuses first on the operators that you're most likely to use on a regular basis, and ends focusing on those that are less common. Each discussion is accompanied by sample code that you can compile and run. Studying its output will help reinforce what you've just learned.

Java provides many types of operators which can be used according to the need. They are classified based on the functionality they provide. Some of the types are:

  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operator
  4. Relational Operators
  5. Logical Operators
  6. Ternary Operator
  7. Bitwise Operators
  8. Shift Operators
  9. instance of operator

Let’s take a look at them in detail. 

1. Arithmetic Operators: They are used to perform simple arithmetic operations on primitive data types. 

  • * : Multiplication
  • / : Division
  • % : Modulo
  • + : Addition
  • – : Subtraction

2. Unary Operators: Unary operators need only one operand. They are used to increment, decrement or negate a value. 

  • – : Unary minus, used for negating the values.
  • + : Unary plus indicates the positive value (numbers are positive without this, however). It performs an automatic conversion to int when the type of its operand is the byte, char, or short. This is called unary numeric promotion.
  • ++ : Increment operator, used for incrementing the value by 1. There are two varieties of increment operators. 
    • Post-Increment: Value is first used for computing the result and then incremented.
    • Pre-Increment: Value is incremented first, and then the result is computed.
  • — : Decrement operator, used for decrementing the value by 1. There are two varieties of decrement operators. 
    • Post-decrement: Value is first used for computing the result and then decremented.
    • Pre-Decrement: Value is decremented first, and then the result is computed.
  • ! : Logical not operator, used for inverting a boolean value.

3. Assignment Operator: ‘=’ Assignment operator is used to assigning a value to any variable. It has a right to left associativity, i.e. value given on the right-hand side of the operator is assigned to the variable on the left, and therefore right-hand side value must be declared before using it or should be a constant. 

The general format of the assignment operator is:

variable = value;

In many cases, the assignment operator can be combined with other operators to build a shorter version of the statement called a Compound Statement. For example, instead of a = a+5, we can write a += 5. 

  • +=, for adding left operand with right operand and then assigning it to the variable on the left.
  • -=, for subtracting right operand from left operand and then assigning it to the variable on the left.
  • *=, for multiplying left operand with right operand and then assigning it to the variable on the left.
  • /=, for dividing left operand by right operand and then assigning it to the variable on the left.
  • %=, for assigning modulo of left operand by right operand and then assigning it to the variable on the left.

4. Relational Operators: These operators are used to check for relations like equality, greater than, and less than. They return boolean results after the comparison and are extensively used in looping statements as well as conditional if-else statements. The general format is, 

variable relation_operator value
  • Some of the relational operators are- 
    • ==, Equal to returns true if the left-hand side is equal to the right-hand side.
    • !=, Not Equal to returns true if the left-hand side is not equal to the right-hand side.
    • <, less than: returns true if the left-hand side is less than the right-hand side.
    • <=, less than or equal to returns true if the left-hand side is less than or equal to the right-hand side.
    • >, Greater than: returns true if the left-hand side is greater than the right-hand side.
    • >=, Greater than or equal to returns true if the left-hand side is greater than or equal to the right-hand side.

5. Logical Operators: These operators are used to perform “logical AND” and “logical OR” operations, i.e., a function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e., it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Java also has “Logical NOT”, which returns true when the condition is false and vice-versa

Conditional operators are:

  • &&, Logical AND: returns true when both conditions are true.
  • ||, Logical OR: returns true if at least one condition is true.
  • !, Logical NOT: returns true when a condition is false and vice-versa

6. Ternary operator: Ternary operator is a shorthand version of the if-else statement. It has three operands and hence the name ternary.

The general format is:

condition ? if true : if false

The above statement means that if the condition evaluates to true, then execute the statements after the ‘?’ else execute the statements after the ‘:’.  

Java

public class operators {

    public static void main(String[] args)

    {

        int a = 20, b = 10, c = 30, result;

        result

            = ((a > b) ? (a > c) ? a : c : (b > c) ? b : c);

        System.out.println("Max of three numbers = "

                           + result);

    }

}

Output

Max of three numbers = 30

7. Bitwise Operators: These operators are used to perform the manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing update and query operations of the Binary indexed trees. 

  • &, Bitwise AND operator: returns bit by bit AND of input values.
  • |, Bitwise OR operator: returns bit by bit OR of input values.
  • ^, Bitwise XOR operator: returns bit-by-bit XOR of input values.
  • ~, Bitwise Complement Operator: This is a unary operator which returns the one’s complement representation of the input value, i.e., with all bits inverted.

8. Shift Operators: These operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two. General format- 

 number shift_op number_of_places_to_shift;
  • <<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as multiplying the number with some power of two.
  • >>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of the initial number. Similar effect as dividing the number with some power of two.
  • >>>, Unsigned Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit is set to 0.

9. instanceof operator: The instance of the operator is used for type checking. It can be used to test if an object is an instance of a class, a subclass, or an interface. General format- 

object instance of class/subclass/interface

Java

class operators {

    public static void main(String[] args)

    {

        Person obj1 = new Person();

        Person obj2 = new Boy();

        System.out.println("obj1 instanceof Person: "

                           + (obj1 instanceof Person));

        System.out.println("obj1 instanceof Boy: "

                           + (obj1 instanceof Boy));

        System.out.println("obj1 instanceof MyInterface: "

                           + (obj1 instanceof MyInterface));

        System.out.println("obj2 instanceof Person: "

                           + (obj2 instanceof Person));

        System.out.println("obj2 instanceof Boy: "

                           + (obj2 instanceof Boy));

        System.out.println("obj2 instanceof MyInterface: "

                           + (obj2 instanceof MyInterface));

    }

}

class Person {

}

class Boy extends Person implements MyInterface {

}

interface MyInterface {

}

Output

obj1 instanceof Person: true
obj1 instanceof Boy: false
obj1 instanceof MyInterface: false
obj2 instanceof Person: true
obj2 instanceof Boy: true
obj2 instanceof MyInterface: true

Precedence and Associativity of Operators

Precedence and associative rules are used when dealing with hybrid equations involving more than one type of operator. In such cases, these rules determine which part of the equation to consider first, as there can be many different valuations for the same equation. The below table depicts the precedence of operators in decreasing order as magnitude, with the top representing the highest precedence and the bottom showing the lowest precedence.

Give the meaning of each of the following java operators: a) ++ b) && c) !=

Interesting Questions about Operators 

1. Precedence and Associativity: There is often confusion when it comes to hybrid equations which are equations having multiple operators. The problem is which part to solve first. There is a golden rule to follow in these situations. If the operators have different precedence, solve the higher precedence first. If they have the same precedence, solve according to associativity, that is, either from right to left or from left to right. The explanation of the below program is well written in comments within the program itself.

Java

public class operators {

    public static void main(String[] args)

    {

        int a = 20, b = 10, c = 0, d = 20, e = 40, f = 30;

        System.out.println("a+b/d = " + (a + b / d));

        System.out.println("a+b*d-e/f = "

                           + (a + b * d - e / f));

    }

}

Output

a+b/d = 20
a+b*d-e/f = 219

2. Be a Compiler: Compiler in our systems uses a lex tool to match the greatest match when generating tokens. This creates a bit of a problem if overlooked. For example, consider the statement a=b+++c; too many of the readers might seem to create a compiler error. But this statement is absolutely correct as the token created by lex are a, =, b, ++, +, c. Therefore, this statement has a similar effect of first assigning b+c to a and then incrementing b. Similarly, a=b+++++c; would generate an error as tokens generated are a, =, b, ++, ++, +, c. which is actually an error as there is no operand after the second unary operand.

Java

public class operators {

    public static void main(String[] args)

    {

        int a = 20, b = 10, c = 0;

        a = b++ + c;

        System.out.println("Value of a(b+c), "

                           + " b(b+1), c = " + a + ", " + b

                           + ", " + c);

    }

}

Output

Value of a(b+c),  b(b+1), c = 10, 11, 0

3. Using + over (): When using + operator inside system.out.println() make sure to do addition using parenthesis. If we write something before doing addition, then string addition takes place, that is, associativity of addition is left to right, and hence integers are added to a string first producing a string, and string objects concatenate when using +. Therefore it can create unwanted results.

Java

public class operators {

    public static void main(String[] args)

    {

        int x = 5, y = 8;

        System.out.println("Concatenation (x+y)= " + x + y);

        System.out.println("Addition (x+y) = " + (x + y));

    }

}

Output

Concatenation (x+y)= 58
Addition (x+y) = 13

This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect or if you want to share more information about the topic discussed above.


What is A & B in Java?

Different bitwise operators available in Java are as follows: & (bitwise and): Bitwise & operator performs binary AND operation bit by bit on the operands. a&b = 0001 which is 1. | (bitwise or): Bitwise | operator performs binary OR operation bit by bit on the operands. a|b = 1111 which is 15.

What are the 4 operators in Java?

Java divides the operators into the following groups:.
Arithmetic operators..
Assignment operators..
Comparison operators..
Logical operators..
Bitwise operators..

What is the meaning of operators in Java?

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. As we explore the operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence.

What are the 6 Java operators?

Java Operators.
Arithmetic Operators..
Assignment Operators..
Relational Operators..
Logical Operators..
Unary Operators..
Bitwise Operators..