This question already has answers here:
Immutability of Strings in Java
(26 answers)
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
In java String is a class and it is imutable so we can not change its value.In following code it will concate other string without any error.So I want to ask that if it is immutable then why in this following code value of String is changed??
import java.util.*;
public class conc
{
public static void main(String args[])
{
String a="Sheetal";
a=a+"Ga";
System.out.println("Result:"+a);
}
}
In the code that you have shown, you have not changed the original String object.
Instead, you have created a new String object, which represents a + "Ga", and then re-assigned it to the reference variable a.
Note that all variables in Java other than primitive types are references.
You are creating a new object by concatenating two strings, that is: You are not changing the object referenced by a but assigning to that reference the value referencing to a new String object.
String a="Sheetal";
a=a+"Ga"; // now this is not the same object you are referring early
When you alter your a will create a new String.
Your original String is not change that's why we call String are immutable and new String will create in the heap.
In this moment there are 2 object in your heap. now a is referring new Object.
Related
This question already has answers here:
Difference between heap memory and string pool [duplicate]
(2 answers)
Closed 3 years ago.
There is a rule in Java, that to create any object of class we have to use 'new' keyword, but when we use String class,we can create object as
String s = "hello";
so we haven't used new as an operator still new object has been created in String constant pool in heap!
Can anyone explain how we created an object without using new keyword!
Comparision of string initialization performance for String Literal and String object. :
String Literal
String str = “Hello”;
This is string literal. When you declare string like this, you are actually calling intern() method on String. This method references internal pool of string objects. If there already exists a string value “Hello”, then str will reference of that string and no new String object will be created.
String Object
String str = new String(“Hello”);
This is string object. In this method JVM is forced to create a new string reference, even if “Hello” is in the reference pool.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
How many Strings are created in memory?
(4 answers)
Closed 4 years ago.
How does Java implement the below string comparisons
public class MyClass {
public static void main(String args[]) {
String a = "Chaipau";
String b = "pau";
System.out.println(a == "Chai"+"pau"); //true
System.out.println(a == "Chai"+b); //false
}
}
This is different from How do I compare strings in Java? , as the answer does not contain the why a new object is created in the second case , when it could have pointed to the same object reference as it is done in the first case.
"Chai"+"pau" is semantically identical to "Chaipau", and thus is the same instance that a refers to.
"Chai"+b is evaluated at runtime (because b is not a compile-time constant expression), creating a new instance of String, and thus is not the same instance that a refers to.
This question already has answers here:
Why does Java's concat() method not do anything?
(6 answers)
Closed 5 years ago.
i would like to know why the output of this code is "roar roar!!!" not "roar!!! roar!!!"?
the code is:
public class Lion
{
public void roar(String roar1, StringBuilder roar2) {
roar1.concat("!!!");
roar2.append("!!!");
}
public static void main(String[] args)
{
String roar1 = "roar";
StringBuilder roar2 = new StringBuilder("roar");
new Lion().roar(roar1, roar2);
System.out.println(roar1 + " " + roar2);
} }
i try to find the reason of method concat() dont appends one String to the end of another. please with explain.
Here roar1 is you string variable and roar2 is your object of class string builder, and only string builder is mutable, string can not be mutated.... Because of which only inbuilt function in class string builder concate will work on its object roar2.
Roar1 is not object of string builder thus concate function will not work on it.
As said before, Objects of the class String are always immutable.
Additionally you have to remember how variables in JAVA are passed to some Methods.
In your case you build a String and a StringBuilder an pass them to your roar() method. The JVM will create two new Variables which are referencing the two orignal Objects. Due to the immutablity of your local String Variable, a new Object will be created, if you want to change this Object. The append() method of an String Object, normally creates and returns a new Instance of a String, with the changes you've made. You doesn't have a reference on this Object. Even if your local String references this object, the String Variable outside of the roar() method still references the old String ("roar").
That's the reason why your System.out.println, doesn't show the changes you made.
This question already has answers here:
Final variable manipulation in Java
(11 answers)
Closed 8 years ago.
If name is declared final, why i can still call name.append and the output is: shreya? I thought final variables cannot be changed once a value is assigned?
public class Test1 {
final static StringBuilder name = new StringBuilder("sh");
public static void main(String[] args) {
name.append("reya");
System.out.println(name);
}
}
final refers to not being able to change the reference, e.g. you cannot say name = new StringBuilder(). It does not make the referenced object immutable.
Immutability is a property of a class. An object of a mutable type is always mutable.
You have to start making the distinction between variables, values (reference values and primitive values) and objects and primitives.
A variable is container for a value. That value is either a reference value (for objects) or a primitive value.
You cannot use the assignment operator to assign a new value to a final variable once it has been initialized with its original value.
This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 6 years ago.
If I declare a String as
String test=new String("testing");
and
String test1="testing1"
Since String is a class in JAVA how does test1 be a String Object without using a new Operator.Also,when a new Operator is used memory is assigned for new String("testing") so in the case of test1 how is the memory assigned?
Also,when the string is interned ,if two Strings have the same value with what reference is the String store once in the String intern pool?
Let us first consider this String test=new String("testing");
It creates an String Object in Heap.No Checking is done in String Pool for existence of this String in the pool.
and now this String test1="testing1"
It creates String a String Object in String Pool not in Heap.Before Creation check is done whether this string is already there in the pool.If yes its reference is returned else a new String is created in the pool and its reference is returned.Basically this is a String Literal, which is put in Constant pool for memory optimization and re-usability.
intern(): It is used when you construct an object using new() and you call intern() on that object then first a check is done in Stirng pool if that String already exists there or not,if yes it is directly used
Java has a separate memory for Strings that are created without calling the constructor with new. Every time such a String is created Java checks if that String is already in this memory. If it is, then Java sets the same reference to the new String until one of them changes.
When you create a String with the constructor using new then it behaves as a normal object in Java.
Take a look at this example:
String s1 = "Test";
String s2 = "Test";
When you compare this String with the == operator it will return true. s1.equals(s2) will also return true.
It looks different if you create String objects with the constructor like this:
String s1 = new String("Test");
String s2 = new String("Test");
When you now compare this Strings with the == operator it will return false, because the reference of this strings is now different (you created 2 unique String objects).
But if you use s1.equals(s2) it will return true as expected.
When you are using
String test1="testing1"
then it means you are storing only one copy of each distinct string value
but
String test=new String("testing");
gives you a new string object.
Consider your second assignment was:
String1 test1 = System.getenv("PATH");
Here, test1 is most probably also a reference to a String object, without using new().
You can assign references to already existing objects to new variables.
So where is the problem?
The problem is, you must not use sloppy wording like "test1 is a String object". It is not. It is a reference to a String object or null. That's all about it.