Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Problem understanding a java statement!

Thread view: 
Moham12345 - 06 Mar 2007 23:19 GMT
Please could somebody help me understand what this statement is doing,

k = (k1 > s0) ? k1 : s0;

what is this statement doing?

what will k be equal to if
k1 = 3 and s0 = 2 ?
Tom Hawtin - 06 Mar 2007 23:34 GMT
> k = (k1 > s0) ? k1 : s0;
>
> what is this statement doing?

if (k1 > s0) {
    k = k1;
} else {
    k = s0;
}

Or, assuming suitable types:

k = Math.max(k1, s0);

Tom Hawtin
Adam Maass - 07 Mar 2007 02:01 GMT
> Please could somebody help me understand what this statement is doing,
>
[quoted text clipped - 4 lines]
> what will k be equal to if
> k1 = 3 and s0 = 2 ?

The trick is in understanding the ternary operator,

? :

This takes three operands,

<op1> ? <op2> : <op3>

op1 must have a boolean type; op2 and op3 must have compatible types.

The value of the expression is op2 if op1 is true, op3 otherwise.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.