Reputation: 1914
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?
Upvotes: 29
Views: 21326
Reputation: 157
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.
}
}
Upvotes: 0
Reputation: 159
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
Upvotes: 0
Reputation: 12770
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
Upvotes: 1
Reputation: 279245
"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.
Upvotes: 4
Reputation: 21795
Remember that Java final keyword serves two purposes in this case:
Upvotes: 2
Reputation: 19496
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.
Upvotes: 1
Reputation: 137146
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:
See the Wikipedia article on final in Java for examples on each case.
Upvotes: 59