This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ternary Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ternary Operator" Watch "Ternary Operator" New topic

Ternary Operator

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Can you tell me where I am going wrong with this ternary operator ?..After "?" mark, should I keep only Strings ??
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Exp1 is giving out "boolean" type and Exp2 gives out an integer value... So its a type mismatch

the expressions should be evaluated to give "String" type objects..

SCJP 6 [SCJP - Old is Gold]
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

we are declaring variable first2 as String so the ternary operator should return String type value but you are returning boolean if the condition is true and an integer if the condition is false
to clear this, either convert them to String or declare first2 as Object like this

hope this helps

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Put simply (in my eyes) Your ternary expression appears to be assigning a boolean or an integer to a String reference variable, thats all.

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
I agree. Here's the link:
subject: Ternary Operator
Similar Threads
why to use conditional operator " ? " ?
ternary operation
return null from primitive method
can any one explain this