0% found this document useful (0 votes)
40 views2 pages

Typecasting Java

java features

Uploaded by

Mohan Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Typecasting Java

java features

Uploaded by

Mohan Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

DATATYPES:

------------
byte,short,int,long (without decimal)
float,double (with decimal)
boolean
ORDER:
------
byte < short < int < long < float < double

TYPE CASTING IN JAVA


--------------------
1) Primitive Casting : convert data from one primitive data type to another prim
itive data type

(i)Auto Widening -converting data from small sized data type to big s
ized data type
Example
-------
byte b = 10;
short s = b;
(ii)Explicit Narrowing -converting data from big sized data type to small s
ized data type
Example
-------
double d = 10.25;
float f = (float) d;
2) Derived Casting -change the type of object from one user defined data type to
another user
(i) Auto-up casting (child to parent)
change the type of object from sub class type to super class type.
(ii) Explicit-down casting (parent to child)
change the type of object from super class type to sub class type.

CLASS-CAST EXCEPTION :
----------------------
-> object is automatically upcasted to its super class type
================================================================================
==============================================================
Wrapper class: ([Link] package)
--------------
boxing - primitive to object
----------------------------
old
===
byte b = 10;
Byte B = new Byte(b);
new
===
after 1.5 (auto boxing)
byte b = 10;
Byte B = b;
unboxing -object to primitive (xxxValue())
-------------------------------------------
old
====
Byte B = new Byte((byte) 10);
byte b = [Link]();
new
====
after 1.5 (auto unboxing)
Byte B = new Byte((byte) 10);
byte b = B;

You might also like