• Post Reply Bookmark Topic Watch Topic
  • New Topic

Process of Calling Methods as value.method()  RSS feed

 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
eg: "Sourdough".toUpperCase();

I guess this is actually a 2-part question:
- How does this work? (nothing seems to be passed through the parameter, so how is it that the value or variable is manipulated in this fashion?)
- How would I create a method that does something similar? (ie. adjusting a variable without having it passed through the parameters)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Java gives you shortcuts for String objects. It's a convenience, since Strings are so common.

Basically (and I could be WAY wrong here), the compiler sees the literal (the thing inside quotes), and makes a String object for you. So really, you are calling String.toUpperCase(), which I would assume then uses the 'this' reference inside.

Note that nothing is being changed here. toUpperCase() doesn't CHANGE the string...it creates a NEW, second string. If you don't then assign a reference variable to point to the newly created string, it is immediately available for garbage collection.

Finally, I don't believe there is a way for you to write your own...unless you want to write your own JVM/compiler that supports it, but I'm guessing that may be more trouble than it's worth.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Fred said, when you write "dhfgdjbl" that constitutes a String object in its own right. You are used to writing obj.foo() where obj is a reference (variable) pointing to an object. In this instance you actually write the object itself.

You can telescope this sort of code… into this sort of thing… provided you don't need to use that Foo object again.
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. Thanks for the explanations, guys! Sorry it took me a while to respond. Programming is pretty fun to learn, so I'll probably be asking more questions later on. =D
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote:Interesting. Thanks for the explanations, guys! Sorry it took me a while to respond. Programming is pretty fun to learn, so I'll probably be asking more questions later on. =D

And just FYI, in languages like C# (and many other object-based languages like Smalltalk) ALL (or most) literals are proper objects. So in C# you can write stuff like:

2.toString();     (not sure of the exact syntax, but hopefully you get the idea)

whereas in Java you can only do it for String literals.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is 2.ToString()
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java already has automatical boxing and unboxing. So why not extend it so 2.toString() would become Integer.valueOf(2).toString()?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Java already has automatical boxing and unboxing. So why not extend it so 2.toString() would become Integer.valueOf(2).toString()?

My opinion: DontSweatIt.

Winston
 
Louis Denning
Ranch Hand
Posts: 64
4
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Java already has automatical boxing and unboxing. So why not extend it so 2.toString() would become Integer.valueOf(2).toString()?

Primitives cannot be dereferenced. The latter is correct, though.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PP meant that as an enhancement to the language.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!