What is widending in Java

2.3 Operators and Expressions

Expressions in Java are anything that provides a return value:

  • Constants
  • variables
  • Methods
  • Operators

Java has

  • unary (single-digit, monadic) operators
  • binary (two-digit, dyadic) operators
  • a three-digit (ternary, tryadic) operator (the conditional operator "_? _: _")

Unary operators have a single operand. Examples are:

  • Sign operator: -a
  • Post increment: a ++
  • Negation:! A

Binary operators have two operands. Examples are:

  • Addition: a + b
  • logical comparison: a == b

Arithmetic operators

The arithmetic operators can be applied to the following types

  • byte
  • short
  • int
  • long
  • float
  • double
operatorexamplesemantics
+a + bAddition: sum of a and b
-a - bSubtraction: difference at a and b
*a * bMultiplication: product of a and b
/a / bDivision: quotient of a and b
%a% bModulo: remainder of an integer division of a by b

The division of integers always gives integer results!

Java works without a detection of the overflow of the value ranges. The developer must take the appropriate precautionary measures himself.

There are also single-digit (unary) arithmetic operators

operatorexamplesemantics
++ aThe value of a is retained (idempotent operation)
--aThe value of a is negated
++

a ++

++ a

Post increment: The expression retains its original value. The value of a has been increased by 1

Pre-increment: The value of a is increased by 1 and the expression receives the increased value of a

--

x--

--x

Post decrement: The expression retains its original value. The value of a was decreased by 1

Pre-decrement: The value of a is decreased by 1 and the expression receives the decreased value of a

The following three statements do the same thing:

a = a + 1; a ++; ++ a;

However, for increments with simultaneous assignment, different values ​​result for the assigned value

version 1Value aValue bVariant 2Value aValue b

a = 10; b = 4;

b = a ++;

10

11

4

10 (!)

a = 10; b = 4;

b = ++ a;

10

11

4

11 (!)

example

