Why isn't String() constructor private? - java

Is using a String() constructor as against string literal beneficial in any scenario?
Using string literals enable reuse of existing objects, so why do we need the public constructor? Is there any real world use?
For eg., both the literals point to the same object.
String name1 = "name";//new String("name") creates a new object.
String name2 = "name";

One example where the constructor has a useful purpose: Strings created by String.substring() share the underlying char[] of the String they're created by. So if you have a String of length 10.000.000 (taking up 20MB of memory) and take its first 5 characters as substring then discard the original String, that substring will still keep the 20MB object from being eligible for garbage collection. Using the String constructor on it will avoid that, as it makes a copy of only the part of the underlying char array that's actually used by the String instance.
Of course, if you create and use a lot of substrings of the same String, especially if they overlap, then you'd very much want them to share the underlying char[], and using the constructor would be counterproductive.

Since string is immutable, operations like substring keep the original string that might be long. Using the constructor to create new string from the substring will create a new string and will allow dropping the old string (to GC). This way might free up needed memory.
Example:
String longS = "very long";
String shortS = new String(longS.substring(4));

Because sometimes you might want to create a copy and not just have a new reference to the same string.

All good answers here, but I think it's worth pointing out that the Java treats literals quite different than Strings constructed the traditional way.
This is a good Q/A about it.

Related

What is benefit of creating a duplicate String object in heap if the same string literal is created(or already present) in string constant pool area?

String str = new String(“my literal”);
In above statement ,two objects would be created ,one as String literal “my literal” in string constant pool (If it's not present in string pool) and other in heap area as object String(“my literal”)
Q-1 I know the benefit of putting the string literal in string pool area but I am not able to think of about the benefit of creating a duplicate object in heap?
Q2- As I read in some stack over flow link: If use of new String("my literal") is almost always bad because you will be creating 2 Strings one on String constants pool and another on heap with the same value ,then my question is why does Java creates duplicate object in heap? Why not java just ignore creating in heap?
There is almost no benefit to calling the String(String) constructor with a literal string. There used to be a significant benefit to calling the String(String) constructor with a different string expression.
The literal is already a String, and String objects are immutable. More generally, for any String expression passed to the String(String) constructor, the constructor is usually unnecessary, because the argument is already an immutable String.
From the String(String) constructor documentation:
public String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.
With older versions of Java (prior to 1.7.0_06), the String(String) constructor was more useful. A String created by String.substring() could refer to the larger original String, and prevent it from being garbage collected. The String(String) constructor cut the ties to the larger String.
You asked:
Q-1: ... The benefit of creating a duplicate object in heap?
Usually there is none. However, if you're using a String object in a context where object identity matters, you might want a different object. For example:
You're using String objects as keys within a IdentityHashMap, and want only your own String objects to match.
You want to synchronize on a string value provided by external code. You don't want any other code synchronizing on the same String object; it could lead to unexpected interference and deadlock. [This example provided by #biziclop in a comment below.]
Q-2: ... why does Java creates duplicate object in heap?
Because you explicitly asked it to with new String("my literal"). If you use the new operator, you get a new object.

Why do I need to redefine a String if I use the method replace?

Why do I need to redefine the variable theString if I use the method replace in this code :
String theString = "I w#nt to h#ve the regul#r \"A\"!";
theString = theString.replace("#", "a");
System.out.println(theString);
Why can't I do :
theString.replace("#", "a");
and that's it?
Strings are immutable -- you cannot change them once they have been created. There are exceptions of course, if you use reflective magic, but for the most part, they should be treated as invariants. So therefore the method replace(...) does not change the String, it can't, but rather it creates and returns a new String. So to be able to use that new String, you have to get access to its reference, and that can be done by assigning it to a String variable or even to the original String variable. This discussion gets to the heart of what is the difference between an object and a reference variable.
Because String objects are, by design, immutable, so you need to create a new String to contain your changes
The posted answers mention the technical reason (strings are immutable) but neglect to mention why it is that way. See this: Why are strings immutable in many programming languages?
Taken from this link
In Java, String is not implemented as character arrays as in other programming languages. Instead string is implemented as instance of String class.
Strings are constants/ immutable and their values cannot be changed after they are created. If any operations that results in the string being altered are performed a new instance of String object is created and returned. This approach is done for implementation efficiency by Java.
It is recommended to use StringBuffer or StringBuilder when many changes need to be performed on the String. StringBuffer is like a String but can be modified. StringBuffer is thread safe and all the methods are synchronized. StringBuilder is equivalent to StringBuffer and is for use by single thread. Since the methods are not synchronized it is faster.

Are Strings *really* immutable in Java?

Everyone knows that Java's String object is immutable which essentially means that if you take String object a and concatenate it with another String object, say b, a totally new String object is created instead of an inplace concatenation.
However, recently I have read this question on SO which tells that the concatenation operator + is replaced with StringBuilder instances by the compiler.
At this point, I get a bit confused since, as far as I know and think, the StringBuilder objects are mutable due to their internal structure. In this case, wouldn't this essentially make String objects in Java mutable ?
Not really.
The actual Strings are still immutable, but in compile time, the JVM can detect some situations where the creation of additional String objects can be replaced by a StringBuilder.
So if you declare a String a and concatenate with another String, your a object doesn't change, (since it's immutable), but the JVM optimizes this by replacing the concatenation with the instantiation of a StringBuilder, appending both Strings to the Builder, and finally assigning the resulting String.
Let's say you have:
String a = "banana";
String d = a + "123" + "xpto";
Before the JVM optimized this, you would essentially be creating a relatively large number of Strings for something so simple, namely:
String a
String "123"
String "xpto"
String a + "123"
String a+"123"+"xpto"
With the optimization of transforming concatenation into a StringBuilder, the JVM no longer needs to create the intermediate results of the concatenation, so only the individual Strings and the resulting one are needed.
This is done basically for performance reasons, but keep in mind that in certain situations, you'll pay a huge penalty for this if you aren't careful. For instance:
String a = "";
for(String str: listOfStrings){
a += str;
}
If you were doing something like this, in each iteration the JVM will be instantiating a new StringBuilder, and this will be extremely costly if listOfStrings has a lot of elements. In this case, you should use a StringBuilder explicitly and do appends inside the loop instead of concatenating.
Strings are immutable objects. Once you concatenate it with another String, it becomes a new object. So remember - you don't change the existing String, you just create a new one.
Strings really are immutable. The compiler may generate code involving StringBuilder, but that is just an optimization which does not change how the code behaves (apart from performance). If there was some case where you could observe mutation (e.g. by keeping a reference to one of the intermediate results), the compiler would have to optimize in a way that still gives you an immutable string for that intermediate reference.
So if StringBuilder is used under the covers, even if you can't directly see the difference, doesn't that still mean that mutability is involved? Well, yes, but if you get down to it all the RAM in your PC is mutable. The memory of immutable objects can be moved around by the garbage collector, which definitely involves mutation as well. In the end, the important thing to you as a programmer is that this mutation is hidden from you, and you get a big promise that your program will behave the way you expect, i.e. you'll never see mutation in an immutable object (except in cases of severe problems, e.g. faulty RAM).

No reverse method in String class in Java?

Why there is no reverse method in String class in Java? Instead, the reverse() method is provided in StringBuilder? Is there a reason for this? But String has split(), regionMatches(), etc., which are more complex than the reverse() method.
When they added these methods, why not add reverse()?
Since you have it in StringBuilder, there's no need for it in String, right? :-)
Seriously, when designing an API there's lots of things you could include. The interfaces are however intentionally kept small for simplicity and clarity. Google on "API design" and you'll find tons of pages agreeing on this.
Here's how you do it if you actually need it:
str = new StringBuilder(str).reverse().toString();
Theoretically, String could offer it and just return the correct result as a new String. It's just a design choice, when you get down to it, on the part of the Java base libraries.
If you want an historical reason, String are immutable in Java, that is you cannot change a given String if not creating another String.
While this is not bad "per se", initial versions of Java missed classes like StringBuilder. Instead, String itself contained (and still contains) a lot of methods to "alter" the String but since String is immutable, each of these methods actually creates and return a NEW String object.
This caused simple expressions like :
String s = "a" + anotherString.substr(10,5).trim().toLowerCase();
To actually create in ram something like 5 strings, 4 of which are absolutely useless, with obvious performance problems (despite after there has been some optimizations regarding underlying char[] arrays).
To solve this, Sun introduced StringBuilder and other classes that ARE NOT immutable. These classes freely modify a single char[] array, so that calling methods does not need to produce many intermediate String instances.
They added "reverse" quite lately, so they added it to StringBuilder instead of String, cause that's now the preferred way to manipulate strings.
As a side-note, in Scala you use the same java.lang.String class and you do get a reverse method (along with all kinds of other handy stuff). The way it does it is with implicit conversions, so that your String gets automatically converted into a class that does have a reverse method. It's really quite clever, and removes the need to bloat the base class with hundred of methods.
String is immutable, meaning it can't be changed.
When you reverse a String, what's happening is that each letter is switched on it's own, means it will always create the new object each times.
Let us see with example:
This means that for instance Hello becomes as below
elloH lloeH loleH olleH
and you end up with 4 new String objects on the heap.
So think if you have thousands latter of string or more then how much object will be created.... it will be really a very expensive. So too much memory will be occupied.
So because of this String class not having reverse() method.
Well I think it could be because it is an immutable class so if we had a reverse method it would actually create a new object.
reverse() acts on this, modifying the current object, and String objects are immutable - they can't be modified.
It's peculiarly efficient to do reverse() in situ - the size is known to be the same, so no allocation is necessary, there are half as many loop iterations as there would be in a copy, and, for large strings, memory locality is optimal. From looking at the code, one can see that a lot of care was taken to make it fast. I suspect the author(s) had a particular use case in mind that demanded high performance.

Java StringBuffer questions

class MyStringBuffer {
//TODO explain: why you would need these data members.
private char[] chars; //character storage.
private int length; //number of characters used
public String toString(){
//TODO
//Hint: just construct a new String from the ‘chars’ data member
//and return this new String – See API online for how create
//new String from char[]
This is only part of the code, I didn't want to post the whole thing. I just to focus on this and then then move on when I fully understand it.
1) What does he mean when he said construct a new String from the 'chars' data member? I'm confused as to what he wants me to do. Am I suppose to do something with char charAt(int index)? or something like this: StringBuffer sb = new StringBuffer("test");?
2) Does construct and create mean the same thing in Java?
What does he mean when he said construct a new String from the 'chars' data member? I'm confused as to what he wants me to do. Am I suppose to do something with char charAt(int index)? or something like this: StringBuffer sb = new StringBuffer("test");?
Do either of those things create a String? (Hint: no)
How do you create an instance of a particular class in Java? (Hint: review your notes on new. Hint 2: look at the javadocs for the java.lang.String class.).
Does construct and create mean the same thing in Java?
Java objects are created by a constructor, so it is reasonable to use "construct" and "create" interchangeably.
(Technically they don't mean exactly the same thing. An object is created by allocating its memory and calling a class constructor. So if you are being pedantic, constructing is part of the process of creating an object. But there's no need to be pedantic.)
1) The easy way to create a string from chars:
String result = new String(chars, 0, length);
Note, you probably can't just use the String(char[]) constructor, because the existence of the length member implies that some elements of chars may not be used. If you don't specify what elements to use, you could end up with extra chars (maybe NULs, maybe garbage) at the end of the string.
2) Not exactly. While the object is created (the memory for the object itself allocated) at the time the constructor's called, at that time it's just a blob of memory. The object is not fully constructed (read: any class invariants may not necessarily hold) until the constructor returns. This means the difference between the two only applies in the constructor, though, so it's common for people to use "create" and "construct" almost interchangeably.
You need an array of chars because, well, you have to store the characters somewhere. You could store them in a String but then you would have little need of a StringBuffer, whose raison d'etre is to be more efficient than a String (you generaly use the more efficient StringBuffer to construct a string, then use .toString() to get a real String object).
The length would be used to specify the length of the StringBuffer. You may thing you could just use the length of the character array but it's actually more efficient, when reducing the size of the StringBuffer, to not re-allocate a smaller array, especially if you may just end up needing more anyway.
The String constructor you would need for this scheme would be String (char[] value, int offset, int count) using an offset of zero and a count of length. This would allow you to use only the desired subset of the char array.
As he says, have a look at the API, where you will find a String constructor that takes an array of characters: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#String%28char[]%29 .

Categories