• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to use a long  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im currently going through various videos and learning Java through some exercises
The latest exercise is regarding using a 'long' to calculate out lightspeed (186282 miles per sec) after 300 mins.

The way I did it, is as follows:

public static void main(String[] args) {

                long milesPerSec = 186282L;

long lightSpeed = milesPerSec * 60;

System.out.println("\n\tA lightbeam will travel " + lightSpeed * 300 + " miles after 300 minutes!");

The way the tutor did it, is as follows:

public static void main(String[] args) {

              long distance;
              int minutes = 300;

              distance = 186282L * minutes * 60;

              System.out.println("The distance travelled is " + distance + " miles.");

The query I have is, am I using too much memory than the tutor doing this exercise my way, or
does it matter, as in, are they both usin the same amount of memory?

 
Ranch Hand
Posts: 200
3
Java MySQL Database Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are taking more memory by taking 2 long values.

long takes 8 bytes and int takes 4 bytes.

You are taking 2 values values total of 16 bytes but your tutor takes 1 long as well as 1 int which sums up 12 bytes.

But that hardly matters .
 
Rick Maver
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O Shea wrote:Yes you are taking more memory by taking 2 long values.

long takes 8 bytes and int takes 4 bytes.

You are taking 2 values values total of 16 bytes but your tutor takes 1 long as well as 1 int which sums up 12 bytes.

But that hardly matters .


thanks O Shea, I thought thats what I was doing, cheers.
 
Bartender
Posts: 572
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the compiler may optimize out the use of the intermediate variable, especially if it is not used again elsewhere, leaving the size the same for both approaches.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your solution is fine and does not use more memory than the tutor's solution.

Don't worry about "efficiency" and memory use for now. I see lots of beginners being too worried about their program not being "efficient" enough. That's the wrong focus for a beginner. Computers are superfast and have billions of bytes of memory. The first thing that is important is that you learn the syntax of Java and that you learn how to write small programs, and then when you are writing larger programs, you learn how to organize and design your code so that it remains understandable for human beings. Don't worry about efficiency, performance and optimization until much later.
 
Rick Maver
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Your solution is fine and does not use more memory than the tutor's solution.

Don't worry about "efficiency" and memory use for now. I see lots of beginners being too worried about their program not being "efficient" enough. That's the wrong focus for a beginner. Computers are superfast and have billions of bytes of memory. The first thing that is important is that you learn the syntax of Java and that you learn how to write small programs, and then when you are writing larger programs, you learn how to organize and design your code so that it remains understandable for human beings. Don't worry about efficiency, performance and optimization until much later.



Slowly but surely im finding out that code design is an integral part of the learning curve, and im beginning to realsie how vital it is to practice the basics and then building upon them to create more in depth programs. Thank you for your input.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about efficiency, but I don't like yours as much due to variable names.

milesPerSec  - this is so-so - it's not clear WHAT is travelling at this speed, but ok...it's a speed, for something

lightSpeed  - ummm..isn't the above variable lightSpeed?  How is this different than what we already have?  After all, lightSpeed can be reported in many ways...MPH, KPH, feet per second, etc.  In a short program like this, it may not be such a big deal. but if you have a long program with parameters being passed around, you want to be able to know exactly what a variable holds by the name along, not by digging back through the code.

Of course, your tutor's version has a "magic number" - two, in fact.  I have no idea where 186282 comes from, or why it's used. The same with 60.  Unless you know what the first is, the second makes NO sense. Variable names are cheap. I'd probably do something like



Yes, I still have a magic number, but it's more obvious where that 60 comes from...I'd probably debate if a variable of "secondsPerMinute" would help or not...it would give us this:



What I like about this last is it shows you all the units for all your numbers, and you can see what units cancel.  Seconds cancel out and minutes cancel out, leaving only miles.
 
Ranch Foreman
Posts: 919
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There could be differences in how the code is compiled depending on which operating system and what version you have of the java compiler versus what I have. However you can do some tests locally on your system if you choose to for instance:

  • You can get the final size of the class file, then change from long to int and get the final size again. You may see a difference that you didn't expect to see. Maybe the file size will be different by a few bytes, maybe it will be the same, it all depends. When I created a small program I was supersized the the "byte" version of a for loop counter was larger then the "int" version as discussed here https://coderanch.com/t/682659/java/Odd-byte-int
  • If you want to go and do more advance inspections of the class file then you can use javap https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javap.html. However javap is not really a tool that beginners need to worry about.
  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!