Expressions

Expressions in Java are very much like in other programming languages. You can use the operators that may be similar from languages such as C or C++. They are as follows:

  • Unary prefix and postfix increment operators ( -- and ++ before and after a variable)
  • Unary sign (+ and -) operators
  • Logical (!) and bitwise (~) negation
  • Multiplication (*), division (/), and modulo (%)
  • Addition and subtraction (+ and - again, but this time as binary operators)
  • Shift operators move the values bitwise, and there is left (<<) and right (>>) shift and unsigned right shift (>>>)
  • The comparing operators are <, >, <=, >=, ==, != and instanceof that result in boolean value
  • There are bitwise or (|), and (&), exclusive or (^) operators, and similarly logical or (||), and (&&) operators

When logical operators are evaluated, they are shortcut evaluated. It means the right-hand operand is evaluated only if the result cannot be identified from the result of the left operand.

The ternary operator is also similar to the one, like it is on C, selecting from one of the expressions based on some condition: condition ? expression 1 : expression 2. Usually, there is no problem with the ternary operator, but sometimes you have to be careful as there is a complex rule controlling the type conversions in case the two expressions are not of the same type. It's always better to have the two expressions be of the same type.

Finally, there is an assignment operator (=) that assigns the value of an expression to a variable. For each binary operator, there is an assignment version that combines = with a binary operator to perform an operation involving the right operand and assign the result to the left operand, which must be a variable. These are +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, and >>>=.

The operators have precedence and can be overridden by parentheses, as usual.

An important part of expressions is invoking methods. Static methods can be invoked by the name of the class and the name of the method. For example, to calculate the sine of 1.22, we can write the following line:

double z = Math.sin(1.22);

Here, Math is the class from the package java.lang. The method sin is invoked without using any instance of Math. This method is static, and it is not likely that we will ever need any other implementation of it than the one provided in the class Math.

Non-static methods can be invoked using an instance and the name of the method with a dot separating the two. For example, take the following code line as an example:

System.out.println("Hello World");

The preceding code uses an instance of the class PrintStream that is readily available through a static field in the class System. This variable is called out, and when we write our code, we have to reference it as System.out. The method println is defined in the class PrintStream and we invoke it on the object referenced by the variable out. This example also shows that static fields can also be referenced through the name of the class and the field separated by a dot. Similarly, when we need to reference a non-static field, we can do it through an instance of the class.

Static methods defined in the same class from where it is invoked or inherited can be invoked without the class name. Invoking a non-static method defined in the same class or being inherited can be invoked without an instance. In this case, the instance is the current object the execution is in. This object is also available through the this keyword. Similarly, when we use a field of the same class where our code is, we simply use the name. In case of a static field, the class we are in by default. In the case of a non-static field, the instance is the object referenced by the this keyword.

You can also import a static method into your code using the importstatic language feature, in which case you can invoke the method without the name of the class.

The arguments of the method calls are separated using commas. Methods and method argument passing is an important topic that we will mention in detail in a separate subsection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.142.194