wells101 / PSET1

0 stars 0 forks source link

Use StringBuilder #1

Open mpmenne opened 9 years ago

mpmenne commented 9 years ago

Hey Adam,

Great looking code. Nice job with the first pset.

My biggest recommendation would be to watch the videos about String again.
http://tv.launchcode.us/#/videos/java_strings_performance?lesson=Java

Since Strings are immutable, string1 + string2 = string3... we end up with three strings total. For a small application like this it's not bad, but what if your pyramid was 1,000,000 levels high; that would lead to a lot of wasted String objects and garbage collection. So use StringBuilder. It helps with that.

Cheers, Mike

wells101 commented 9 years ago

Actually, i'm goign to correct myself and probably disagree with you Mike.

To my knowledge, printing to the console isn't creating string objects, since i'm using literals instead. Now, if this isn't the case (entirely possible!) the solution to reducing the number of objects created is to go to character literals inside the printn methods. this eliminates all objects entirely, as I've now used primitives instead (okay, technically objects, but much more memory-friendly!)