943,972 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 13937
  • Java RSS
Dec 29th, 2004
0

Conditional Expression in JAVA

Expand Post »
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;
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
niamul is offline Offline
12 posts
since Dec 2004
Dec 31st, 2004
0

Re: Conditional Expression in JAVA

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
Java Syntax (Toggle Plain Text)
  1. return (a==2)?4:5;
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 3rd, 2005
0

Re: Conditional Expression in JAVA

Thank u .
I have used your suggested from and got the required result.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
niamul is offline Offline
12 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: DNS: TRACERT , Java Network Programming
Next Thread in Java Forum Timeline: Document.insertString(); not working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC