dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Oct 31, 2018

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Object Casting in Java

In Java, objects can be cast explicitly and implicitly:

  • implicit casting happens when the source type extends or implements the target type (casting to a superclass or interface).
  • explicit casting has to be done when the source type is extended or implemented by the target type (casting to a subtype). This could produce a runtime exception (ClassCastException) if the object being cast is not of the target type.
Float floatVariable = new Float(89.0f);
Number n = floatVariable; //Implicit (Float implements Number)
Float secondfloatVariable = (Float) n; //Explicit
Double doubleVar = (Double) n; //Throws exception (the object is not Double)
Octavia Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date