File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Boolean type as String within generics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 3011
    
    9
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: 1409

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: 32657
    
    4
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Boolean type as String within generics
 
Similar Threads
Generics
generics
Generics
Generics
Generics