Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Automatic Conversion / Cast

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Markl:

Why don't I get the error before?

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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

HTH
Layne
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, now I got it:
Comparison of primitive numbers with „==“ and
Objects like Integer or String with „==“ and „equals“.


C:\Java\EigeneJavaProgramme>java Test104
Equal1
Equal0
Equal2
Equal3A
Equal3A
Equal4
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a new question? If so, what are you asking?
[ January 16, 2003: Message edited by: Layne Lund ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robbie shi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
float x=1; it is ok
int x=1.0f; cant happen
Robbies
-----------------------------
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
http://www.pivotonic.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic