Solved

Boolean vs boolean as method parameter

Posted on 2012-03-26
8
405 Views
Last Modified: 2012-03-26
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

0
Comment
Question by:allelopath
  • 8
8 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37768285
It will make no difference - if compiler accepts just. "True" or "false" it means it is doing autoboxing - so it would be fine; if it does not accept. Then you haveto use new Boolean(truie)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768333
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);

}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768349
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

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 47

Expert Comment

by:for_yan
ID: 37768355
I think it will work only after Java 5 or 6 - when they introduced autoboxing which deals with those types like Integer, Boolean , Double...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768366
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768387
The only difference would be if there is overloading
See an example
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768399
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

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37768415
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

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question