wood burning stoves 2.0*
The moose likes Java in General and the fly likes type cast or class object methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "type cast or class object methods" Watch "type cast or class object methods" New topic
Author

type cast or class object methods

Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

Hello experts !

#1 I was wondering. What is the best practice of going from one a primitive type to a primitive type.

For example, if I have this piece of code :

byte num = Math.random() * 10

we all know that random() returns a double, and I want this number to be converted to a byte.

what is the best practice in this case, and which of this approaches has the best performance in terms of memory usage and time to execute the instruction ?

1) Using type casting, as in byte num = (byte) (Math.random() * 10)

2) using a class object method :
Double x1 = new Double ( 10.0 ) ;
byte num = x1.byteValue ( );

Thank you for all your help and support,

kind regards,
marius

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Marius Constantin wrote:Hello experts !

#1 I was wondering. What is the best practice of going from one a primitive type to a primitive type.

For example, if I have this piece of code :

byte num = Math.random() * 10

we all know that random() returns a double, and I want this number to be converted to a byte.


Well, for starters, I wouldn't use Math.random(). I'd use the java.util.Random class.

However, if you want to convert a double do a byte, just cast it.


Keep in mind, of course, the loss of precision and range.

which of this approaches has the best performance in terms of memory usage and time to execute the instruction ?


1) Using type casting, as in byte num = (byte) (Math.random() * 10)

2) using a class object method :
Double x1 = new Double ( 10.0 ) ;
byte num = x1.byteValue ( );


You'll never notice a difference. The cast will be slightly faster and use slightly less memory, but that's not why you'll choose it. You'll choose it because it's simpler and more directly expresses your intent.

Again, you'll never notice a performance difference in a real program.
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Not sure if I understand what you are trying to do or why.

You won't fit a double "as is" into a byte as there is not enough space. The getBytes method of Double will give you an array of byte (byte[]). If you are effectively trying to find a random number that fits into a byte precision, then you'll need to come up with a way to limit the range of the random to fit into a byte.

You may also be able to just pick one of the elements of the byte array, depending on what you are trying to do this may be "random" enough.
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62


Thank you very much Jeff for all the explanations !

Jeff Verdegan wrote:

Well, for starters, I wouldn't use Math.random(). I'd use the java.util.Random class.



Why do you say this ? I took a look at both Math.random() and ( new Random() ).nextDouble, but since this doc http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#random%28%29 is full or contradictory information and too abstract, and being a greenhorn doesn't help either, I can only see that Random class is more complex than Math class, and since it implements Knuth's theories, it can't get better than this. but what's the true story ?

thank you so so much,

kind regards,
marius
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Marius Constantin wrote:
Thank you very much Jeff for all the explanations !


You're very welcome.

Jeff Verdegan wrote:

Well, for starters, I wouldn't use Math.random(). I'd use the java.util.Random class.



Why do you say this ?

Math.random() is the old way. The Random class was introduced later. It provides methods that do a lot of the grunt work for you if you want, say, a random int in a given range, so it's easier to use. I think it also gives a better distribution than just doing the scaling yourself. And finally, if you read the docs for Math.random(), you'll see that it uses Random anyway.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
What is contradictory about java.util.Random? I can see nothing contradictory, but I can see much that is difficult to understand.
You ought not to use new Random().nextXXX(), butYou ought to check the documentation carefully for the range of values which are returned.
Marius Constantin
Ranch Hand

Joined: Nov 23, 2011
Posts: 62

@ Jeff Verdegan and @ Campbell Ritchie : Thank you so so much for everything. it's Very clear now !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: type cast or class object methods
 
Similar Threads
java.lang.Void and void ??
casting int to Integer
help with this q
Graphs and identifying adjacent nodes
Difference between int and Integer