Conditional Expression in JAVA

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2004
Posts: 12
Reputation: niamul is an unknown quantity at this point 
Solved Threads: 1
niamul niamul is offline Offline
Newbie Poster

Conditional Expression in JAVA

 
0
  #1
Dec 29th, 2004
I am a new programmer in Java and now I am facing the following problem. If u please answer my queries it will be very much helpful to me.


Can I use the following code :

(a == 2) ? return 4 : return 5;

instead of the following code :

if(a == 2)
return 4;
else
return 5;
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Conditional Expression in JAVA

 
0
  #2
Dec 31st, 2004
no you can't, at least not in that form.
The conditional expression returns a value, it cannot terminate a method.

Instead you would use
  1. return (a==2)?4:5;
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: niamul is an unknown quantity at this point 
Solved Threads: 1
niamul niamul is offline Offline
Newbie Poster

Re: Conditional Expression in JAVA

 
0
  #3
Jan 3rd, 2005
Thank u .
I have used your suggested from and got the required result.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 9965 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC