Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IF statement ptoblem

 
Micheal Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

this is my first post on these forums hehe

I am a beginner in java.

These days I was trying out some multi-threading and I am going on quite well but I encountered one strange problem with, believe it or not, an if statement.

I have the following code snippet inside my Run() method:



I think at this point the program should display YES EQUALS but it is displaying:

t1s: l
thread: l

Therefore the problem is that if t1s and thread are the same why is it jumping to the else statement?

Thanks alot for your help
Micheal
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to change ur code with this..
i m not sure about it but may be ur problem is solve.. just try it..

code:


String t1s;


t1s = Thread.currentThread().getName().toString();

if (t1s.equals(Thread.currentThread().getName().toString()))
{
System.out.println("YES EQUALS");
}
else
{
System.out.println("t1s: "+t1s);
System.out.println("thread:"+Thread.currentThread().getName().toString());
}



[ April 08, 2006: Message edited by: saif uddin ]
 
Micheal Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A REAL BI THANKS MY FRIEND!

YOU SOLVED MY PROBLEM.

but may I ask ... why didnt it work with == and it workd with .equals?

just for curiosity hehe
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

== this comparesion operator is not for String but for all
because there is a wounderful method to comparing a strings
whenever u compare 2 String values String.equals()



Regards
SAif uddin
[ July 25, 2006: Message edited by: saif uddin ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the == operator basically compares the value of the variables. tls is a REFERENCE to a string object. what it holds is basically the memory address of where that String is.

the way your code was originally written, you created two distinct Strings, at different places in memory.

Therefore, == returns false.

the equals() method checks the CONTENTS of the objects, to see if they are equivelant.

Note that when you write your own classes, you will have to override the default equals() method with one that makes sense for your class.
 
Jackie Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation! This has just solved my problem too
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic