Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Boolean vs boolean as method parameter

Posted on 2012-03-26
8
Medium Priority
?
423 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 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