• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

this operator

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}



}
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic