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

  • Home
  • SEARCH
  • 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 8186189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:06:36+00:00 2026-06-07T02:06:36+00:00

I read this previous post . Can any one say what the exact difference

  • 0

I read this previous post. Can any one say what the exact difference between CharSequence and String is, other than the fact that String implements CharSequence and that String is a sequence of character? For example:

CharSequence obj = "hello";
String str = "hello";
System.out.println("output is : " + obj + "  " + str);

What happens when “hello” is assigned to obj and again to str ?

  • 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-07T02:06:37+00:00Added an answer on June 7, 2026 at 2:06 am

    General differences

    There are several classes which implement the CharSequence interface besides String. Among these are

    • StringBuilder for variable-length character sequences which can be modified
    • CharBuffer for fixed-length low-level character sequences which can be modified

    Any method which accepts a CharSequence can operate on all of these equally well. Any method which only accepts a String will require conversion. So using CharSequence as an argument type in all the places where you don’t care about the internals is prudent. However you should use String as a return type if you actually return a String, because that avoids possible conversions of returned values if the calling method actually does require a String.

    Also note that maps should use String as key type, not CharSequence, as map keys must not change. In other words, sometimes the immutable nature of String is essential.

    Specific code snippet

    As for the code you pasted: simply compile that, and have a look at the JVM bytecode using javap -v. There you will notice that both obj and str are references to the same constant object. As a String is immutable, this kind of sharing is all right.

    The + operator of String is compiled as invocations of various StringBuilder.append calls. So it is equivalent to

    System.out.println(
      (new StringBuilder())
      .append("output is : ")
      .append((Object)obj)
      .append(" ")
      .append(str)
      .toString()
    )
    

    I must confess I’m a bit surprised that my compiler javac 1.6.0_33 compiles the + obj using StringBuilder.append(Object) instead of StringBuilder.append(CharSequence). The former probably involves a call to the toString() method of the object, whereas the latter should be possible in a more efficient way. On the other hand, String.toString() simply returns the String itself, so there is little penalty there. So StringBuilder.append(String) might be more efficient by about one method invocation.

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

Sidebar

Related Questions

This question is related to the previous post. How to save file and read
Ok so after having re-read over my previous post asking for help on this
Having read this question Immutable or not immutable? and reading answers to my previous
count_record=file.readline() count_prev=count_record[:-1] , i used this to read the previous line and it worked
those of you who read my previous questions may already know this: I'm currently
I read this post where the author advices to store session files in a
This is related to my previous post Problem with downloading multiple files using AsyncTask
Solved using this post: Can you convert an ASP.NET MVC Application to a Web
I've made a previous post that tried to use a textbox. From this I
This post is related to my Previous Post . There I was creating the

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.