Win a copy of Rust Web Development this week in the Other Languages 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why does 1b seem so freakin tricky to me.

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn wrote:
But anyway, it looks like you are depending on something that the operating system does. The program will be simpler if you don't concern yourself with squeezing in that last tidbit of optimization. I suggest that instead, you simply avoid the 80th character and always force a new line at the end of each line.
I had created a simple little formula that determined how many args[0]'s I could fit on one line. So if I entered java Hundred Gregg, I know that 13 Gregg's will fit on one 80 character line. Marilyn's suggestion would imply that I need to not worry about the 80th character, but if I am solely printing Strings, how can I worry about each character of a line that I have no representation of.
With the exception of some optimization on string concantinations, my formula and program works. So I am kind of confused.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always love this one because the simplicity is mind-boggling. Do what Marilyn suggested, just act like the 80th character doesn't exist.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jason adam:
I always love this one because the simplicity is mind-boggling. Do what Marilyn suggested, just act like the 80th character doesn't exist.


That's the problem? The 80th character of what? I am dealing with a String of variable lengths everytime the program runs. I have to determine how many of those strings fits on a line 80 characters in length, right?
[ November 19, 2003: Message edited by: Gregg Bolinger ]
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm at work at don't have access to look at my old assignments and nitpicks, but I'll offer a tip of a more general nature. (I think it applies here. If not, sorry.) When doing Cattle Drive assignments and given the choice between string concatenation and printing as you go, favor printing as you go.
(Am I giving too much away?)
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
I'm at work at don't have access to look at my old assignments and nitpicks, but I'll offer a tip of a more general nature. (I think it applies here. If not, sorry.) When doing Cattle Drive assignments and given the choice between string concatenation and printing as you go, favor printing as you go.
(Am I giving too much away?)


Well, that seems to go against what I have been told from the nitpicking. Optimiztion, Optimization. Well, at least concantinate outside of the loop that is.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if I entered java Hundred Gregg, I know that 13 Gregg's will fit on one 80 character line.
Maybe Java can't handle so many gs in one word?
[ November 19, 2003: Message edited by: Michael Matola ]
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Optimiztion, Optimization.
I thought it was more Simplicity, Simplicity, Readability, Readability, oh, and if by the way there's a chance for Optimization, go ahead.
Maybe others have a different take.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe, but if you want Java to understand your name isn't Gregory, then it's a good idea to go with the extra g. Just call me 3-G Mo Money B. Well, Maybe 3-G No Money B. That is more accurate.
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JA:
Oh the cow in the meadow goes "moo"
Oh the cow in the meadow goes "moo"
Then the farmer hits him

Aren't cows female?
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deal with Strings, but don't deal with the 80th character. Get it out of your head, act like that 80th column isn't there.
And I didn't write the song Mike, Phoebe from Friends did, go nitpick her
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhhhh OneBee! T'was one of the most significant moments of my life...
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I didn't write the song Mike, Phoebe from Friends did, go nitpick her.
Didn't realize that. I thought it was the work of some terribly droll farmers out on the high plains of Colorado, possibly under the influence of alcohol.
 
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 Gregg Bolinger:
The 80th character of what? ... fits on a line 80 characters in length, right?


I think you answered your own question.
 
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
Wow. I can't even follow some of these arguments. Can someone point me to a tutorial on optimization? Exactly what is the difference between concatenation and print as you go? Why would one be better than the other in any one particular program? I need a drink..................
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about a previous thread on the subject?
 
Carol Murphy
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
You know, I remember trying to follow that thread back when it first came out, and I have to admit that I can follow it a lot better now, but I still have this block because I can't visualize the difference between print as you go and concatenation. I'm still missing a large part of the puzzle. I'm going to look at the API for concat() to see if I can fill in the blanks. Thanks for the link!
 
Michael Matola
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some code that attempts to make the distinction clearer. Maybe it would be less confusing if you read "concatenation" in these discussions not just as plain old concatenation, but as "concatenation into a holding variable".

[ November 22, 2003: Message edited by: Michael Matola ]
 
Carol Murphy
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
Okay, so in the case of concatenating all these bits of strings into one long string, you have to create a whole bunch of intermediate strings that take up system resources? Does that tie up a lot memory, or what?
What happens in print as you go to each string after it has been printed? Does it just go away, so it doesn't use up any memory?
Sorry, but there is a BIG HOLE in the middle of my understanding about how programs work. I don't understand the concepts of the stack and the heap, so a lot of this just escapes me. I've tried to read up on this subject, but I haven't found any satisfactory material yet.......
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the case of concatenating all these bits of strings into one long string, you have to create a whole bunch of intermediate strings that take up system resources? Does that tie up a lot memory, or what?

You have to create a whole bunch of intermediate objects (not all of them are strings). Creating a new object is very resource intensive in Java. It's slow. It uses memory. That doesn't mean that objects are bad, just that they shouldn't be created unnecessarily.

What happens in print as you go to each string after it has been printed? Does it just go away, so it doesn't use up any memory?

They are still there in memory, but the extra intermediate objects aren't.

I don't understand the concepts of the stack and the heap,

Primitives get stored in the stack and ojbects get stored in the heap. They are just different areas of memory.
Look at it this way.
If you want to print the letter 'a' on a piece of paper, do you go to the computer, turn it on, open the text editor, type 'a', and print it with your printer
or
do you pick up a pen and write 'a' on the paper?

Which one uses fewer resources?
[ November 22, 2003: Message edited by: Marilyn de Queiroz ]
 
Carol Murphy
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
Thanks Marilyn. It makes a little more sense now. Your example made me laugh, because my Pharmacy Manager actually does use the computer to print out EVERYTHING. I've never seen a handwritten note from him, no matter how short.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Matola:
Here's some code that attempts to make the distinction clearer. Maybe it would be less confusing if you read "concatenation" in these discussions not just as plain old concatenation, but as "concatenation into a holding variable".


In relation to the discussion about optimization, don't forget about all of the effects of what you're doing. First, by using string concatenation with the + operator, you're creating a temporary StringBuffer for each full expression. By breaking apart the full expression into pieces, you're creating more temporary SBs than necessary. Instead, you could do the following:

Recall that StringBuffer.append() returns itself so you can string together several calls to append() in a chain. So that optimizes the use of StringBuffer, but how does this relate to the print-as-you-go method?
I/O operations are always complex and slow in comparison to manipulating small amounts of memory. For one thing, they're usually synchronized, and that's a pretty expensive operation. Most people understand that you should use some form of buffering when doing I/O because reading 1000 characters in one shot is far faster than reading an individual character 1000 times.
The same principle applies here except that you're doing the buffering by building up a StringBuffer instead of wrapping the OutputStream with a BufferedOutputStream. The effect is the same, however: faster printing.
Now, is the extra complexity worth it? Probably not for something so simple, but I use it a lot when creating simple servlets that build the HTML output directly.
reply
    Bookmark Topic Watch Topic
  • New Topic