-1

I investigated a lot about this but I didn't find an answer that convinces me; for that reason I decided to ask it here.

When you do casting, what occurs internally with the compiler? For example:

double b= 5.67
a = (int) b

Does java convert the type of the variable or converts the internal value of?

rghome
  • 8,529
  • 8
  • 43
  • 62
  • 4
    What exactly do you mean by "convert the type of the variable"? What observable difference are you asking about? – Jon Skeet Jan 13 '23 at 12:18
  • 1
    See https://docs.oracle.com/javase/specs/jls/se17/html/jls-5.html#jls-5.1.3 for the steps that the conversion goes through... – Jon Skeet Jan 13 '23 at 12:19
  • I ve read it. But thats why im asking here. I don't know if the type of the variable changes or the inside value of the variable. I read even in some post and says the type of the value changes and in other post says the type of variable changes. So.... XD im confused – albertocp05 Jan 13 '23 at 12:22
  • `b` obviously remains a `double` and doesn't change its type. `a` keeps its type too and is assigned an integer value – knittl Jan 13 '23 at 12:25
  • 2
    I'm not sure this is an exact duplicate (https://stackoverflow.com/questions/676503/what-does-casting-do-at-compiler-machine-level) but it contains a lot of small pieces you may find interesting. – Federico klez Culloca Jan 13 '23 at 12:25
  • 1
    The cast does not change `b` in any way. `a` is assigned the value resulting from the evaluation of the expression `(int) b`, which is described by the [JLS chapter "5.1.3. Narrowing Primitive Conversion"](https://docs.oracle.com/javase/specs/jls/se19/html/jls-5.html#jls-5.1.3) already linked to – Hulk Jan 13 '23 at 12:27
  • What type is `a`? – k314159 Jan 13 '23 at 12:49
  • Assuming `a` is an `int`, it will convert the bit pattern stored in `b`, which is `0100000000010110101011100001010001111010111000010100011110101110`, into the bit pattern for 5, which is `00000000000000000000000000000101`, and store that in `a`. – k314159 Jan 13 '23 at 13:02
  • "and in other post says the type of variable changes" - where? Variables *never* change their type. If you're confused by different materials, you should link to those materials rather than asking a somewhat vague question. – Jon Skeet Jan 13 '23 at 13:15

1 Answers1

1

Normally, the Java compiler will prevent you from assigning primitives that might result in a loss of data. You don't make it clear, but I am assuming that a is an int. Assigning a double to an int can result in information being lost, so the compiler objects.

If you add a cast the compiler ignores the issue and adds code to do whatever is necessary to convert the value from one type to the value of the other (in this case, assuming a is an int, simply truncating the double in many cases) and ignores any data loss. The type of the variables remains unchanged. Only the value changes.

There is a bit more to it than that for certain values. For example, if the double has the special value NaN (not a number) it is converted to int 0. See in the Java Specification 5.1.3. Narrowing Primitive Conversion.

rghome
  • 8,529
  • 8
  • 43
  • 62