• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method inside if

 
Barry Andrews
Ranch Hand
Posts: 528
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic