This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 8 years ago.
String a = "Hello";
String b = new String("Hello World");
Can someone please tell me how many objects are created and elaborate.
Thank you.
String greeting = "Hello world!";
In this case, "Hello world!" is a
string literal—a series of characters in your code that is enclosed in
double quotes. Whenever it encounters a string literal in your code,
the compiler creates a String object with its value—in this case,
Hello world!.
String a = "Hello"; // 1 object
String b = new String("Hello World");
// 1 object with new String(),
// 1 object with "Hello World"
in total you created 3 objecs.
Related
This question already has answers here:
How to Replace dot (.) in a string in Java
(4 answers)
Closed 2 years ago.
I have following problem with java.This method in Class should return String as is.
private String getAsString(Resource res) {
return "We wish you good luck in this exam!\nWe hope you are well pre-\npared.";
}
Then in Constructor this String shlud be converted into array of words
private int index;
private String string_arr[];
public TextFileIterator(Resource res) {
this.index=0;
if(res==null){
throw new NullPointerException();
}
String text=this.getAsString(res);
//text=text.replaceAll("-\n(?=[a-z])", "");
text=text.replaceAll("\\n", "");
text=text.replaceAll("!", " ");
text=text.replaceAll("-", "");
text=text.replaceAll(".", "");
this.string_arr=text.split(" ");
}
Problem is that at the end I get array which is null... what is the problem. I attach the debugger screenshots.
Please could explain me why does it happen?
The culprit is line no 17-
text=text.replaceAll(".", "");
The above line is replacing all of the content with "", because in regex world "." means any character.
Try this instead-
text=text.replaceAll("\\.", "");
This question already has answers here:
String s = new String("xyz"). How many objects has been made after this line of code execute?
(21 answers)
Closed 3 years ago.
How many objects will be created in the following code and where they will be stored?
String s = "abc"; // line 1
String s1 = new String("abc"); // line 2
String str1 = new String("efg"); //line 3
Total 3 objects will be created.
Line 1 : Object will be created in string pool,
Line 2 : Object will be created in Heap,
Line 3 : Object will be created in Heap.
Reason is :
a) By string literal : Java String literal is created by using double quotes like in line 1. It is always created in String pool,
b) By new keyword : Java String is created by using a keyword “new” like in line 2 and 3. It is always created in Heap memory.
For reference:
https://www.geeksforgeeks.org/string-initialization-java-string-literal-vs-string-object/
This question already has answers here:
Immutability of Strings in Java
(26 answers)
String is immutable. What exactly is the meaning? [duplicate]
(19 answers)
Closed 4 years ago.
I was learning string concepts, so wrote a code,expected a different output but got something very unexpected.
class stringmute
{
public static void main(String[] args)
{
String s1="Hello "; //string one.
System.out.println("Str1:"+s1);
String s2= s1+"world"; //New String.
System.out.println("Str2="+s2);
s1=s1+"World!!"; //This should produce only Hello right?
System.out.println("Str1 modified:"+s1);
}
}
when I execute the above code i get the output as:
Str1:Hello
Str2=Hello world
Str1 modified:Hello World!!
if i've done something wrong please let me know.
Since strings are immutable, which implies we should get the output of the "Str1 Modified" as "HELLO" instead of "HELLO WORLD!!".
When you assign s1 as :
s1=s1+"World!!";
New String created in jvm string pool and assigned to s1.
So it's value became "Hello World!!"
This question already has answers here:
Questions about Java's String pool [duplicate]
(7 answers)
Closed 9 years ago.
Java Question: Suppose str is a String variable. The statement str = new String("Hello World"); is equivalent to ?
Here are my choices...
a.
new String = "Hello World";
c.
str = "Hello World";
b.
String new = "Hello World";
d.
"Hello World";
It's equivalent to declaring and initializing it at once:
String str = new String("Hello World");
You don't need to do new String("...") though. You already have a String literal. You can just do:
String str = "Hello World";
Otherwise, you're taking the first, canonical String object ("Hello World") and unnecessarily using it to initialize a second object (new String(...)) with the same text.
Edit: According to the choices you've posted now, it's not exactly equivalent to any of them. As explained in more detail by the other answers, "Hello World" has a subtle (yet important) difference to new String("Hello World"), so it is not exactly equivalent to str = "Hello World". On the other hand, the other three options don't compile at all, so that is certainly the intended answer.
Lots of answer get it right about internalization. Take this program:
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
String str4 = "He";
System.out.println(str1 == str2); // true
System.out.println(str1 == str3); // false
System.out.println(str1 == str3.intern()); // true
str4 = str4.concat("llo");
System.out.println(str1 == str4.intern()); // true
}
The interesting issues are point 2 and 3. new always creates a new object (as per JLS 12.5), so str1 != str3. What happens with internalization is that the new object points to the internalized one, which you can retrieve with String.intern(). Similarly, another String created in a form completely unrelated to the original literals (str4) also gets "interned".
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the purpose of the expression “new String(…)” in Java?
I know that String s = new String("Hello World") should be avoided as it will create extra space for "Hello World" which is unnecessary in most cases.
Related question explaining why String s = new String("Hello World")should be avoided is here:
What is the difference between "text" and new String("text")?
But when do we need to use String s = new String("Hello World"), instead of String s = "Hello World"? This is a interview question I experienced.
If String s = new String("Hello World")should be avoided in most cases, why Java still allows that?
1) String s = "text";
This syntax will allocate memory for "text" in heap. and each time when you will assign this "text" to other variable it will return the same memory reference to each time.
for Exp -
String aa = "text";
String bb = "text";
if(aa == bb){
System.out.println("yes");
} else {
System.out.println("No");
}
will print - Yes
but
String s = new String("text");
Always create a new location in memory and returns a new reference each time.
for Exp -
String aa = new String ("text");
String bb = new String ("text");
if(aa == bb){
System.out.println("yes");
} else {
System.out.println("No");
}
will print - No