This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Boolean type as String within generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean type as String within generics" Watch "Boolean type as String within generics" New topic
Author

Boolean type as String within generics

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have this:



When I do this:


then it prints out:


I use generics, so I allow, that instead of the boolean-value a String-value can be inserted.

Now, when I do that:



then I get a classCastException because of my value (which is a string).

So I have to convert my String to boolean with:



but it does not work. I even get the same classcastexception:



I cannot use the class.getName()... as I get the same failure as above, when I use this method.

So how can I enforce a convert from String to Boolean within generics, when I know, that the value is a String?
[ December 02, 2008: Message edited by: nimo frey ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3170
    
  10
Originally posted by nimo frey:

Now, when I do that:



then I get a classCastException because of my value (which is a string).


You shouldn't get a CCE here. get will return a Boolean which will be unboxed into a boolean. It works for me.

So I have to convert my String to boolean with:



but it does not work.[/QB]


This won't work because the get() is returning a Boolean and parseBoolean expects a String as an argument.

Originally posted by nimo frey:
So how can I enforce a convert from String to Boolean within generics, when I know, that the value is a String?


What are you trying to convert from String to Boolean ? There shouldn't be any need to do this.
[ December 02, 2008: Message edited by: Joanne Neal ]

Joanne
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1667
    
    7

What did you mean when you said:

Originally posted by nimo frey:
I use generics, so I allow, that instead of the boolean-value a String-value can be inserted.


The way I interpret this sentence is that you are able to insert both Boolean and String objets as values in the Map, indicating that somehow you're circumventing the compile-time type checking mechanism. You should be getting type safety warnings if that's the case.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay, it works now.

However, I have thought, that generics are not absolutely type-save.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Generics are type-safe, as long as you don't suppress or ignore any warnings from the compiler.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean type as String within generics
 
Similar Threads
Generics
Generics
generics
Generics
Generics