• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

when to use casting

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
PHP C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jaspreet manchinda wrote:what do casting actually do and when can we use object type casting


Polymorphism
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope you found the other discussion helpful.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was indeed
this is really a good place to be very helpfull
reply
    Bookmark Topic Watch Topic
  • New Topic