The code String name = null;
is simply setting my variable name to the string "null" so I can't figure out how to make my program wait for input. The compiler won't allow me to declare the variable without initializing it.
CodePudding user response:
When we write
String s = "Hello World!";
s = null;
The String still exists in memory because this does not delete it. However the garbage collector will clear the object from memory as there is no variable referencing it.
For all practical purposes s = null;
deletes the String.
CodePudding user response:
Before posting here on basic Java Questions, study the Java Tutorials provided by Oracle free of cost.
See the tutorial page on string literals. To quote:
There's also a special
null
literal that can be used as a value for any reference type.null
may be assigned to any variable, except variables of primitive types. There's little you can do with anull
value beyond testing for its presence. Therefore,null
is often used in programs as a marker to indicate that some object is unavailable.