This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hello, The automatic cast from float to int takes only place for first parameter. Why not for the second, too? I get this error:Why? Why don't I get the error before? Thomas [ edited to make code and error message more readable -ds ] [ January 15, 2003: Message edited by: Dirk Schreckmann ]
If I understand your question correctly, you are asking why: float x = 1; is ok. int y = 1.0F is not. The reason is you can upcast implicitly. But when you want to downcast, your have to be explicitly. So, int y= (int)1.0F will work.
The above line doesn't do any automatic conversion. The variable y is declared as a float and the F indicates that the literal is a float, also.
This comparison DOES have an automatic conversion. The variable x is converted to a float so that operation can occur. When an the orpands for an operator have different types, the larger (more bits) type is usually used.
Here you are trying to assign a float literal to an int variable. Since int uses less bits than float, the compiler will complain. There is no automatic conversion from int to float, only from float to int.
If you can fix the above problem, this line will once again convert the int variable to a float.
Note that the == operator doesn't compare obects. It compares object references. The expression ref1 == ref2 only returns true if ref1 and ref2 both refer to the exact same object. Depending on how it's actually implemented, the equals method likely determines equality by considering the states of the objects in question.