Type Casting / Type Conversion
Converting the values of one type to another for the evaluation of an
expression is called type casting or type conversion.
Type casting is of two types:-
1) Upcasting – Conversion of lower types to upper types is called upcasting. Upcasting is implicitly performed.
Upcasting is performed according to the following graph:-
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivMMJp_Wcb8M7dOH1jUeWB57tEu-vHTuzI_yOO2kfNE83ZOdhzSri4a1D0gvb28WxAjKXtnTpYAVT5LLEikbKc4TtVYye0BCIxutRPYz71JtUVnFi-Qg8wcVstKZu3-OKuBLhwJ4lA3oRs/s1600/uptuexam_core_java.png)
Explanation -
a.
Let n be an
integer and n = 2n then to store n in memory in integer
representation k bits are required.
For e.g
32768 = 215
0 111111111111111
15 bits are required to store 32768.
b.
In floating
point representation number is not stored rather its exponent is stored.
i.e
32768 = 215
Then only 15 need to be stored.
n=2k and k = 2l where l<k then to store n only l
bits are required.
2) Downcasting – Conversion of higher types to lower type is called downcasting. Downcasting need to be explicitly performed.
Syntax –
(target type) expression;
Example- float f = 12.45; ==========> float f = (float)12.45; OR float f = 12.45f;
By this statement compiler
- PROGRAM – Rectangle.java
- public class Rectangle
- {
- private int l, b;
- public void Rectangle(int x, int y)
- {
- l = x;
- b = y;
- }
- public void display()
- {
- System.out.println("Length = " + l);
- System.out.println("Breadth = " + b);
- }
- public int area()
- {
- return l * b;
- }
- public void setDimension(int x, int y)
- {
- l = x;
- b = y;
- }
- }
- Syntax of creating object of a class-
- className ReferenceVariable = new className();
- OR
- className ReferenceVariable;
- ReferenceVariable = new className();
Reference Variable – is an implicit pointer that contains the reference of a class object. In Java, objects are dynamically created. Hence, they don’t have names. They are referred using the name of their reference variable.
In C++ :-
Rectangle r;
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpSuWZ4VlgJ3NBzkPOURQ-7xj9ZxYOKJoeQ3n4D8msU5mg4vTc8-Q_l9VjbQCcCzgl2_zObIunZBrUAs3QmyBkiPmGniJhvyqBr5V3tTTmXIp8VcNvuoB9pAMKIvCkgmVuqbBs0g20dMFR/s1600/uptuexam_core_java.png)
Traditionally in languages when we write statement
int a=5, b=6, c=10;
a table is created during compilation.
Variable Name
|
Type
|
Address
|
Value
|
Scope
|
a
|
int
|
0 + 1000
|
5
|
main
|
B
|
int
|
2 + 1000
|
6
|
main
|
c
|
int
|
4 + 1000
|
10
|
main
|
int * p;
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEitGzsVkscePcNoaE2dBDScF8N5oanQY_9Qx7zmOBiLEHiLP3z-sMZeC-VBMurOT486czuEfIj0q-f9p_Xu4h_SWgWILXiBai60EEoTyNHMlEP-ZDhz7fNSfG4diNV20QFliRs7yCLsKk_K/s1600/uptuexam_core_java.png)
Address & Reference denotes two different things.
o Address represents actual memory location whereas Reference represents a mechanism of finding out objects.
Different JRE provides different implementations of references.
o In Microsft JRE, References are implemented as pointers that is they actually holds the address of object.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqCSCu0yGFpvenrjEFoWLzJGiAmKOPBzt-Dbg84X1bOMXxNhBPveOW6tn0290kHi67-u-Bt_yFpLN4uU6XdXBA2iwR-0Ftfm0s1UqjUHRNgZx7uwIWnrRIFcyyocsuLCKa3ZCmucheOJfm/s1600/uptuexam_core_java.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJzCZcvSWxR_TA1YlCbpS-HqwjGp__VCnnVGFzS-3qGJunXqakkZQK7N4-whgA4JKgY5Anp0ClmEZxQdLX9C4VtCMIVZthkTfN7Aq2Yxh6FYFBXitOrsiZvTWIQffNMyAd8v8idyMQIMlf/s1600/uptuexam_core_java.png)
o In Sun Microsystem JRE, Reference Variables are implemented as pointer to pointer.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirFHg8Ow9ZG4aWqcA4119u8pWJ01OEYskRbvn-zy9JTq5lSYkJ0wvKjywWq5VRodf697rqSbF7tp4r2vYB5zhCzRaXHj4IIgSC7Pjt-fFz0pReEVmapw6cAscVOr8Q-fxAycRoFys3IrOX/s1600/uptuexam_core_java.png)
In this implementation, Reference and Address are different.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglGPuB5mIqLPkwE2XFK8oJGk_zVJMaNpnqTe8zRH4heS-UvV3Wm7sJzyn8Hknnd-v_a-66lY6p_PE3OjvDpz764GYj8UxxGeXju8k_lbMto8FvxUyNaUVBCpJNQraiCWjSBaBkdPuJ_noT/s1600/uptuexam_core_java.png)
Now we will write a program RectTest.java to test the program Rectangle.java written above.
- public class RectTest
- {
- public static void main(String[] args)
- {
- Rectangle r = new Rectangle();
- r.l = 5;
- r.b = 7;
- r.setDimension(5, 4);
- System.out.println("Dimension of rectangle r ");
- r.display();
- System.out.println("Area of r = " + r.area());
- }
- }
Output –
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyN-HnuLbrcVtSNQmULfQ58xvrqsNaRGTziT3Ll4noilydKjHNsijkCUJSC30O1uY8CNcCSosQQhqBWpUVwFOcDRm0-1Fl-KzWoQ0kMk7C8qx2hSkj2iJ-EOpJYsxV4QzVZSM2gok9QuCl/s1600/uptuexam_core_java.png)
Since variables l and b are declared private so we cannot access it from outside.
Now the same program RectTest.java again.
- PROGRAM RectTest.java
- public class RectTest
- {
- public static void main(String[] args)
- {
- Rectangle r = new Rectangle();
- //r.l = 5; // Invalid statement
- //r.b = 7; // Invalid statement
- r.setDimension(5, 4);
- System.out.println("Dimension of rectangle r ");
- r.display();
- System.out.println("Area of r = " + r.area());
- }
- }
OUTPUT –
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgk2-hnNkBc6hVrYHp-WoF4F3ni8toK3yVvZF6mjzClf2VdKQ53bZIXvm8YnMrMvAUF5GKHntVgLLTr1VgtUM11EjxdH8NDK5YAN_2soPCIH_iBxxUgDoZV0dZKanGWrvk18EEb_h97Qpoc/s1600/uptuexam_core_java.png)
No comments:
Post a Comment