Consider the following code,string s=new String(); will create an instance of string with
Question
Consider the following code:
string s = new String();
will create an instance of string with
Solution
The code string s = new String();
will create an instance of a string with no characters in it, essentially an empty string.
Here's the step-by-step explanation:
-
string s
: This declares a variables
of typestring
. In other words,s
is a variable that will hold a string. -
new String()
: This is a constructor that creates a new instance of theString
class. The parentheses()
indicate that this is a method call. In this case, it's calling theString
constructor method with no arguments. -
=
: This is the assignment operator. It assigns the value on its right (the new String instance) to the variable on its left (s
). -
;
: This is a semicolon that marks the end of the statement.
So, after this line of code is executed, s
will be an empty string.
Similar Questions
String myString = new String("This is a string!");System.out.println(myString.toString());What will be returned by this snippet of code?
What is the output of the following code snippet? String str1 = "Java"; String str2 = new String("Java"); System.out.println(str1 == str2);
Which of these method of class String is used to check whether a given object starts with a particular string literal?
difference between String a = "Selenium"; and String b = new String("Selenium"); ####
ct answerWhat will s2 contain after following lines of Java code?StringBuffer s1 = "Ram";StringBuffer s2 = s1.append(" Ali")
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.