This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this operator

 
tom chansky
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any major performance issues in using the "this" operator in a class?

For example:

Public Class MyObject {
private String title;

public void setTitle(String title){
this.title = title;
}



}
 
M Beck
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i very seriously doubt that this style of writing would slow your code down at all - it seems to me like something the compiler should be able to very easily identify and optimize for. but even if it does, i'd still do it; you gain so much more clarity about what kind of variable you're using, your code gets so much more easily readable, that i think it would be worth it.
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As opposed to what? This code?

It's exactly the same thing. In this case the "this" reference is implied. Even if there were a difference, in any non-trivial program the difference certainly would be dwarfed by GUI code, IO overhead, network latency and so on.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're hiding a variable name, which you're doing here by using the same name in the parameter list as in the class, you need to use this to remove ambiguity.

For example, the following code prints correctly:


The output is "Testing 1-2-3", as expected. However, if we remove the "this" keyword, as in the following code:


The output here is "null". Why? Because Java tried to assign "this.title" to "this.title" because it didn't know which "title" the assignment wanted to use for the right value!

By the way, the original code also had two capitalization errors, "Public" for "public" and "Class" for "class". Java cares about such things.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The choice of variable names does not affect execution times. The choice of method names might do this if it results in dynamic dispatching (polymorphism), but variable references are resolved at compile time.

[ February 25, 2005: Message edited by: Mike Gershman ]
[ February 25, 2005: Message edited by: Mike Gershman ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Bosch:
If you're hiding a variable name, which you're doing here by using the same name in the parameter list as in the class, you need to use this to remove ambiguity.

For example, the following code prints correctly:


The output is "Testing 1-2-3", as expected. However, if we remove the "this" keyword, as in the following code:


The output here is "null". Why? Because Java tried to assign "this.title" to "this.title" because it didn't know which "title" the assignment wanted to use for the right value!

By the way, the original code also had two capitalization errors, "Public" for "public" and "Class" for "class". Java cares about such things.

Actually, the parameter named "title" overrides the member variable named "title". So in the later example, "title=title" assigns the value of the parameter to itself. However, when the method returns, the memory where the parameter is stored is destroyed, so the assignment essentially did nothing.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic