• Post Reply Bookmark Topic Watch Topic
  • New Topic

multiplying Longs  RSS feed

 
Nancy Dernell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you multiply two values that are Longs?
[javac] Compiling 18 source files to C:\ndernel\work\bpc\build\classes
[javac] C:\ndernel\work\bpc\src\com\mckesson\bpc\eo\OPatCharge.java:79: oper
ator * cannot be applied to java.lang.Long,java.lang.Long
[javac] return getAmount() * getQuantity();
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks from your error that you are using Long objects instead of the long primitive. Objects cannot be multiplied.
you should either just use two long values and then you can multiply them and store it into a long, or you can extract the long values from your Long objects by using your long object variable name.longValue()
Long is called a wrapper class because it wraps an object around the primitives
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I concur with the above post. Incidentally, a lot of people are surprised when they try to add/subtract/multiply/divide two literals and store it in a long and it doesn't work:

This will not compile because literals with decimal points are declared as doubles by the JVM. So, there are two fixes, either of which will work:

The first way is preferable, especially if you're performing a long string of calculations because it will carry out all of those calculations to the level of precision allowed by doubles, only rounding off precision at the last moment before assigning the result to x. In the second way, each operand is cast to a long and loses the precision of the double, so the loss-of-precision errors can compound and end up throwing off the end value. (This is usually only significant if you're working with a very long list, or at the limits of long precision--in which case you ought to be using doubles anyway.)
Another thing that always shocks people is when they run this code the first time:

You'd think the result would be 1.5. It's not...it's 1.0. That's because when you use literals without decimal points, the compiler assumes they're ints. Dividing two ints yields an int result--any fractional portion is simply dropped, so 1.5 becomes 1. As long as one or both of the operands is marked as a double (or a float, but that would be silly seeing as how this code stores the result into a double anyway), things work out.
As it happens there's lots of ways to indicate this:

The last option of each block is the most readable of that block (I think), so therefore the best way. When it comes to appending a type suffix to literals, I usually use small case D's for doubles because a capital D can look a lot like a zero if you're not paying attention. Along the same vein, to indicate a long literal I usually will use capital L instead of small l because a small l looks a lot like a one. Either case of B and F work (for bytes and floats, respectively), while I only would use small i to denote an integer (again, capital I looks like 1).
That's if I'm using those literal suffixes--because of the rules governing these primitive types, there is rarely if ever a need to specify bytes, ints, or doubles with these suffixes so I have only ever found occasion to append L's and d's, and I prefer using ".0" instead of "d" anyway, so I really only ever use "L".
Hey, I didn't say this post would be fascinating.
sev
 
Nancy Dernell
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - that helped alot. Then I got to figure out how come I couldn't just return that value back - (had to create a new Long to do that)
public Long getExtAmount() {
long myAmount = getAmount().longValue();
long myQuantity = getQuantity().longValue();
return new Long(myAmount * myQuantity);
}
Thanks again.
Nancy
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B]
This will not compile because literals with decimal points are declared as doubles by the JVM. So, there are two fixes, either of which will work:[/B]
I'd argue that there's a third fix which in most cases is even better, when it's possible: change the type of x to double. If it's got values with decimal points in them, it probably shouldn't be an integer type. There are certainly cases where you don't have a choice, but if you can change the data type, it should probably be considered.
[ March 02, 2004: Message edited by: Jim Yingst ]
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nancy,
Perhaps this is what you're after, using primitive long as the return type instead of a Long object?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!