• Post Reply Bookmark Topic Watch Topic
  • New Topic

What do You Mean By Casting?  RSS feed

 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Casting Really does!! Means if you convert int to float or to double ,what changes happens in memory ,the data allocated for int is transferred from to the memory allocated for float and the memory allocated for int is destroyed ??? Can someone guide me please!! (What's Happening in Memory)

ThankYou
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A typecast changes the type of a value to another type. It doesn't change the value in memory, but the type of value.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the memory allocated for the int is not destroyed. When you cast from int to float (or double), the value of the int is converted to the format for a float or double. Nothing happens to the original int value.

When you cast reference types (not primitive types), then not much at all happens. The cast is then just a way to tell the compiler: "look, I know that this variable refers to an object of type X, so I want you to treat it that way". It doesn't do any automatic conversion of objects. It does, however, at runtime perform a check to see if the variable really refers to an object of type X, and if it doesn't, you get a ClassCastException.
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's when you do a narrowing primitive conversion that you are most likely to change the values
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on the change in format Jesper mentioned: An int and a float both require 32 bits, but an int is stored as a 32-bit signed two's complement integer, and a float is a single-precision 32-bit IEEE 754 floating point. Therefore, as detailed under JLS - 5.1.2 Widening Primitive Conversion...
Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (ยง4.2.4).
 
M Khalid
Ranch Hand
Posts: 57
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting lets you convert a value from one type to another.
Value can be primitive or a reference object.
Casts can be implicit or explicit.

An implicit cast means you don't have to write
code for the cast; the conversion happens automatically.



 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!