-1

This is my debugging code:

log.error(u.getName() + " - " + u.getName().length() + " - host_node" + " - " + u.equals("host_node"));         

My log:

11:27:16 [main] ERROR com.google - host_node - 9 - host_node - false

Why it doesn't equal, I am even checking length, to see if first contains whitespaces. But it does not.

Jaanus
  • 16,161
  • 49
  • 147
  • 202

4 Answers4

8

I would use following as I suspect u is not a String.

u.getName().equals("host_node")

or better you can use the following to get false if the name is null.

"host_node".equals(u.getName())
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I will always go with second option. Safe check to save the code from most irritating NullPointerException. :) – Vishal Nov 07 '12 at 10:50
1

You should compare u.getName() with the text, thus:

u.getName().equals("host_node")
sp00m
  • 47,968
  • 31
  • 142
  • 252
iOS
  • 813
  • 5
  • 14
0

This

 u.equals("host_node")

compares the string with the object u, not its name! i.e. it's calling u.equals()

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

I guess

u.equals()

is not what you wanted to compare. Maybe you want the name of u?

u.getName().equals() would be a better choice then.

Anyhow, your Post lacks some information about u and other context to answer this properly.

Stefan
  • 2,603
  • 2
  • 33
  • 62