• 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
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

So, which of these would be...

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I just concluded the 'Introduction to Java Applications' chapter in 'Java How to Program'. I'd probally be punching my computer cause of those damned environmental variables if not for the kind people here.

Anyways, one of the exercises in the end of the chapter is to put together an app that collects two numbers, then give the; sum, difference, product and quotient.

I put this together and it worked thankfully:


After it ran, I was wondering how I could shorten the length of the code, so I tried this:


I tried it out, and it worked. YAY ME, but I was wondering, which is more efficient.

I concluded the second one because there is no need to store the four solutions. As a result the first one ties up memory that could otherwise be used one something else. Am I right in this assumption? I know this is a meaningless app, and memory use means nothing for it. Just curiosity.

Thanks for your time =)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does not really make any difference. In the last line, where you print all the results, Java will have to store the results of the calculations somewhere, so that they can be passed to the System.out.println method. Whether you store the results in named variables, or let Java store them somewhere, doesn't really make any difference at all for memory usage.

Don't worry too much about memory and performance optimization, especially if you're just beginning to learn Java.

Optimizing code is often much less intuitive than people think, because the compiler and the JVM do a lot of sophisticated optimizations below the covers, and sometimes people think they optimize things by changing their code, while they're really not, because they unknowingly make changes that make it harder for the compiler or the JVM to automatically optimize things. The only way to improve the memory or speed of a program is to use tools like profilers to measure where the bottleneck in the program is, try to improve that part of the code, re-run, measure again etc. Optimizing by just inspecting the source code and reasoning about it often doesn't work.
 
Marshal
Posts: 75844
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Jesper has implied, the real difference is that by not storing the results with identifiers (local variables), you are losing the values and cannot use them again.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other consideration is ease of understanding. Which can be a matter of opinion, but I think in the first piece of code it's clearer what you're doing and why you're doing it, because the variable names have meaning.
 
pie. 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
reply
    Bookmark Topic Watch Topic
  • New Topic