Try this code, and you will see the output, the code uses two String one in a class and another in a variable, the output of the both usages is very different and strange, why and how can I solve it?
---------With class...
null
null
null hello
---------With variable...
null
hello
Setting null to the name becomes into this value for the String, why? I need the name to be null and not 'null'.
Thanks by your help.
//'main' method must be in a class 'Rextester'.
//Compiler version 1.8.0_72
import java.util.*;
import java.lang.*;
class Car {
private String name;
public void setName( String name ) {
if (this.validateName( name ))
this.name = name;
}
public String getName() { return name; }
public boolean validateName(String name) {
return name != null && name.trim() != "";
}
public boolean validate()
{ return this.validateName( this.name ); }
}
class Rextester
{
public static void main(String args[])
{
System.out.println( "---------With class..." );
Car t = new Car();
t.setName(null);
System.out.println( t.getName() );
t.setName("");
System.out.println( t.getName() );
t.setName(t.getName() + " hello" );
System.out.println( t.getName() );
System.out.println( "---------With variable..." );
String nullString = new String();
nullString = null;
System.out.println( nullString );
nullString = "";
System.out.println( nullString );
nullString = nullString + " hello";
System.out.println( nullString );
}
}