Sign in to follow this  
Followers 0
BLuFeNiX

[JAVA] type casting VS new-line initialization

5 posts in this topic

In Java (as well as C++ an most other high-level languages) there is a feature called "type-casting" in which a variable of one type (double, float, etc) can be represented as another type (int, long, etc), or vice-versa. My question is: Which of the following is more efficient code, and why?

int a;
int b;
double c;
...
a = b = (int) (c = 0);

int a;
int b;
double c;
...
a = b = 0;
c = 0;

I predict that the second can be executed faster because initializing variables on the same line probably doesn't speed it up in the first place, but I'd just like to know everyone's thoughts. Thanks.

Share this post


Link to post
Share on other sites



None of the above:

int a = 0, b = 0;
double c = 0.0;
Always initialize your variables when you declare them for best performance. For non-trivial types this will almost always be faster as the object is constructed in place. Code like you show involves a construction and a copy for non-trivial types. For native types the compiler can optimize away the copy but for non-trivial types it can't. However, you should stay in the habit of doing proper initialization even for native types because it's a good habit to have.

Also, learn RAII.

All this is spoken from experience with C++. I suspect the same rules apply with Java.

Share this post


Link to post
Share on other sites

Variables in the Java VM are all "reference" variables. They start initialized to (null) and an assignment on one line or the next is the same cost. It's a little weird.

Share this post


Link to post
Share on other sites

None of the above:

int a = 0, b = 0;
double c = 0.0;
Always initialize your variables when you declare them for best performance. For non-trivial types this will almost always be faster as the object is constructed in place. Code like you show involves a construction and a copy for non-trivial types. For native types the compiler can optimize away the copy but for non-trivial types it can't. However, you should stay in the habit of doing proper initialization even for native types because it's a good habit to have.

Also, learn RAII.

All this is spoken from experience with C++. I suspect the same rules apply with Java.

Yes, I know it is best to initialize with the needed variables, but in some cases you need a variable to be global, and you may need to reassign it later. I showed a break in code with an ellipse, but I probably should have made that clearer. Thank you for the advice.

Variables in the Java VM are all "reference" variables. They start initialized to (null) and an assignment on one line or the next is the same cost. It's a little weird.

Thank you, also.

Share this post


Link to post
Share on other sites

...but in some cases you need a variable to be global...

No you don't.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0