Source codeConsole output
package s1.block2.cript; public class PrePostFixTest {public static void main (String [] args) {int x = 10; int y = 100; System.out.println ("x =" + x + "; y =" + y); x ++; System.out.println ("x ++ results in" + x); ++ x; System.out.println ("++ x results in" + x); System.out.println ("Set x to 0"); x = 0; System.out.println ("x =" + x + "; y =" + y); y = x ++; System.out.println ("y = x ++ (Postfix)"); System.out.println ("x =" + x + "; y =" + y); y = ++ x; System.out.println ("y = ++ x (Prefix)"); System.out.println ("x =" + x + "; y =" + y); } . . . . . x = 10; y = 100. x ++ results in 11. ++ x results in 12 Set x to 0. x = 0; y = 100. y = x ++ (postfix) x = 1; y = 0. y = ++ x (prefix) x = 2; y = 2.

Integer arithmetic

  • All operations on integers result in integers again. This is not necessarily expected from the division!
  • Attempts to divide by 0 (zero) will throw an ArithmeticException exception.

Floating point arithmetic

In the case of arithmetic with floating point numbers, in contrast to the integers, overflows are recognized. The floating point numbers have a number of constants:

constantsemantics
POSITIVE_INFINITYPositive infinite
NEGATIVE_INFINITYNegative infinite
MAX_VALUELargest representable value
MIN_VALUESmallest representable value
NaN"Not a number" This value is not equal to all other values ​​in the value range

Comparison operators

Equality or inequality refers to the value of the variables x and y

operatorexampleSemantics (meaning)
==x == yis x equal to y?
!=x! = yis x not equal to y?
<x is x smaller than y?
<=x <= yis x less than or equal to y?
>x> yis x greater than y?
>=x> = yis x greater than or equal to y?

Logical operators

The logical operators act on the type Boolean which only knows the value true or false.

operatorexampleSemantics (meaning)
!! anegation
&a & bAnd
|a | bOr (inclusive)
^a ^ bEither ... or
&&a && bconditionally evaluating and
||a || bconditionally evaluating or

Conditional operator

The three-digit (ternary) condition operator (conditional operator) allows an assignment to be made dependent on the result of a condition. It has the form:

<ausdruck1> ? <ausdruck2> : <ausdruck3>

expression1 must result in a boolean value. Becomes expression1 true so will expression2 assigned to the corresponding variable. Becomes expression1 untrue, so it will be expression3 assigned

This can be used to formulate assignments like the following

int maximum; int x = 1; int y = 2; maximum = (x> y)? x: y;

The result is 2 because y (= 2) is greater than x (= 1).

Conditionally evaluating logical && and || Operators

The conditional evaluating operators only evaluate terms until the end result is fixed. This makes them look efficient.

For example:

boolean a = ((1 <3) || (4> 5));

becomes the term (4>5) no longer evaluated. There (1<3) is true, the end result is already clear.

In addition to their speed advantage, the conditionally evaluating logical operators are also used with pleasure in order to avoid potential errors and exceptions.

An example of this is:

if ((a> 0) && (Math.sqrt (a)> 2))

The root is only evaluated if a is greater than zero.

Attention: Due to the conditional evaluation, different results can arise if a value is changed in an expression at the same time!

Example:

conditional "or" operatorsimple "or" operator
Source code public static void t1 () {int a = 3; int b = 5; if ((a> 1) || (a System.out.println ("Hello");
   }
System.out.println ("b =" + b);
}
public static void t2 () {int a = 3; int b = 5; if ((a> 1) | (a

example

package s1.block2.cript; public class ShiftingBits {public static void main (String [] args) {int x = 4; int result; int shift = 1; result = x << shift; System.out.println ("x =" + x + "; shift =" + shift + "result =" + result); result = x >> shift; System.out.println ("x =" + x + "; shift =" + shift + "result =" + result); result = result >> shift; System.out.println ("x =" + x + "; shift =" + shift + "result =" + result); result = result >> shift; System.out.println ("x =" + x + "; shift =" + shift + "result =" + result); result = result >> shift; System.out.println ("x =" + x + "; shift =" + shift + "result =" + result); }}

Result

x = 4; shift = 1 result = 8
x = 4; shift = 1 result = 2
x = 4; shift = 1 result = 1
x = 4; shift = 1 result = 0
x = 4; shift = 1 result = 0

The internal representation of the values ​​used:

Decimal valueBinary value
80 0000000 00000000 00000000 00001000
40 0000000 00000000 00000000 00000100
20 0000000 00000000 00000000 00000010
10 0000000 00000000 00000000 00000001
00 0000000 00000000 00000000 00000000

The equals sign = is used as an assignment operator in Java. The instruction

x = y + z;

is not to be understood as a mathematical equation, but as an assignment of the expression on the real side (y + z) to the variable x on the left side.

Assignments like:

x = y = 8;

are also possible. They have the same meaning as

y = 8; x = y;

For most binary operators there are compound operators with which you can assign something to a variable and at the same time use the old value:

Compound operatorcorresponds to
a + = ba = a + b
a - = ba = a - b
a * = ba = a * b
a / = ba = a / b
a% = ba = a% b
a & = ba = a & b
a | = ba = a | b
a ^ = ba = a ^ b
a << = ba = a << b
a >> = ba = a> b
a >>> = ba = a >>> b

For expressions with multiple operators, the following rules apply to the order of evaluation:

  1. Partial expressions in round brackets are evaluated first, as in mathematics
  2. Expressions with unary operators are then evaluated
  3. Finally, sub-expressions with multi-digit operators are evaluated

Unary operators all have the same priority

Order of execution of operators

The execution order of operators determines how a term is resolved.

Tip: It is good programming style to make terms clear. If in doubt, use brackets!

rankoperatordescription
1=, +=, -=, *= ...Assignment operator
2?:Conditional operator
3||Logical or
4&&Logical and
5|logical or bitwise or
6^logical or bitwise either-or
7&logical or bitwise and
8==, !=Comparison operators: equal, not equal
9<, <=, >, >=Comparison operators
10<<, >>, >>>Shift operators
11+, -Addition, subtraction, concatenation of strings
12*, /, %Multiplication, division, remainder
13++, --, +, -, ~, !unary (one-digit) operators

Evaluation of operators with the same priority

It can happen that an expression has several operators with the same priority. In these cases the order of evaluation is determined by the associativity of the operators.

Operator associativity
The associativity of operators is the order in which operands are linked by operators of the same priority

If an operator is left-associative, the left operand is evaluated first. The example shows the plus and minus operator. Both have the same priority. Here is the operand first a + b evaluated.

Some operators in Java are right-associative. An example of this is the assignment operator

Evaluation order of the operands of an operator

Evaluation order of the operands
In Java, the operands of an operator are evaluated strictly from left to right.

This rule is particularly important because methods and various operators can have side effects. This means that these operators change the value of variables while evaluating the overall expression. Examples are the increment and decrement operators.

j = i-- -i;

is a legal expression in Java. The value assigned to j is always 1;

The evaluation of this assignment takes place in the following steps:

  1. Evaluation of the subtrahend (and intermediate storage)
  2. Decrement of i
  3. Evaluation of the minuend and calculation of the difference
  4. Assignment of the difference to j

A sample program for testing:

package s1.block2.cript; public class PrePostInkrement {
public static void main (String [] args) {
int i = 4;
int j;

j = i-- -i;
System.out.println ("i:" + i + ", j =" + j);
}
}

Output:

i: 3, j = 1

The evaluation of the expression and the assignment j = i-- -i; takes place as follows:

ijj = i-- -i;comment
40j = 4 - i;Determination of the minuend of the subtraction
30j = 4 - i;Post decrement from i
30j = 4 -3;Determination of the subtrahend of the subtraction
30j = 1;Determination of the difference
31allocation

Java can perform all arithmetic operations even if the number types in the expression are different. The result of the calculation depends on the types of expression. It applies in the following order:

  1. Is one of the guys doubleso the result becomes the type double converted.
  2. If not, the result becomes a float Types converted if a type is a float Type is.
  3. If not, the result becomes a long Types converted if a type is a long Type is.
  4. If not, both operands become one first int Types converted.

Anonymous (not verified)

Sun, 12/16/2012 - 13:47

Permalink

Stefan Schneider

Sun, 12/16/2012 - 14:54

Permalink

In reply to Typpfehler by Anonymous (not verified)

Correctly.

Thanks. I corrected the mistake.

Anonymous (not verified)

Sun, 12/16/2012 - 13:53

Permalink

Multiple assignment

You write that "x = y = 8;" has the same meaning as "x = 8; y = 8;". But x is assigned the value of y and not directly the number 8, right? So "x = y = 8;" be equivalent to "y = 8; x = y" (since the expressions are evaluated from right to left). Or?

Stefan Schneider

Sun, 12/16/2012 - 15:05

Permalink

In reply to multiple assignment by Anonymous (not verified)

Right consideration

You are very, very likely right.

For the final clarification, one would have to look at the generated bytecode or come between the two assignments in the debugger.

The following Java code is a strong indication that you are right:

int x, y;
x = y = 8;
// (x = y) = 8; // You can't do that
x = (y = 8); // Something like that works. This bracket is probably redundant at this point. y is guaranteed to be pre-assigned here.
x = (y = 8) +1; // Something like that works too. This takes the principle to the extreme. x is now given the value 9.
System.out.println (x);
System.out.println (y);

Hane changed the text according to your suggestion.

Anonymous (not verified)

Wed, 01/16/2013 - 13:35

Permalink

Hello course WIBI12C

DHBW-Mannheim - Java lecture - here we are right now, right?
Who looks everything and finds the comment ???

___________ c $$$$$$$$$$$$$$$$$$$$$$$$$$ h,
________ c $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
____ d $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
____ $$$, d $$$$$$$$$$$$$$$$$$$$$$$$$ hc`? $$$$
___ $$$ u $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ h,? $$
___ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ b $$$
___ $$$$$ P $$? $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
___ $$$$$$$ `$$$$$$$$$$$$$$$$$$$$$$$$$ d $$$$$$
____ $$$$$$ c, nn,? $$$$$$$$$$$$$$$ ,, `$$$$$$$$
_____ $$ MMMMM` $$$$$$$$$$$$ F, MMMb` $$$$$$$$
______ $$$ MMTTM.?$$$$$$$$$$$,MMM $$$$$$$$
_______ `$$$$$, M ;;;` $$$$$$$$$$ ´M ,, `, $$$$$$
__________?$$$$, ____________?$$$$,( ) $$$$$$$$$$ (´ ),$$$
____________`$$$$.`-´ $$$$$$$$$$,`--´,$$$´
____________ $$$$$ hh $$$$$ ???? $$$ hc $$$$$$$ ´
___________ d $$$$$$$$$ `====== ´ $$$$$$$;
___________ $$$$$$$$$$$$ c ,,,, c $$$$$$$$$
____________? $$$$ P $$$$$$ ????? $$ ?? $$$$
______________________$$$$$
_____________________ 4 $$$$$ c
____________________,$$$$$$$
__________________ z $ ________________> z $ _________________ ________________ `$ _________________? L $$$$$$$$$$$: $
__________________? $$$$$$$$$$$ d´
___________________ `$$$$$$$$$$ F
____________________ `? $ c` ?? 3 $ F
____________________CCC_CCC
____________________ ,; CC_CC ;;
____________________ `CC__CCC
. ,,,,, bitten CCCCCCCCC__CCCCCCCCCCCCCCC
Cci CCCCCCCCCCC, CCCCCCC, CCCCC
CciCCCCCCCCCCCC ___ CCCCC´`´CCCCCCCCCC

Anonymous (not verified)

Tue, 01/22/2013 - 16:39

Permalink

invoice

public class PrePostInkrement {
public static void main (String [] args) {
int i = 4;
int j;

j = i-- -i;
System.out.println ("i:" + i + ", j =" + j);
}
}

Shouldn't 0 come out here for j? And i = 3?
The operator - (i.e. i--;) only takes place after the assignment, doesn't it?

Stefan Schneider

Tue, 01/22/2013 - 20:22

Permalink

In reply to Invoice by Anonymous (not verified)

Explanation

I built the exact sequence into the script.
The decrement takes place after the minuend has been determined.
This means that the decrement takes place after the minuende has been assigned. Not after assigning to j!
The assignment of the minuend can be understood as an assignment to a temporary variable.

Anonymous (not verified)

Sun, 11/23/2014 - 17:56

Permalink

Anonymous (not verified)

Fri, 10/11/2019 - 11:24

Permalink

Typing error

The unary arithmetic operators use "pre-decrement" instead of "pre-decrement".