Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:58:31+00:00 2026-06-17T23:58:31+00:00

I have a generic implementation of a Stack. An object of this Stack has

  • 0

I have a generic implementation of a Stack. An object of this Stack has the name stak and is of the type Character.

I tried

if ( ( ((String)(stak.head).equals("{")) && (str.charAt(i)=='}') )  { /* do something */}

The code compiles correctly but it gives me following error on runtime:

Exception in thread "main" java.lang.ClassCastException: java.lang.Character can
not be cast to java.lang.String
        at One.main(One.java:36)

However, the following code works:

if ( ((Character)(stak.head) == '{') && (str.charAt(i)=='}') )  { /* do something */}

Can you please explain why doesn’t the character object get cast into String?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:58:32+00:00Added an answer on June 17, 2026 at 11:58 pm

    Simply because Character is not a String, doesn’t fall in the common hierarchy and hence doesn’t allow casting. You can try casting a List to an ArrayList because the compiler knows that they share a common inheritance tree. Also, if you have a generic implementation of a Stack, you really shouldn’t be relying on concrete types like this so I wonder what you’ve got in your code.

    Also, { getting converted to Character is not about casting but about auto-boxing/unboxing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to create generic stack in python? My stack implementation in python: class Node(object):
I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
I have the following code implementation of my generic singleton provider: public sealed class
I have generic type that looks like: public class GenericClass<T, U> where T :
Is there any name for the following DB table design: Basically we have generic
I have a Generic List object which hold below table as its own value.
I have a generic linked-list that holds data of type void* I am trying
This is probably my naivety showing through, but anyway... I have a generic interface
I have asimple WCF HTTP/SOAP web service, the service implementation looks something like this:
I have a component done in JSF 1.x, this component has a command button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.