Tutorial
Menus

Java Beginners - Java For Beginners

Java Operators

Rating: 5.0/5 (1 vote cast)

Level   : Beginners
Author : Arunkumar S
Download Source : Not Avaliable

Java Operators,Overview of Java's Operators,

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

Overview of Java's Operators


Java provides a rich set of operators for manipulating program data. Most of these were taken directly from C/C++, but you must be careful because there are some significant differences to keep in mind. In this tutorial we will cover the most commonly used operators.

Java Operators
postfix [] . () expr++ expr--
unary ++expr --expr +expr -expr ! ~
creation/caste new (type)expr
multiplicative * / %
additive + -
shift << >> >>>
relational < <= > >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ?:
assignment = "op="
Operators are listed in precedence or derwith the highest precedence at the top of the table.

 

Evaluation Order

In Java, the order of evaluation of operands in an expression is fixed. All operands are evaluated from left to right. The order of execution of the operations may be completely different. For example:

  1. int [] a = {5, 5};
  2. int b = 1;
  3. a[b] = b = 10;
In this example, on line 3 the a[b] operand is evaluated to a[1] because at that moment b equaled 1. Next b is evaluated to 1 again. Finally, 10 is evaluated simply as the constant 10. After the operands are evaluated, the assignment operations take place. Assignment is a right to left operation, so b is assigned 10 and then a[1] is assigned the value of b, which is 10.

Assignment

Assignment operators set the value of a variable or expression to some new value. The simple = operator sets the lefthand operand to the value of the righthand operand. If the operands are object references, then a copy of the reference value will be assigned--not the object body. The assignment operator is evaluated from right to left, so a = b = c = 0; would assign 0 to c, then c to b then b to a.

Arithmetic

/ + - represent division, addition, and subtraction, as in standard math notation. * represents multiplication and % represents modulo. All of these are self-explanatory except the modulo operator. % is similar to division but returns the remainder of the division operation. For example, z = 5 % 2;, z would be 1. In other words, 5 / 2 is 2 with a remainder of 1.

One other note about the division operator: Make sure you always avoid division by 0 or else you will have an ArithmeticException thrown.

Comparison

The comparison operators return true or false boolean values.  These are most often used in control flow statements such as if(), while(), and for() to determine if certain lines of code should be executed.

  • Less than: <
  • Less than or equal to: <=
  • Greater than: >
  • Greater than or equal to: >=
  • Equal to: ==
  • Not equal to: !=
Do not get the equals and assignment operators confused. Use == to compare operands and use = to assign one operand to another. In the next Java Operators tutorial, we will cover the advanced operators including: instanceof, shift, unary, ternary, bitwise, short-circuit, and "op=".

1 | 

Discussion about this tutorial

  Start a new Discussion | Read All Discussion
Subject RepliesLast Post
Javaorigin.com contact@javaorigin.com