Solved

Boolean vs boolean as method parameter

Posted on 2012-03-26
8
399 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now