• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

few queries

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. why does not StringBuffer class have a
equals method. Since most of the class
have is it just StringBuffer or some other
classes do not have equals method to be overridden from the one from object.
since if it is not overridden is there any difference between == operator and equals method.

2.why is it possible to access static variables using this otherthan main. I understand for the main then why isn't it applies to all the methods.
3.why does System.out.println(- -b) behaves strangely .
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aryan,
I couldn't get your questions 2 & 3 properly. Can you send in sample code that you might have written.
I ran the following code and it works. Of course if you remove the comments then it works only if you remove the "this" reference too as both main() and method() are static methods.

I can't answer your first question. May be someone else will.
Thanx.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aryan,
1. StringBuffer and equals() - I think the reason for this is that StringBuffer is not immutable - that is, its value may change anytime. This is in contrast to immutable objects like String and the wrapper classes, whose values may not change once set. (Note that String and the wrapper classes override the equals() method.)
2. static variables - you can acces static variables in two ways: the class name or an instance of an object. Using the keyword this is the second option.
3. (- -b) - Java interprets this as -(-b) -> b.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. I don't know either why StringBuffer has no equals() method, but Paul's point seems reasonable. There are other classes without an overridden equals(), such as Throwable. Finally, there is no difference between equals() of Object and the == operator between object references. According to the Java docs:


The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).


2. Every instance method has an object reference called this. this is just a reference to the object that "owns" the instance method. Static methods do not have this, as, for example, main(). As Paul pointed out, you can access a static variable (assuming the access modifers allow you) using either the class name, or using an object reference, such as this.
3. What do you mean by "strangely"?
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic