4

1.I am confused between these two,Do they have different functionality if so then How ?

StringBuffer(CharSequence chars) 

and

StringBuffer(String str)

2. What is basic Difference between String And CharSequence(Specially functionality) ?

fge
  • 119,121
  • 33
  • 254
  • 329
Abin Lakhanpal
  • 380
  • 2
  • 15
  • Good catch, I thought that all such things were removed - `String` replaced with `CharSequences`... – Betlista Nov 27 '14 at 17:31
  • http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/StringBuffer.java#StringBuffer.%3Cinit%3E%28java.lang.String%29 – Maksym Nov 27 '14 at 17:36
  • @Maksym: it's not describing, why there is this redundant constructor, the one with `String` do not need to be there... – Betlista Nov 27 '14 at 17:51
  • "the one with String do not need to be there." Why ? – Maksym Nov 27 '14 at 18:43
  • 1
    @Avin Very good question. Shows you correctly understand polymorphism and interfaces. – Basil Bourque Nov 28 '14 at 01:03
  • @Maksym: because while `String` implements `CharSequence` it matches with the first constructor... And the implementation is the same for both, so it's clear copy & paste. – Betlista Nov 28 '14 at 07:10

3 Answers3

5

CharSequence is an interface, so you cannot directly instantiate it. String is a concrete class that implements the CharSequence interface. StringBuffer also implements the CharSequence interface.

As for why StringBuffer has two constructors one that takes a String and one that takes a CharSequence, it is almost certainly because (per the Since line in the Javadoc) CharSequence was not added until Java v1.4 while StringBuffer (and String) were in Java 1.0

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
4

A CharSequence is an interface; it happens that String implements it.

This means that for instance, when you call .charAt() on a String, what is really called is the implementation of String for this method of CharSequence.

As you can see from the javadoc of CharSequence, not many classes in the JDK actually implement this interface.

As to why two constructors, StringBuffer dates back to Java 1.0 and CharSequence appears in 1.4 only; however, this is also the case that StringBuilder (which you should use, really, instead of StringBuffer) has two constructors (one with a CharSequence as an argument, another with a String as an argument), so there are probably optimizations implied when a String is passed as an argument. As to what such optimizations could be, well, it is a case of "Use The Source, Luke"(tm).

As an example of a CharSequence implementation which is not in the JDK, you can for example see one of my projects: largetext. Note that among other things, generating a Matcher from a Pattern uses a CharSequence and not a String as an argument; and since String implements CharSequence, well, passing a String as an argument works.

fge
  • 119,121
  • 33
  • 254
  • 329
  • 1
    Your Post Cleared scenario little bit, But Why do we need charsequence and string argument both ? Isn't one of them are enough to demonstrate behavior of StringBuffer Constructor. – Abin Lakhanpal Nov 27 '14 at 18:57
  • 1
    Sure, just the `CharSequence` constructor would suffice, but optimization is a perfectly good reason to have both. – Louis Wasserman Nov 27 '14 at 18:58
  • What sort of example are you looking for? I cant really tell what you're confused about. – Louis Wasserman Nov 27 '14 at 19:27
  • @LouisWasserman I got your Answer but i was wondering that in which case optimization really matter ?(that example) Which one is good to use String or CharSequence ? – Abin Lakhanpal Nov 27 '14 at 19:34
  • String has a fixed implementation with a char[] which can be copied more efficiently than an arbitrary CharSequence. – Louis Wasserman Nov 27 '14 at 19:36
  • 2
    No, optimization is *not* the reason we have both. Polymorphism means the optimized implementation would automatically be used under the covers. @Avin, you correctly point out that we should not need both. As `String` is a `CharSequence`, we could simply pass a String instance to the method expecting a CharSequence instance. The reason we have both is because the early Java teams did not think this through thoroughly. The CharSequence interface was added much later, in Java 4 (1.4). So the signature expecting a String already existed. – Basil Bourque Nov 28 '14 at 01:00
  • 1
    @BasilBourque OK, but then why does `StringBuilder`, which is from 1.5, also have both constructors? – fge Nov 28 '14 at 06:06
  • Ok,I got it and thank you for appreciation.@BasilBourque – Abin Lakhanpal Dec 04 '14 at 07:43
  • @fge Dunno – certainly not technically needed. In Java 5, the pre-existing `StringBuffer` gained a new constructor for `CharSequence` to go along with its original constructor taking a `String`. Perhaps the Java 5 team wanted the new `StringBuilder` class to have a similar profile, and included the constructor taking a `String` despite being redundant with its constructor taking a `CharSequence`. They may have felt some of the millions of Java programmers may be confused by the lack of explicit `String` constructor. Source code of `StringBuilder` & `AbstactStringBuilder` holds no explanation. – Basil Bourque Aug 22 '17 at 21:57
  • Those confused by this assortment of text-related classes and their twisted history might find helpful [my diagram](https://i.stack.imgur.com/PIFk9.png) in [my Answer](https://stackoverflow.com/a/26498954/642706) to the Question, [*Exact difference between CharSequence and String in java*](https://stackoverflow.com/q/11323962/642706). – Basil Bourque Aug 22 '17 at 21:59
0

public StringBuffer(String str) : Constructs a string buffer initialized to the contents of the specified string. The initial capacity of the string buffer is 16 plus the length of the string argument.

public StringBuffer(CharSequence seq) : Constructs a string buffer that contains the same characters as the specified CharSequence. The initial capacity of the string buffer is 16 plus the length of the CharSequence argument. If the length of the specified CharSequence is less than or equal to zero, then an empty buffer of capacity 16 is returned.

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/StringBuffer.java#StringBuffer.%3Cinit%3E%28java.lang.String%29

Kartic
  • 2,935
  • 5
  • 22
  • 43