IntelliJ Java IDE
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Operators" Watch "Operators" New topic
Author

Operators

Thomas Misik
Greenhorn

Joined: Sep 30, 2000
Posts: 16
I am fairly confident in most uses of all operators used in Java, but there are some that still cause a bit of confusion. They would be:
^ (XOR) on Binary or boolean;
? - for instance: (as seen in MindQ)
Q).If arr[] contains only positive integer values, what does this function do?
public int guessWhat( int arr[] )
{ int x= 0;
for( int i = 0; i < arr.length; i++ )
x = x < arr[i] ? arr[i] : x;
return x;
}
/* Will this return the highest positive integer value in this array? */
If anyone knows a good (and concise) source on these operators and there functions, could they please post a link up? It would be greatly appreciated.
Thanks,
Thomas
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Thomas,
(1) ^ (XOR) on binary or boolean
The XOR operation says that the operands MUST be different.
In other words,
1 XOR 0 produces 1 and
0 XOR 1 produces 1 but
1 XOR 1 produces 0 and
0 XOR 0 produces 0.
Here are the tables to use:
(bitwise XOR table)
Op1 ----- Op2 ----- Op1 XOR Op2
0 ----- 0 ----- 0
0 ----- 1 ----- 1
1 ----- 0 ----- 1
1 ----- 1 ----- 0
(boolean XOR table)
Op1 ----- Op2 ----- Op1 XOR Op2
false ----- false ----- false
false ----- true ----- true
true ----- false ----- true
true ----- true ----- false
(2) ?: (conditional operator)
The conditional operator is just a convenient way to code simple conditions (if/else) into a single statement.
The variable on the left side of the ? must be a boolean.
It is evaluated first.
If the result of this evaluation is true, the result of the entire expression is the value of the sub-expression to the left of the colon. If the result is false, the result of the entire expression is the value of the sub-expression to the right of the colon.
For example:

What does a equal?
x is true, so a = b;
If x was false, a = c;
a = x ? b : c is the same as

You are right about the MindQ question, guessWhat() will return the highest positive integer value in the array.
Hope this helps. If not, let me know.
Stephanie
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Thomas,
Your code does return the highest value in the array. I tested it with the following:

and the value '10' was returned.
The ternary or conditional operator (?:) is a short form 'if' construct. It takes the following form:

Basically,

Hope that helps.
------------------
Jane


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Thomas Misik
Greenhorn

Joined: Sep 30, 2000
Posts: 16
Stephanie & Jane,
Thanks a lot guys, it was much appreciated. All of the Java I have written has been using Ultraedit; I've written many if statements, but have not really had to worry about ?: . It seems a rather silly question now, but I definitely needed some clarification on it. Thanks again, and since I'm writing the exam at the end of this month, I'm sure I'll be asking some more questions soon
Thomas
 
IntelliJ Java IDE
 
subject: Operators
 
Threads others viewed
here is a question and i have a very similar code but i need to convert it
MindQ
using set and get methods
Very Interesting Question
exceptions and type casting confusion
IntelliJ Java IDE