This question may sound dumb but I honestly can't find an answer anywhere...what does the operators "^=", "|=" and "?" mean in Java? Thanks..
Asked
Active
Viewed 1,930 times
-6
-
4http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html - typing "java operators" into google provides that as the first link. Where were you looking? – Brian Roach Mar 13 '12 at 13:48
-
You should take a look at some tutorials for java. Here is an oracle page on operators: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html. – twain249 Mar 13 '12 at 13:51
5 Answers
1
The notation x ^= y; means the same as x = x ^ y;.
The ^ operator is bitwise exclusive OR, the | operator is bitwise OR (see Bitwise operation).
Just like x += y; means x = x + y;, etc.
For ?, see Ternary operator.
Jesper
- 202,709
- 46
- 318
- 350
1
x^=yis short forx = x^yx|=yis short forx = x | y?is the conditional operator. It's a shortcut for an if / else statement. It's use is highly controversal.
mrab
- 2,702
- 1
- 17
- 10
-
It's called a `ternary` operator and why would you ever say it's "controversial" ? – Brian Roach Mar 13 '12 at 13:53
-
@BrianRoach I perhaps wouldn't call it controversial but some people dislike it as it can make code less readable. – Jim Mar 13 '12 at 13:57
-
@Jim - And I'd ... politely disagree with you :-D I've never worked with or met anyone who finds them difficult to read or expressed a dislike in any language, never mind just java. I use them when they're a good fit. – Brian Roach Mar 13 '12 at 14:00
-
@BrianRoach Ternary and conditional are synonyms. Your and Jims comment are a good example for the controversal debate. I personally use it, when it fits (what has to be defined). – mrab Mar 13 '12 at 14:09
-
@BrianRoach [This](http://stackoverflow.com/questions/160218/to-ternary-or-not-to-ternary) is the first hit searching SO for "ternary" it includes discussion about the name and the use. – mrab Mar 13 '12 at 14:16
1
They're all covered by the Java tutorial.
The question mark is used in the ternary operator, which is a shorthand for if-then-else. For example,
int i = 1;
System.out.println ( i == 0 ? "No" : "Yes" );
If i is 0 then "No" will be printed, otherwise "Yes" will be printed.
The other two are used in bitwise assignments.
chooban
- 9,018
- 2
- 20
- 36
0
|= assignment operator -> bitwise inclusive OR
^= assignment operator -> bitwise exclusive OR
? logical operator as in booleanValue == true ? something() : somethingElse()
darijan
- 9,725
- 25
- 38
0
^is the XOR operator|is the bitwise OR operator?is the ternary operator.
The first two have the = operator after them because that's a shortcut writing. For example x |= y is the same as x = x | y.
More details about them can be found on thosands of websites on the Internet.
Radu Murzea
- 10,724
- 10
- 47
- 69