> can we call methods in TERNARY OPERATORS ; > e.g, x==y ? method1() :method2(); > ps explain with a example or two. Such a powerful method such a little explaination. > thanking you in advance > Alpha.
Paul Stevens
Ranch Hand
Joined: May 17, 2001
Posts: 2823
posted
0
The short answer is yes. You didn't ask for a good example public class Temp { public static void main(String[] args) { Temp t = new Temp(); String s = 1 > 2 ? t.method1() : t.method2(); System.out.println(s); } public String method1() { return "method1"; } public String method2() { return "method2"; } }
Alpha Mak
Greenhorn
Joined: Sep 05, 2001
Posts: 10
posted
0
Thanks Paul for you reply but ques is what if my method returns any other data type than string???
Originally posted by Paul Stevens: The short answer is yes. You didn't ask for a good example public class Temp { public static void main(String[] args) { Temp t = new Temp(); String s = 1 > 2 ? t.method1() : t.method2(); System.out.println(s); } public String method1() { return "method1"; } public String method2() { return "method2"; } }
Jennifer Warren
Ranch Hand
Joined: Aug 24, 2001
Posts: 53
posted
0
let me add to the question that if we can call any methods which do not return any thing. thanks. Jennifer
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
to Jennifer You can not add these kind of methods because in ? A : B ; A an B must evalute to something
SCJP2. Please Indent your code using UBB Code
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Jose is right... Try compiling this piece of code. I have modified Alpha Mak's version.
Cheers! Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 06, 2001).]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.