java basics

hello experts,

I have got some doubts in java basics

1) what is the difference between String equals() method and '== 'operator,
as per my knowledge '==' compares the reference nos of the object  and  equals() compares the content of the object.

when I tried this example with '==' operator it is showing strings are not equal
            String s1 = "Test";
            String s2 = new String("Test");

but reference no s are same...i know that new operator will create new String object....but why it is giving same hashcode(reference no).

2 ) what is the use FactoryMethods in java...they are used to create the object of the class to which method belongs....in real scenario when we will use FactoryMethods in our own class.

3) what is the advantage of interface compare to abstract class .....what i know is we can't extend more than one class but we can implement multiple interfaces....is that the only advantage or any thing else. I mean we can declare all the abstract class methods are abstract....so any class extends this abstract class must implement the methods.

4) wait(),notify(),notifyAll()....methods are used in Thread communication, even though they are used only  in Thread class....why they are in Object class.


Thanks..!!


mr_objectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

garypfirstechCommented:
1) When used with two String references, == tests to see if both references refer to the same String object.  In your case, they don't.  s1 points to one String object and s2 points to a different String object.  Both objects have the same sequence of characters but they are different objects.  See http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.21.3.

The hashCode() for String objects, however, is calculated based on the sequence of characters in the string.  Since both objects have the same sequence of characters, the hashCode is the same even though they are two separate objects.

2) You can use factory methods for several different purposes.  If you want to create a singleton object, many people use a factory object in order to create the singleton.  In other cases, you use a factory object when you have a pluggable design that let's you plug in different ways to create objects such as when using different service providers.  Have a look at this Wikipedia article:  http://en.wikipedia.org/wiki/Factory_method_pattern

3) An interface is used in java in lieu of multiple inheritance as you state.  If you only need one superclass or interface for your class, some people like to use abstract classes and some like to use interfaces.  If there is not much in common between different classes that use an interface, then I usually use an interface rather than a superclass.  I use a superclass when I have something to put in some of the methods, ie when not *all* of the methods are abstract.

4) In multithreaded programs, you need to do thread synchronization in "working" classes.  Each class instance has its own monitor so you wouldn't want to have to instantiate some helper class for each class that needed to participate in the synchronization.  Have a look at http://java.ittoolbox.com/groups/technical-functional/java-l/why-wait-and-notify-methods-are-defined-in-object-class-not-in-thread-class-757190

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mr_objectAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.