GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method inside if 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 "method inside if" Watch "method inside if" New topic
Author

method inside if

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

I ran across something I have never seen before in the new Java 2 Programmer Certification test. There was something like:
if (amethod())
//do something
or some such. This may not have been the exact format (Hard to remember).......... Anyway, is this possible to do? Can someone give some code, if this is possible? Many thanks!!!
Ray Hsieh
Ranch Hand

Joined: Sep 20, 2000
Posts: 59
Hi Barry,
Maybe it's a simple question to find out whether you know that you can't treat if in Java the way you do in C++? So that will only work of amethod() returns a boolean?
Just a guess.
Originally posted by Barry Andrews:
I ran across something I have never seen before in the new Java 2 Programmer Certification test. There was something like:
if (amethod())
//do something
or some such. This may not have been the exact format (Hard to remember).......... Anyway, is this possible to do? Can someone give some code, if this is possible? Many thanks!!!

Rong Chen
Ranch Hand

Joined: Aug 07, 2000
Posts: 30
>if(amethod())
the return type of amethod() other than boolean must result in compiler error... think so.
sample code like this:
boolean compare(int a, int b){
if(a >= b)
return true;
else
return false;
}
// then it will compile like this
if(compare(3, 2))
System.out.println("a >= b");
else
System.out.println("a < b");
actually, there are lot of methods return boolean value in java API, check them out...
rong chen
[This message has been edited by Rong Chen (edited October 06, 2000).]
[This message has been edited by Rong Chen (edited October 06, 2000).]
rahul bajaj
Greenhorn

Joined: Sep 28, 2000
Posts: 3
Hey check this out.works perfectly.as far as the return type of that method is boolean we can do that.

public class As{
int i = 10;
int j;
boolean b;
public static void main(String argv[]){
char z= 'c';
if(amethod()){
System.out.println(z);
}
}
public static boolean amethod()
{
return true;
}
}
ra.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Rong is correct. The condition of an if statement must be a boolean value, so if there is a method in an if statement, the return type must be boolean. Unlike some other programming languages where 0 = false and any other number = true, Java doesn't exactly follow this. It is true that with bitwise calucations 0 = false and 1 = true, but in Java you can not implicitly or explicitly convert an int or any primative type to boolean or vice versa. So if you can't convert it, then the method must have a return type of boolean.

[This message has been edited by bill bozeman (edited October 06, 2000).]
 
GeeCON Prague 2014
 
subject: method inside if