How to create a String in Java ?
Always use this expression to create a string variable.
String helloWorld="Hello World";
Don't use new keyword to create a String. Because new keyword will create many instances of the String.
(For ex)
for(int i=0;i<10;i++){
String data = new String(i);
System.out.println ( data );
}
The above code snippet will create 10 String variables .
String data = " ";
for(int i=0;i<10;i++){
data = "DATA "+i;
System.out.println ( data );
}
The above code snippet will create 1 String variable. This is the optimized suggested way to deal with strings.
Always use this expression to create a string variable.
String helloWorld="Hello World";
Don't use new keyword to create a String. Because new keyword will create many instances of the String.
(For ex)
for(int i=0;i<10;i++){
String data = new String(i);
System.out.println ( data );
}
The above code snippet will create 10 String variables .
String data = " ";
for(int i=0;i<10;i++){
data = "DATA "+i;
System.out.println ( data );
}
The above code snippet will create 1 String variable. This is the optimized suggested way to deal with strings.
Comments
Post a Comment