233k views
3 votes
What will the following write?

String str = "Hello World!" ;
str.substring(6);
System.out.println( str );

User Drunken M
by
7.0k points

1 Answer

4 votes

Final answer:

The System.out.println(str) in the provided Java code will print the original string "Hello World!". The str.substring(6) has no effect as the result is not assigned to any variable.

Step-by-step explanation:

The student has asked what will be printed by the following Java code snippet:

String str = "Hello World!"; str.substring(6); System.out.println(str);

The substring method in Java is used to extract a portion of a string. In the code provided, str.substring(6) is called, which would return a new string starting from index 6 of the string str.

However, it is important to note that the substring method does not modify the original string; instead, it returns a new string. Since the new string is not assigned to any variable, the original string remains unchanged. Therefore, when System.out.println(str) is executed, it prints the original string "Hello World!".

User PieCot
by
8.4k points