File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ternary operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ternary operators" Watch "ternary operators" New topic
Author

ternary operators

Alpha Mak
Greenhorn

Joined: Sep 05, 2001
Posts: 10
> 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
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
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
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
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
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).]
 
wood burning stoves
 
subject: ternary operators