Question

So I have purchased the book "Java for Dummies" 4th Edition, and I must say it is probably the best 30 dollars I have ever spent on a book. I'm not new to coding, am actually fairly decent at at it if I say so myself.

However, I've come across a line of code that has me a touch confused:

public void setName(String n)
{
     if(!n.equals(""))
     {
          name = n;
     }
}

My question comes up on the third line, the if(!n.equals("")) part...I know how if loops work (ie: if(this == that){do stuff}), but I've not seen the !n.equals("") set up before. Can anyone please explain it to me?

PS: Just to throw in a guess. Is it the same as:

public void setName(String n)
{
     if(n != "")
     {
          name = n
     }
}

I think it's just a way to make sure that if the user doesn't type in a name (ie. myAccount.setName = ""; ) it doesn't kick back an error and runs like normal, but I wasn't sure.

Thanks in advance for the help!

EDIT: changed my "myAccount.name = "";" to "myAccount.setName = "";", sorry about the confusion.

THANK YOU: Goes to Asaph, appreciate the answer! Same to Lucas Aardvark, he answered as well, but Asaph answered my verification comment in his own answer first, thanks to everyone!

Was it helpful?

Solution

if(!n.equals(""))
{
     name = n;
}

means if n is not an empty String, assign its value to name.

In Java, every Object has an equals(Object o) method to test for equality with another Object. The == operator is typically used to compare primitives. It can also be used to compare Objects for "sameness". ie. the two Objects are in fact the same instance. This comes in handy for immutable types such as Strings and all the Object wrappers for the primitive types such as Integer and Long.

OTHER TIPS

In java, strings are immutable but not interned, so if(""==n) might or might not be true for another string for which "".equals(n) is true.

Just to confuse you more, this is bad code, it will get a NullPointerException if called with null as the argument. It should be written as "".equals(n)

The equals() method compares contents of the two strings. The == and != operators tell you whether the two String objects are the same object or not. Two different strings with the same contents, and hence equals() to each other, may still be != to each other. Even though Strings aren't mutable, it helps to understand the difference by imagining that Strings are mutable, and then equals() means "are currently the same" and == means "will always be the same".

The safe rule is to always use equals() unless you're sure both strings are interned. Quoting:

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned.

Consider this:

String a="";
String b="";

Both a and b are String OBJECTS, each with its own memory allocation and thus a unique address. a and b are at different addresses. When you code the boolean expression

a == b

you are comparing the addresses of the objects, not their contents. To compare the contents you must use the String object's equals() method.

An object has a physical location in memory, which is unique for each object -- no two distinct objects can have the same memory address -- and its contents or value. The == operator compares the addresses of the objects; when you code a==b you are asking if a and b are aliased names for the same object -- do a and b refer to the same physical location. a.equals(b) asks if the two objects, wherever they may be, have the same value.

This is complicated somewhat by compiler "interning", where the compiler may detect at compile time that two constants have the same value and reuse the same object, but this won't be true for values created at runtime.

the method equals() will return a boolean value which states that the Object being passed in is 'equal to' the Object making the call. This 'equals to' method can be overridden in classes to make their own test. In the case of String, the test is whether the value of the original String is the same as the value of the String representation of the Object being passed in.

As it returns a boolean, you can negate the value with a !, so the test is 'is the method argument not an empty String?' Yes? then assign it to our name variable.

== will always test whether the Object on the left is the same as the Object on the right, as it compares references.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top