aspose file tools*
The moose likes Beginning Java and the fly likes when to use casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "when to use casting " Watch "when to use casting " New topic
Author

when to use casting

jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 67
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

Joined: Nov 15, 2011
Posts: 47

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


Live Curious!!!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Polymorphism
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Please search this forum; there is another thread about casting active today.
jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 67
Campbell Ritchie wrote:Please search this forum; there is another thread about casting active today.

i already did that
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
I hope you found the other discussion helpful.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 27, 2012
Posts: 67
It was indeed
this is really a good place to be very helpfull
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to use casting