Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

what do you think?

 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this page that someone wrote to explain about print() and strings and such:

http://www.cs.umd.edu/users/clin/MoreJava/Intro/print-to-screen.html

what do y'all think?
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While just about everybody uses concatenation with the empty string to convert a primitive to a string, I don't think I'd recommend that to Java learners. The Java API does have methods to do that, after all.
 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that's about the clearest discussion of this subject I've come across. I'm sure I've heard this discussed before, but I either forgot or never quite understood what was being said.
Concatenation and evaluation from the left are subjects that seem to be glossed over as things everyone already knows in most of the Java books I've read, or I simply didn't understand fully what was being presented. Now I think I get it. (!)
And that bit about the + operator being overloaded so at least one of the arguments to print() must be a String rings a faint bell in the nether regions of my brain, but brushing up on the basics seems to be something I'm constantly in need of. (Probably because I only get to write code on the rare occasions where I'm not working or sleeping or driving to or from work...)
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you guys. When I came upon that page I had actually been looking around for something that went into what happens with something like:

int dogsInMyHouse = 3 ; // I wish!
System.out.print( dogsInMyHouse ) ;

The original question really was: why don't we need to do

System.out.print( dogsInMyHouse.toString() ) ;

So though the concatenation "trick" isn't really the issue, I wondered about your impressions of that article.

This comes up every once in a while with nitpicking, and I've even thought about doing a campfire story on it or something, but my lazy side told me to start with google. Think I'll keep looking around first...
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pauline McNamara:
I had actually been looking around for something that went into what happens with something like:

1) int dogsInMyHouse = 3 ; // I wish!
System.out.print( dogsInMyHouse ) ;

The original question really was: why don't we need to do

2) System.out.print( dogsInMyHouse.toString() ) ;


Because System.out is an object of type PrintStream, I looked in the PrintStream API. I see that the print() method (and the println() method) is overloaded to accept all sorts of input including primitives like int as well as Objects and Strings.

You can't call toString() on a primitive, so your second example will not work in any jdk less than v5. In jdk 5 it will work, not because it makes dogsInMyHouse into a String, but because autoboxing will convert the int to an Integer and call toString() on the Integer.

Regarding the article (last updated in March, 2004), I think it has some good points. However, I have an issue with his/her statement, "You may not realize it, but each time a concatenation occurs, a new string is created. This is considered slow. ... As a programmer, you don't have to care (at least not now)."

I also think that there is some minor misinformation in the Concatenation and Mixed Types section. My curiosity is aroused, so I'm going to do some experimenting and then I'll pass along the results.
[ January 03, 2007: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn de Queiroz:
.... so I'm going to do some experimenting and then I'll pass along the results.


It wasn't that long ago that I was elated to arrive at my first test-results, only to see on subsequent iterations of the loop that an optomization {which I now know to be called better late than never} was swamping any test results I had set out to obtain. Like many issues in computer science, the usefullness of the results can only be meaningful in light of some application scenario. That can very legitimately include testing only, to find out what is going on.

I was happy to get any test results at all, but I suggest some loaded loops with analysis of iterations v times grouped into time domains that reflect how long the run has been in effect.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn de Queiroz:

Because System.out is an object of type PrintStream, I looked in the PrintStream API. I see that the print() method (and the println() method) is overloaded to accept all sorts of input including primitives like int as well as Objects and Strings.



Ah, yes, that's the trail I was looking for.


You can't call toString() on a primitive, so your second example will not work in any jdk less than v5. In jdk 5 it will work, not because it makes dogsInMyHouse into a String, but because autoboxing will convert the int to an Integer and call toString() on the Integer.



Oops, bad example. Great correction. Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic