• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to use casting

 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do casting actually do and when can we use object type casting i googled it but did not get any proper explanation
 
Sagar Dabas
Ranch Hand
Posts: 47
C++ Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example :

Now you can use g2d to call the methods of both Graphics and Graphics2D.
But the g can only call methods of Graphics class
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaspreet manchinda wrote:what do casting actually do and when can we use object type casting

Polymorphism
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cast your reference types as little as possible. You need to be sure you can perform the cast (which you may need an instanceof test), otherwise you may suffer an Exception. If the compiler thinks the cast is impossible, it will throw an error.
You should design your classes polymorphically so casting is unnecessary.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaspreet manchinda wrote:what do casting actually do

It changes the way the language 'sees' an object, generally from a less specific to a more specfic type.

and when can we use object type casting

As Campbell said: as little as possible. With generics you can almost eliminate the need to do it at all.

The biggest remaining exception is in equals() methods:
Because the method takes an Object, it's usually necessary to cast it to the type being compared with, once you've established that it is, in fact, the one you want. So, for example:HIH

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search this forum; there is another thread about casting active today.
 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please search this forum; there is another thread about casting active today.

i already did that
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you found the other discussion helpful.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaspreet manchinda wrote:what do casting actually do


It tells the compiler and the runtime, "Even though you know of this reference as type X, I want you to treat it as if it were of type Y."
 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was indeed
this is really a good place to be very helpfull
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic