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;

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

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

Thank u .
I have used your suggested from and got the required result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.