This question already has answers here:
Real Life, Practical Example of Using String.intern() in Java?
(5 answers)
When to use intern() on String literals
(4 answers)
When should we use intern method of String on String literals
(14 answers)
Closed 5 years ago.
I know how "intern" method of String works. But is there any practical / real life scenario where we will actually need to use intern method ? In that case is there any benefit of using intern over not using it ?
Or is it just a nice to have feature; only to be asked in interviews :)
Related
This question already has answers here:
when should I override Equals function? [duplicate]
(3 answers)
Closed 2 years ago.
Why Equals() method override in Java.Reasons for it?
I was not able to understand it clearly.
The java string equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.Whereas (==) compares the references.
This question already has answers here:
Java Equivalent to .NET's String.Format
(6 answers)
String replacement in java, similar to a velocity template
(12 answers)
Closed 4 years ago.
I need to augment some string like it's done by logback/famous loggers or property accessors.
String str= "I can {0} give {1} back your horses or restore your {2} to life.";
String[] got=new String[]{"not","you","dead"};
Are they any utility functions provided by any library that can convert str to replace {i} with indices in array?
Its most probably a duplicate and please refer me to some similar question.
This question already has answers here:
Run piece of code contained in a String
(9 answers)
Closed 5 years ago.
I'm trying to convert a string in code in Java, but i have no idea how to do it or if it is possible.
This is my Java code (Measure is an other class I have created)
String str= "Measure m = new Measure(10,1);";
Is it possible to run the code in the string?
No I dont think that is a good practice, you don't need to do it that way,
just instantiate outside of the string, it will be fine and good practice.
Measure m = new Measure(10,1);
This question already has answers here:
How do I compare strings in Java?
(23 answers)
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
Closed 7 years ago.
I tried below given examples
String s=new String("cat");
String s1=new String("cat");
System.out.println(s==s1);
System.out.println(s.equals(s1));
this gives me output false and true.
String s="cat";
String s1="cat";
System.out.println(s==s1);
System.out.println(s.equals(s1));
in this case the output of both is true
may i know the difference between both
i heard both wrapper class and normal string declaration gives same output
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
String a ="abc";
return (a.substring(1)=="bc");
I tried to print the result of
a.substring(1)
which is also
"bc"
Why the result is false?
I think it's true.
== compares references and the value of primitives (int, long etc), use a.substring(1).equals("bc") instead.
It should be like this:
String s = "abc";
System.out.println(s.substring(1).equals("bc"));