• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Operators

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Thomas Misik
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic