This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

casting

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the advantage of casting
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting allows you to use functionality not present in an interface but present in an implementation. I am refering to an 'interface' in the object-oriented sense, not as a class with that keyword in the declaration. You use it to gain more control of the finer details of a class (which is not always a good thing). Example:
...
URL url = new URL("http://site/");
URLConnection uc = url.openConnection();
// since I know this is an http connection,
// I'll use http specific methods not available
// in a normal URLConnection
HttpURLConnection huc = (HttpURLConnection) huc;
// I'm posting an html form. You can't do this
// with ftp or file protocols.
huc.setRequestMethod("POST");
...
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on what YOU mean by casting. Casting is mearly the conversion of one reference-type to another. The advantage is that you get your program to work.
Look at the following example. Child extends super.

Did that enlighten you?
/Mike
 
Amir Abbas Mooman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amir Abbas Mooman:
what is the advantage of casting


Sir i still not understand this
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are looking for
The advantages of Type Casting.
Suppose
If you want to the value of int data type variable assigned into the short data type variable. You will get an error.
Offcourse (In general life) its impossible to store full volume of water filled in a jug into a glass. Whereas we can store the glass water into the jug.

But with type casting it is made possible to assign the values of different type of variables to each other. The larger type to the smaller or smaller to larger. (int to short or int to long, byte to short or short to byte, vice versa) However we may loose some information, like if we pour the water of jug in a glass (if we want to assign the larger value into the smaller type variable).

Read page 43 of Khalid Mughal's book for beginners.


------------------
 
reply
    Bookmark Topic Watch Topic
  • New Topic