aspose file tools*
The moose likes Java in General and the fly likes Converting objects to char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting objects to char" Watch "Converting objects to char" New topic
Author

Converting objects to char

John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
I have created my own Queue class, along with a Linear Linked list class, and a node class. A Linear Linked list contains Nodes, with a single character as a value, and a link pointing to the next node. The Queue class just contains a Linear Linked list. Now, I store each character in a node by using the new Character() wrapper class. When I pull out values from my queue, i can't cast them as characters. I get the error message "inconvertable types". And I do not want to store anything but Objects inside my nodes, because it gives flexibility. Why can't I cast an object to char? I didn't seem to have a problem storing a char as an object. Please don't ask me to post code, because all it is, is char c = queue.pop(), where pop returns an object.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

Now, I store each character in a node by using the new Character() wrapper class. When I pull out values from my queue, i can't cast them as characters.


Well, you have two options. You can use autoboxing, but it will need some help. It doesn't know that it is a Character object, so you will need to cast it to a Character object and let autoboxing take care of the rest. Like so...



Or you can take care of the unboxing yourself -- after all, you boxed it with Character wrapper class yourself.



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John Lockheart
Ranch Hand

Joined: Oct 13, 2006
Posts: 115
thanks, I was using (char) as opposed to (Character). I haven't worked with simple primitives so long I guess I forgot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting objects to char