views:

311

answers:

5
+3  Q: 

String and Final

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?

+27  A: 

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.

Ayman Hourieh
Remember 'final' is also about safe publication to other threads, though (see my answer below).
Neil Coffey
+1  A: 

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.

Nathan Feger
+1  A: 

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".
Neil Coffey
Yup, the semantics of final is very important in concurrency applications, not so in normal Java applications, because Java programmers tends to embrace mutability.
egaga
+4  A: 

"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.

Steve Jessop
+1  A: 

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
pgras