Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

4a questions

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code goes something like this:
class Say
{
static int number;
//Nitpick goes here -- see below
method1 ( int number )
{
(code);
}

method2 ( int number )
{
(code);
}
main method
{
(lots of code);
}
}
I used the rule "pretend the compiler goes through the code only once as in days of old" and placed my variable at the top (because it's used by the methods). Nitpick result: "Rather than being an object attribute, this looks more like a global variable. I don't think you need this."
Does the compiler rule only apply to methods?
2nd question: I prefer to concatenate a String instead of printing as you go. This is because I find it confusing to figure out where to put a "print" instead of a "println" and vice-versa. Was this nitpicked due to performance issues? Or why is it preferred to print as you go? To me it isn't easier, especially to type.
Thanks
Paul R
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, The use of global variables is bad. Put the var close to the point it is first used (main perhaps) and then pass it to the other methods. And yes the one pass rule applies to methods; I got nitpicked for that one. I think the reason they say print as you go is to do more with the size of the assignment than performance or any other reason, why keep track of the string.
Hope this helps.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As to your second question, I think it has to do more with performance. Since strings are immutable, everytime you do a string concatenation, you are creating new strings. If you are only printing this once, printing as you go will give you better performance because it doesn't have all they holds on memory. If you need to keep the value around for other objects or future use, then I would use concatenation. (however I would probably use a string buffer)
Bill
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was concatenating strings too. To eliminate confusion, I just used print statements instead of println. I didnt know until reading Bill's response that it was a performance issue.
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill is giving all of us a lesson on strings. Thanks Bill
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the rule "pretend the compiler goes through the code only once as in days of old" and placed my variable at the top (because it's used by the methods).
Does the compiler rule only apply to methods?


For this assignment the variable should be at the top of the method and be passed to other methods as necessary, unless it is a constant, in which case it goes at the top of the class.

2nd question: I prefer to concatenate a String instead of printing as you go. This is because I find it confusing to figure out where to put a "print" instead of a "println" and vice-versa. Was this nitpicked due to performance issues? Or why is it preferred to print as you go? To me it isn't easier, especially to type.

print() only prints the String. println() prints the String and jumps to the next line.

As far as typing it, I usually just cut & paste.

It keeps the code simpler and more readable if you just print as you go instead of concatenating and passing Strings back and forth, not to mention the performance issues associated with creating new String objects.

As for using StringBuffer, the rule we follow here is simplicity before performance. Besides, why bother with appending and converting from String to StringBuffer and back again when you can write code that is simpler and more readable using print()?
 
Marilyn de Queiroz
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As to your second question, I think it has to do more with performance. Since strings are immutable, everytime you do a string concatenation, you are creating new strings.

Actually, you are creating more than just new Strings. Every time you concatenate Strings you create 4 objects, and object creation is expensive performance-wise.
 
Paul Ralph
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a nitpick-y grumble about calling a variable with package scope "global", but your explanations have helped quite a bit.
Thanks
Paul R
 
Marilyn de Queiroz
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically you are right, Paul. However, since this is the only instance of this class, and you are creating a variable that is "global" to this class, we're trying to show a better way and build good habits.
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks. This thread has been very informative.
Just needed to make sure I understood correctly what you mean by "printing on the go". It looks to me like you're referring to using the print method at a given point in main() to print out something, as opposed to concatenating a string with whatever that something was and printing it out somewhere else in the "new" string object.
Is that kind of the jist of it?
Thanks,
Pauline
 
Marilyn de Queiroz
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it, Pauline.
Print when possible instead of concatenating and passing String references.
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you program Java-4a the wrong way and get nit-picked to do it the right way you will undoubtedly notice the difference in performance when you get done.
It�s worth experimenting with String object concatenating and StringBuffer.append() just to see how much faster the application runs when you use the print as you go technique. For such a small application it kind of shocked me.
I learned a lot from the Cattle Drive exercise and this thread is outstanding.
Mike
 
On top of spaghetti all covered in cheese, there was this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic