Solved

Boolean vs boolean as method parameter

Posted on 2012-03-26
8
404 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 40
eclipse apache tomcat admin console 52 97
hibernate example for saving data 19 40
CSV file parsing thru Java 13 31
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

777 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