Differences and use of Strings vs. Charsequence

Asked

Viewed 3,208 times

11

Many methods in Java expect parameters CharSequence, but the code runs normally when I pass a String without conversion, for example:

String mensagem = intent.getStringExtra(MainActivity.MENSAGEM);

TextView t = (TextView)findViewById(R.id.minha_text_view);

// O Método setText requer o parâmetro do tipo CharSequence, então:
t.setText((CharSequence) mensagem);

// Porem posso usar sem conversão
t.setText(mensagem);

What is the difference between types and why is there no error when the unexpected type is passed as parameter? Is there any downside in not converting the String for CharSequence?

2 answers

13


CharSequence is a Java interface representing a string of characters.

The class String implements this interface and lets you use polymorphism in methods they receive CharSequence as a parameter, that is, receive a specific implementation, but treat the parameter through a more generic type.

The class itself String uses parameters CharSequence in various methods, for example contains(). This allows interoperability with other text representation classes that also implement the interface CharSequence, such as the StringBuilder.

  • 1

    Excellent, I imagined something like, but I preferred to put doubt and learn the right way.

6

The reason for the existence of CharSequence is that it is often interesting to give more flexibility in the parameters to be accepted by a function. This avoids unnecessary - and often costly - conversions allowing more generic and efficient code.

For example, if your algorithm needs a ensemble of objects, why require that the entry is a ArrayList? Accepted Collection, after all ArrayList implements the interface Collection (and many other classes as well). Now if he needs a orderly list of elements, it is better to ask as parameter a List (still giving choice between ArrayList, LinkedList and others). And so on...

Java strings are immutable. So if you’re in the middle of a string-handling operation (using StringBuilder, CharBuffer or something else of the kind) and need to pass this string to another method, it would be inefficient to first convert into a String (which implies copying all its contents) and then passing as argument. What the method needs is an "ordered string", so the ideal is to accept any class that implements these requirements.

And about why Java accepts String in place of CharSequence, this is answered by @utluiz: why CharSequence is an interface, which String implements. Since [in inheritance] it is always possible to use a more specific type in place of a more generic type, a conversion is not necessary.

  • very good, your answer also added too much to the question.

  • Man, that’s what you said, at least in my conception it’s relative. In some cases it is important to generalize the implementation, but leave a more generic code, most of the time, makes the code less efficient, since the computer (or VM, in the case of Java) has to treat this generalization by specifying each element. But, in general, generalization pays its computational cost. (:

  • @Felipe.Agree. Taking the own List as an example, sort a ArrayList is much more efficient than ordering a LinkedList (so much so that Java converts it into an array before doing so). Implementation details, far from being irrelevant, can be "explored" to increase code efficiency. We only need to be careful not to "lose the forest to the trees": greater efficiency local it is worth little to commit to efficiency global. "Inefficient conversion -> efficient calculation -> inefficient conversion" can be worse than "inefficient calculation".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.