How many instances of String will be created? [duplicate] - java

This question already has answers here:
How many Java Strings Created?
(2 answers)
Questions about Java's String pool [duplicate]
(7 answers)
Closed 7 years ago.
The result of this code:
public class Test {
public static void main(String[] args) {
String s = "Java";
s.concat(" SE 6");
s.replace('6', '7');
System.out.print(s);
}
}
will be "Java"
Who can tell me how many instances of String will be created during execution?

String is immutable in Java. Though you are invoking methods on it, they returns a new string each time.
There are 4 instance created here in this case
Please follow the comments:
String s = "Java"; // 1
s.concat(" SE 6"); // 2 & 3 for concat method returns a new string and another literal created " SE 6"
s.replace('6', '7'); // 4 returns a new string instance which you are not receiving
System.out.print(s);

Related

Difference in the results of String comparison in Java? [duplicate]

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.

Why I am getting different output for string pool while comparing two objects in java? [duplicate]

This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 4 years ago.
I am running this program. To check string pool concept. I refer this link and added two more lines with equals method from Object class in java. Why I am getting different output for objRef1==objRef2 vs objRef1.equals(objeRef2). In my opinion they should be same.
public class Abc2 {
public static void main(String[] args) {
String s1 = "Cat";
String s2 = "Cat";
String s3 = new String("Cat");
System.out.println("s1 == s2 :"+(s1==s2));
System.out.println("s1.equals(s2) :"+(s1.equals(s2)));
System.out.println("s1 == s3 :"+(s3==s2));
System.out.println("s1.equals(s3) :"+(s3.equals(s2)));
}
}
String is a complex datatype so == find two different refetences.
Strings equals() compares the content instead of the reference.
So two "Cat"s has the same content but arent the same object.
Its like you try to say C:\test.txt and C:\test2.txt where the same file because they have the same content.

Passing by reference in Java. Help me understand [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Immutability of Strings in Java
(26 answers)
Closed 5 years ago.
I have the following code
public static void main(String[] args) {
String word = "Super";
reverseString(word);
System.out.println(word);
}
public static String reverseString(String word) {
String helper = "";
int i = word.length() - 1;
while (i >= 0) {
helper += word.charAt(i);
i--;
}
return helper;
I do not understand why when I'm printing the "word" variable it still prints "Super" even though I changed it in the reverseString method. I understand that strings are passed by reference and not a copy like primitive values.
If I do word = reverseString(word) it prints the reverse what I expect, "repuS"
Thanks
You're not changing the string in reverseString, you're creating a new one and returning the new one (which you've called helper).
A second thing to note about strings in Java is that they're immutable - all string methods return a new string rather than modifying the one you're calling the method on.

How String is immutable? [duplicate]

This question already has answers here:
Immutability of Strings in Java
(26 answers)
Closed 6 years ago.
If a String object is immutable then why is it printing "Help"?
The String object s1 shouldn't be modified according to its immutability feature. I am confused for years, please help me understand this:
Code
public static void main(String[] args) {
String s1 = "Hello";
s1 = "Help";
System.out.println(s1);
}
Output
Help
Your second assignment actually is changing the String that s1 references.
There is still a String of "Hello" in existence (in the pool) which cannot be changed.
The behavior you described would be achieved by making s1 final - in which case you would get a compiler error if you tried to change the value the String s1 references.

what does it mean (final Customer c=new Customer(); )? [duplicate]

This question already has answers here:
final variable in methods in Java [duplicate]
(5 answers)
Closed 7 years ago.
I've used final keyword with class, method, fieldsbut this the first i'm seeing something like this
final Customer c=new Customer();
could anyone help me to get what is the use of this?
it is constant reference - you cannot change its value it can be assigned only once when being defined
due to Wikipedia
an example:
final String string = "initial value";
string += " some new content"; //here compiler will raise an error due to you cannot change final value

Categories