Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean or boolean what is the difference in term of use?

 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What is the difference between "Boolean" and "boolean" ? Seem to be both same even others(Float - float,Double - double, Character - char,...)
Boolean is predefined class so, is boolean reserved word used to represent Boolean class ? If so why we use boolean instead of Boolean.. I mean there is no
significant difference between length.. Like String why we don't use it's class name ?

Please help me to clear this.. Is there any difference ?
Thank you.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean is a primitive type whereas Boolean is wrapper class.Same applies for (int,Integer),(long,Long) etc.
Wrapper classes "wrap" the respective primitive data type into an object of that class.

They are used with collections, as primitive types are not allowed with collections.Also using wrapper classes give you access to many methods that you can call on that object.For eg. Character wrapper class have methods like:

isDigit() – to determine whether the character is digit.
isLower() – to determine whether the character is lower case alphabet.
is Letter() – to determine whether the character is an alphabet.

we cannot use the above methods if we use a primitive type as compared to a wrapper class.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranjith Suranga wrote:What is the difference between "Boolean" and "boolean"?

The same as the difference between Long and long, or Short and short: The first is a class, the second is a primitive.

Seem to be both same even others(Float - float,Double - double, Character - char,...)

No, they're very different; but they are automatically converted in certain situations.

Have a look at tutorials for a full explanation, because the rest of your post suggests that you haven't come across this yet.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 50268
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Boolean is an object, an instance of the class of that name. A boolean is a primitive, which keeps quiet about how many bits it uses, but has
When I started Java, you had to write Boolean bb = Boolean.valueOf(b); or similar. They introduced boxing in Java5, so you can write Boolean bb = true; or similar.
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
because the rest of your post suggests that you haven't come across this yet.


Ya, sir.. I am really new to the java... SO what to do...? these problems always come when studying.
Extremely Thank you.
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extremely Thank you for every one who gives me answers, opinions, advice, everything... They help me lot to develop as a beginner...
Thank you once again...
 
Campbell Ritchie
Sheriff
Pie
Posts: 50268
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Autoboxing is supposed to translate seamlessly between Boolean and boolean without you having to write any code. But here's a problem which can arise because of that:




So when autoboxing finds itself trying to convert a null reference to a boolean value, it will throw a NullPointerException. I don't remember whether the tutorial mentions this -- hopefully it does, but I haven't checked.

 
Campbell Ritchie
Sheriff
Pie
Posts: 50268
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . trying to convert a null reference . . . I don't remember whether the tutorial mentions this -- hopefully it does, but I haven't checked.

I have checked, and it doesn’t.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic