String and Final - java

What is difference between in the following statements
String name = "Tiger";
final String name ="Tiger";
Although the String class is final class, why do we need to create a String "CONSTANT" variable as final?

final in this context means that the variable name can only be assigned once. Assigning a different String object to it again results in a compile error.
I think the source of the confusion here is that the final keyword can be used in several different contexts:
final class: The class cannot be subclassed.
final method: The method cannot be overridden.
final variable: The variable can only be assigned once.
See the Wikipedia article on final in Java for examples on each case.

"final" means different things in the two cases.
The java.lang.String class is final. This means you can't inherit from it.
The variable "name" is final, meaning that you can't change it to point to a different instance of String. So a non-final String variable isn't a constant, because you could read it at two different times and get different values.
As it happens, Java string objects are also immutable. This means that you cannot modify the value which a particular String object represents. Compare this with an array - you can replace the first element of an array object with a different object, but you can't replace the first character of a String object with a different char. This is why String.replace() returns a new string - it can't modify the old one.
One reason that String is final is to prevent an instance of a subclass of String, which implements mutable behaviour, being passed in place of a String.
But whether you can modify a particular object, and whether you can assign a different object to a variable, are completely different concepts. One is a property of String objects, and the other is a property of String variables, which are references to String objects.

Remember that Java final keyword serves two purposes in this case:
it means the reference cannot be set to another String-- i.e. you cannot subsequently do "name = ...";
but crucially, it means that the reference is correctly published to other threads (see linked article for more details, or works such as Goetz et al, "Java Concurrency in Practice".

You are confusing immutable with final.
String, like Integer and Long, is an immutable class in that the internal data is protected from modification through encapsulation.
However, like Ayman said, final refers to the pointer to the string.

Have a look at The final word on the final keyword.
String name = "scott";
name = "tiger"; // OK
final String gender = "male";
gender = "female"; // won't compile you cannot reassign gender cause it's final

If a variable is marked as final then the value of that variable cannot be changed i.e final keyword when used with a variable makes it a constant. And if you try to change the value of that variable during the course of your program the compiler will give you an error.
NOTE :
If you mark variable of a reference type as final, that variable cannot refer to any other object. However, you can change the object's contents, because only the reference itself is final.
SOURCE : Final Keyword in Java

To deduce that String objects are Final by default is in itself a vague statement. The basics of Java dictate that if an instance variable is not pointing to a memory location it becomes eligible for Garbage collection. The same thing happens with the String objects. They are immutable but their references can be changed. To overcome this we can use "Final String s1 = "Final String" " the final keyword won't allow any assignment to s1 except at the time of First Declaration, making it truly immutable.
public class DemoStringF
{
String s1; //Declaring an Instance Variable to type String.
public static void main(String... args)
{
DemoStringF d = new DemoStringF ();
d.s1 = "Intializing s1 here"; //Initializing the s1
System.out.println("Value ref. by s1 is " +d.s1); //Displays the String
by which s1 is
initialized.
System.out.println("Value of s1 is " +d.s1.hashCode()); //Displays the
value of the s1.
d.s1 = d.s1.concat(" Adding String to s1"); //Changing the value ref. by
s1.
System.out.println("Value ref. by s1 after concat() is " +d.s1);
//Displays a new value of s1.
System.out.println("Value of s1 is " +d.s1.hashCode()); //Displays
the value of the s1.
}
}

Related

Pass-by-value (StringBuilder vs String) [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 7 years ago.
I do not understand why System.out.println(name) outputs Sam without being affected by the method's concat function, while System.out.println(names) outputs Sam4 as a result of the method's append method. Why is StringBuilder affected and not String? Normally, calling methods on a reference to an object affects the caller, so I do not understand why the String result remains unchanged. Thanks in advance
public static String speak(String name) {
name = name.concat("4");
return name;
}
public static StringBuilder test(StringBuilder names) {
names = names.append("4");
return names;
}
public static void main(String[] args) {
String name = "Sam";
speak(name);
System.out.println(name); //Sam
StringBuilder names = new StringBuilder("Sam");
test(names);
System.out.println(names); //Sam4
}
Because when you call speak(name);, inside speak when you do
name = name.concat("4");
it creates a new object because Strings are immutable. When you change the original string it creates a new object,I agree that you are returning it but you are not catching it.
So essentially what you are doing is :
name(new) = name(original) + '4'; // but you should notice that both the names are different objects.
try
String name = "Sam";
name = speak(name);
Of course now I think there is no need to explain why it's working with StringBuilder unless if you don't know that StringBuilder is mutable.
Looking at the Javadoc for String, one will read that
[...] String objects are immutable [...].
This means concat(String) does not change the String itself, but constructs a new String.
StringBuilders, on the other hand, are mutable. By calling append(CharSequence), the object itself is mutated.
Because String is immutable and hence String#concat does not modify the original String instance, it only returns a new String while the original is left unmodified, while StringBuilder is mutable and the change is reflected in the StringBuilder instance passed as parameter.
Okay, what is speak method doing?
First of all,
name.concat("4");
creates new object, which is equal to name, concatenated with "4".
So, the line
name = name.concat(4);
redefines local (for speak method) variable name.
Then you return the reference to this new value with
return name;
So, the original variable, passed within method is not modified, but the method returns modified value.
In the test method you actually modify variable without modifying the reference (the StringBuilder class is mutable, so variable if this type can be modified).
Then we can see another question arising: why StringBuilder.append returns value, where it can seem redundant. The answer to this question lies in the description of "builder" pattern, for which it is the usual way of implementing modification methods. See wikipedia on Builder pattern.
String is immutable in java. As soon as you invoke concat method on name. A new string is created and while you are playing with the old reference in System.out.println(name).If you want to use the modified string you should explicitly return the reference.
While StringBuilder is mutable and it returns the same reference always.
When you invoke speak(name) it computes the new value, but discards it.
If you replace it with
name = speak(name);
the result will be the one you expect.
With the StringBuilder, the object you pass is mutable: so
names.append(names);
changes the state of the current object (it also returns a reference to the same object, which is just a convenience to allow you to write code like names.append(...).append(...) etc.). So in the case of the StringBuilder, the object you are referencing when you call the method has actually changed, hence you see the changes.
In your method speak, the concat method returns a new String, the original object it was called on is unchanged (strings are immutable). As documented:
If the length of the argument string is 0, then this String object is returned. Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
Calling name.concat("4") is the equivalent of name + "4".
In your test method the append method modifies the content of the StringBuilder. As documented:
The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.
In your main method both name and names are still the same object as before the method call, but the content of name is unchanged as strings are immutable, while the content of names has been changed.
If instead you had used the return values of both methods, then you would have the result you were expecting.
First of all, String is an immutable class in Java. An immutable class is simply a class whose instances cannot be modified. All information in an instance is initialized when the instance is created and the information can not be modified.
Second, in java parameters are sent by values and not by reference.
In your method 'test' you don't need names = names.append("4"), instead names.append("4") will be enough .
If you check java docs for String object, you will see that most of the methods there, including concat, will generate a new String.
So to have on output Sam4 also for the String, you will need in main method to have this name = speak(name).
String
String is immutable ( once created can not be changed )object . The
object created as a String is stored in the Constant String Pool .
Every immutable object in Java is thread safe ,that implies String is
also thread safe . String can not be used by two threads
simultaneously. String once assigned can not be changed.
String demo = " hello " ; // The above object is stored in constant
string pool and its value can not be modified.
demo="Bye" ; //new "Bye" string is created in constant pool and
referenced by the demo variable // "hello" string still
exists in string constant pool and its value is not overrided but we
lost reference to the "hello"string
StringBuilder
StringBuilder is same as the StringBuffer , that is it stores the
object in heap and it can also be modified . The main difference
between the StringBuffer and StringBuilder is that StringBuilder is
also not thread safe. StringBuilder is fast as it is not thread safe
.
For more details check this
Conclusion:
You don't need to re-assign the value again to StringBuilder as it is already a reference
test method should be
public static void test(StringBuilder names) {
names.append("4");
}
but speak should be
String name = "Sam";
name = speak(name);

Are Strings overwritable?

My textbook says a String is not over-writable or immutable, i.e, once you enter the value of a String you can't change it. But today when I was running the following code, the String str gets muted as the compiler does not give any error and the new String a's value is successfully entered into str.
class Test
{
static void main()
{
String str = "something";
String a ="anything";
str = a; //str is being over written without any error
System.out.println(str);
}
}
The output is : anything
So, is my book wrong ?
If my book is not wrong please give an example to show that Strings are immutable
The book is correct. When you say str = a you are not changing anything about the String 'something'. You should distinguish between str and something, they are not the same. "something" here is a String object in memory, whereas str is just the reference to that string. Same with the reference a.
When you say:
str = a
You are not changing something, you are in fact saying, "change the reference str to point to whatever the reference a is pointing to." The Strings remain the same, the references change.
On a similar note, this is why you may see in your book that concatenating Strings is expensive, as doing something like:
str = str + a
Would again not be changing the existing Strings, but instead creating a new String object which is equal to the concatenation of the String that the reference str is referring to and the String that the reference a is referring to.
You need to understand what immutable means. In your scenario you are just changing references.
str = a;
will make both a and str to point to String "anything". The Text Book is correct. String is immutable and can not be overwritten. If you check the JavaDoc for String. Most of the methods return a String. This is because any operation in a String will not change that String object but will result in a new String being created. Effectively you can never change a String after you create it. By Change I mean append new characters, remove characters without a new String object being created.
As many answers already point out is that you only change references. Immutable means you cannot change the string itself. for example you do:
String a = "anything";
System.out.println(a); // -> anything
a.substring(3);
System.out.println(a); // -> anything : this is because the String itself is
// immutable.
a = a.substring(3);
System.out.println(a); // -> thing : this is what immutable means to edit a string
// you must reassign it or assign it
// to a new variable
You're changing the reference of str to a. So str effectively becomes a's value.
they are just pointer to that string. so when you do str =a , you just assign pointer of a to str.
The contents of the String object is not being changed. What's happening is that a new String object is being assigned to the variable. The old String object still exists in memory but you just can't refer to it any more. The 'str' variable now refers to the String object containing "anything" but the String containing "something" still exists as it did before. Try assigning 'str' to another variable first and then, after assigning 'a' to 'str', check that other variable and you'll see that it still says "something", proving that that String was not overwritten.
In Java, the value of a variable is never an object, but a reference. Relevant to your case, the type String on the a variable says the variable is allowed to contain only references to String objects.
You can update the value in the variable, sure; but that won't touch the object it is referring to.
Strings are constant; their values cannot be changed after they are created.
The String object is created and stored on constant pool or literal pool.
In your case, when you say,
String str="something"; // An object is created on constant pool with value 'something' and reference 'str' is pointing to it.
String a="anything"; // An object is created on constant pool with value 'anything' and reference 'a' is pointing to it.
And when you do, str=a; then 'str' actually start pointed to 'a', but the object with value as 'something' remains on constant pool with the same value.

Java- creating a String object but assigning otherwise afterwards

I have a question concerning establishing String objects in Java.
Let's say I create a String object like this:
String mystring=new String();
Now, if I take this String object and assign to it a string like this:
mystring="abc";
What exactly happened here? Is mystring addressed exactly to the original object or is it a different object? Like String mystring; is the short-hand for String mystring=new String(); what could mystring="abc"; stand for?
String mystring = new String();
creates a new String object and assigns the value of its reference to the variable mystring.
So
Variable Heap
-------- ----
mytring ---------------------> "" // an empty String object
Then you do
mystring="abc";
This assigns the value of the reference to the String object "abc" to the variable mystring. So
Variable Heap
-------- ----
mystring -------------------> "abc"
"" // will be garbage collected at some point
A variable does not change. The object it's referencing or the reference itself can change.
Like String mystring; is the short-term for String mystring=new
String();
No String mystring; is a variable declaration. When that line is executed, the variable mystring is declared but not initialized.
On the other hand, String mystring = new String() both declares and initializes the variable mystring.
for what could mystring="abc"; stand for?
That is an assignment expression, assigning the value of the reference to the String object "abc" to the variable mystring.
It's also important to understand that Strings are immutable. Once you create a String object, you cannot change it. For example, in the following code
String name = "user3133542"; // cool
name = "some other value";
You are not changing the object that name is referencing, you are creating a new object and assigning its value to the variable name.
The String API does not provide any methods to change its value. We therefore call it immutable.
Consider going through the Java String tutorial.
Also, before you ask your next question, read this
How do I compare strings in Java?
You are changing mystring with mystring="abc"; It is not the exactly to the original object at all. The mystring is a variable not object.

How do Java strings work [duplicate]

This question already has answers here:
What is meant by immutable?
(17 answers)
Closed 9 years ago.
I'm trying to understand exactly how Java strings are immutable. I get that this should probably be an easy concept, but after reading several online web pages I still don't quite understand.
I don't understand how Java Strings are "immutable". I currently have the following code:
public static void main(String[] args) {
String name = "Jacob Perkins";
System.out.println( name );
name = name + "!";
System.out.println( name );
}
My output is the following:
Jacob Perkins
Jacob Perkins!
Why is this happening if a string is supposed to be immutable? Why am I able to re-assign a value to the string?
Let an image explain this for you:
On the left side, you have the variable, which in fact is a reference.
String name = "Jacob Perkins;" The String "Jacob Perkins" is created, and name points to it.
name = name + "!"; A new String "Jakob Perkins!" is created, and the reference now points to the new String. The old one, however, remains unchanged, because String is immutable.
The string itself, once created, can never be changed. What your code sample does is to replace the string in name with a new string that was constructed from the previous contents of name, and an exclamation point. (The original contents of name, which are no longer referenced by any variable, will eventually be reaped by the garbage collector.)
If you were to examine the compiled code (or step through it with a debugger), you would discover that your name + "!" expression had been compiled into the creation of a StringBuilder object and a few operations on that object.
That is, the strings are immutable, but the variable name is not. Its value changes, to point to different strings. The strings themselves never change.
The String objects are immutable only the variable reference changes.
In your example the "Jacob Perkins" object still exists and a new object "Jacob Perkins!" gets created.
The name variable points to the new object.
Maybe this will help you to understand.
Let's say you have a class Point (java.awt.Point).
Let's say you have one instance p:
Point p = new Point(0,0);
Then you make create a new variable y referencing the same object p as:
Point y = p;
If you change the value of p, you also change y. Because the class Point is mutable.
p.setLocation(1,1);
It makes y reference the location 1,1 as well.
Using the String class it does not happen.
String a = "123";
String b = a;
If you make a = a + "4";
The new value of a will be "1234" but b is still "123";
The object they were referencing didn't change, it's just that a is pointing to another object now.
What actually happens is there are 3 String objects created. "Jacob Perkins", "!" and "Jacob Perkins!". You didn't really modify the "Jacob Perkins" instance. You just changed the reference of the name variable, from "Jacob Perkins" instance to "Jacob Perkins!".
String name = "Jacob Perkins";
String name2 = name + "!";
name.substring(5); // or wather syntax is
Those do not change variable name
In Java there are references and there are values.
When you say
String foo = "John Smith";
foo is a variable, holding a reference. The reference points to the object, or value, containing "John Smith".
The reference held by the variable is mutable. I can do this:
foo = "Jack Smith";
And I have mutated the reference. However if I write code like this:
String foo = "John Smith";
String bar = foo; //bar's reference is a copy of foo's reference - the same value
foo = "Jack Smith";
System.out.println(bar); //prints John Smith
We see that even though we changed foo, bar has not changed. Why? Simply, when we wrote foo = "Jack Smith"; we made foo point to a new String. We didn't reach through foo and modify the string, since No method in Java mutates, or can mutate a String. Instead, a new String is returned, and the variable is made to point to the new, also immutable String. This way, the object a String variable 'points' to can never be modified by anything but through that variable. This is an important property of the Java language that it guarantees.

Does this function create a new String everytime this is called?

public String toString()
{
return "NotSubscribed";
}
We are trying to limit String's creation in our application after the objects are instantiated. So, just wondering if this creates a new String every time this toString is called. It might be a bad question, but I don't know the answer. Please suggest.
--
I just remembered that if a String is created, it is stored in the String Pool and after that it is always retrieved from the pool without being re-created. Am I right?
No, that won't create another string each time. String constants are interned - so actually the constant "NotSubscribed" will refer to the same string object throughout your code, not even just every time this method is executed.
From section 3.10.5 of the Java Language Specification:
Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.
However, in your edit:
I just remembered that if a String is created, it is stored in the String Pool and after that it is always retrieved from the pool without being re-created. Am I right?
No, that's not right. Only string constants are interned by default. For example, every time you run this code:
public List<String> createStrings() {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
list.add("foo" + i);
}
return list;
}
... that will create another 10 strings.

Categories