• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String parsing and Boolean Constructor

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Page 292 of Head First Java discuss static utility methods that are present in Wrapper classes. For example
int x = Integer.parseInt("2");

No problems with this example. It also gives the following example in case of Boolean:
boolean b = new Boolean("true").booleanValue();

Here, book mentions, the Boolean constructor takes and parses the String and primitive variable 'b' gets the primitive value by unwrapping it. I cannot make sense out of this code. To me, the code should be:
boolean b = (new Boolean("true")).booleanValue();

The above two pieces give similar results, but I can understand the second implementation which tells me that we are calling the unwrap method on an object. Whereas the code mentioned in the book makes little sense to me.

Any help will be appreciated. Thanks.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and (((new Boolean("true")))).booleanValue(); will also work...

new Boolean("true") returns an object and then you are calling booleanValue() on that object. the two parenthesis are redundant.

you can think of it this way:

you have:

new Boolean("true").booleanValue();

now replace [new Boolean("true")] with b you get b.booleanValue();

now define Boolean b = new Boolean("true");

Regards,
Majid
 
Imtiaz Nizami
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I got your point of new Boolean("true") being a single entity whether within parenthesis or not. I was not sure if the method is being called after the object creation.

Although, I still feel that the other syntax with extra parenthesis is more readable. It might be a preference based on working with C.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I think that the above code with parentheses is more readable too. In other words "me too".

I disagree with the book using "true" as a String. This doesn't work for languages other than English. Using the string constructor for numbers is OK because they are the same in other languages, but using the String "true" instead of the boolean literal true is bad practice in general.

Kaydell
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic