• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IF statement ptoblem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation! This has just solved my problem too
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic