-6

what is the difference between :

String s1;

And

String s1 = new string();

How does the memory operate?

Amit
  • 45,440
  • 9
  • 78
  • 110
NM2
  • 127
  • 6

1 Answers1

2
String s1;

Is either:

  1. An uninitialized local variable. Attempting to use s1 without first assigning a value will result in a compilation error.
  2. An uninitialized field. It will have a default value (null).

String s1 = new String();

Is a variable or field initialized to an empty string;


The first declares an identifier or field for later use, while the second allocates and assigns a value (empty string) to the identifier.

Amit
  • 45,440
  • 9
  • 78
  • 110