Early Access Courses. Assessments New. Free Trial New. For Business. For Educators. Become an Affiliate. Terms of Service. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Last Updated : 27 Sep, The results are int anyway. This works in the same way, but now there is an implicit cast to double.
This conversion from int to double is an example of a widening primitive conversion. Converting to double can be done by casting an int to a double:. Alternatively you can use a hard-parameter of type double 1. I think you should casting variable or use Integer class by call out method doubleValue. Either use casting as others have already said, or multiply one of the int variables by 1. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Improve this question. Milo Lu Milo Lu 2, 1 1 gold badge 31 31 silver badges 40 40 bronze badges. The cast to double emphasizes the conversion from int, which is good for anybody reading the code.. Unless this is a throwaway example, you should just declare the variables as type double. That makes the code self-documenting. Yes, it is ugly, but so is a cast. They're supposed to be ugly so they call people's attention to them.
In fact, they are usually free. Assuming an x86 architecture, for this specific piece of code a compiler would generate code to load an integer to the TOS, instead of a floating point value fild vs. No penalty at all. Show 8 more comments. Active Oldest Votes. You can multiply by 1. Improve this answer. Kyle 2 2 gold badges 12 12 silver badges 22 22 bronze badges.
If something is questionable "most compilers " you should not do it. When somebody concerned such thin details he need to 1. Rewriting, what is meant to be a cast to a meaningless multiplication is a really poor suggestion.
Don't do this. Use a cast, when you need to convert between types. Hard coding number without to change it to named constands is bad pattern at all.
0コメント