Link to home
Start Free TrialLog in
Avatar of allelopath
allelopath

asked on

Boolean vs boolean as method parameter

For a method that has a Boolean parameter, as opposed to boolean, is it acceptable to use true or false?

For example, AbstractRenderer.setSeriesVisibleInLegend(Boolean visible):
renderer.setSeriesVisibleInLegend(false);

Open in new window

or would it make a difference in doing:
renderer.setSeriesVisibleInLegend(new Boolean(false));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, it works and compiler doe not complain so it isOK to use true and false in this case:

  public static void takeBoolean(Boolean b){
       
       
    }
   
    public static void main(String[] args) {
       
       
        takeBoolean(true);

}
You see how it works:

public class TestBoolean {
  public static void takeBoolean(Boolean b){

        System.out.println("Class of b:" + b.getClass());

         System.out.println("Vlaue  of b:" + b.booleanValue());


    }

    public static void main(String[] args) {


        takeBoolean(true);

}

}
        

Open in new window


Output:

Class of b:class java.lang.Boolean
Vlaue  of b:true

Open in new window

I think it will work only after Java 5 or 6 - when they introduced autoboxing which deals with those types like Integer, Boolean , Double...
The only difference would be if there is overloading
See an example
here we defined two methdos which differe that one tkaes boolean and anothe takse Boolean,
then in thios case it will take the method wchich uses Boolean:


public class testBooolean {

     public static void takeBoolean(boolean b){
                     System.out.println("I type here something else");
     }



    public static void takeBoolean(Boolean b){

        System.out.println("Class of b:" + b.getClass());

         System.out.println("Vlaue  of b:" + b.booleanValue());


    }

    public static void main(String[] args) {


        takeBoolean(new Boolean(true));


}
 
}       

Open in new window


Output:

Class of b:class java.lang.Boolean
Vlaue  of b:true

Open in new window

however in this case:
it wil use method which has argiment of boolean:
(unless you have tw exactly identical methods which only differe by Bollean vs boolens - then you do not care. This is just exotic example

public class TestBoolean{
  public static void takeBoolean(boolean b){
                     System.out.println("I type here something else");
     }



    public static void takeBoolean(Boolean b){

        System.out.println("Class of b:" + b.getClass());

         System.out.println("Vlaue  of b:" + b.booleanValue());


    }

    public static void main(String[] args) {


        takeBoolean(true);
        

}
}

Open in new window


Output

I type here something else

Open in new window