Page 1 of 1

About Question enthuware.ocajp.i.v7.2.991 :

Posted: Sat Jun 15, 2013 2:46 pm
by nickeasyuptech
For this question -

What can be the return type of method getSwitch so that this program compiles and runs without any problems?

Code: Select all

public class TestClass{
   public static XXX getSwitch(int x){
      return x - 20/x + x*x;
   }
   public static void main(String args[]){
       switch( getSwitch(10) ){
          case 1 :
          case 2 :
          case 3 :
          default : break;
       }
   }
}
"The return type cannot be byte, short, or char because the expression x - 20/x + x*x; returns an int."

How do we know that it returns an int? (Thanks in advance for your help!)

Re: About Question enthuware.ocajp.i.v7.2.991 :

Posted: Sat Jun 15, 2013 4:38 pm
by admin

Re: About Question enthuware.ocajp.i.v7.2.991 :

Posted: Sat Jun 15, 2013 5:20 pm
by nickeasyuptech
Thank you for the quick response!

Re: About Question enthuware.ocajp.i.v7.2.991 :

Posted: Mon Jul 22, 2019 11:42 am
by javabean68
Hi,

the expression

Code: Select all

x - 20/x + x*x
can actually return a double, 20/x isn't always int...

Thank you for your help,
Bye
Fabio

Re: About Question enthuware.ocajp.i.v7.2.991 :

Posted: Mon Jul 22, 2019 9:49 pm
by admin
Not always, but in the code given in this question, 20/x will be an int.