You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch3_javadatatypes.ptx
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -294,13 +294,13 @@ public class TempConv {
294
294
</p>
295
295
296
296
<p>
297
-
Implicit typecasting happens automatically when converting a value from a smaller data type to a larger one, as there is no risk of losing information. For example, you can assign an int to a double without any special syntax.
297
+
Implicit typecasting happens automatically when converting a value from a smaller data type to a larger one, as there is no risk of losing information. For example, you can assign an int to a double without any special syntax as shown in <xrefref="implicit-typecasting"text="type-global"/>.
298
298
</p>
299
299
300
300
<listingxml:id="implicit-typecasting">
301
301
<programinteractive="activecode"language="java">
302
302
<code>
303
-
void main() {
303
+
void main() {
304
304
int myInt = 10;
305
305
double myDouble = myInt; // Automatic casting from int to double
306
306
@@ -311,7 +311,7 @@ public class TempConv {
311
311
</listing>
312
312
313
313
<p>
314
-
Explicit typecasting is required when converting from a larger data type to a smaller one, as you might lose data. You must do this manually by placing the target type in parentheses () before the value.
314
+
Explicit typecasting is required when converting from a larger data type to a smaller one, as you might lose data. You must do this manually by placing the target type in parentheses () before the value as shown in <xrefref="explicit-typecasting"text="type-global"/>.
315
315
</p>
316
316
<listingxml:id="explicit-typecasting">
317
317
<programinteractive="activecode"language="java">
@@ -330,7 +330,7 @@ void main() {
330
330
Besides primitive types, type casting is also a fundamental concept when working with objects, especially within an inheritance hierarchy. This involves converting an object reference from one class type to another, typically between a superclass and a subclass. This is often referred to as upcasting and downcasting.
331
331
</p>
332
332
<p>
333
-
Let's imagine we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
333
+
In <xrefref="animal-dog-example"text="type-global"/>, we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
334
334
</p>
335
335
336
336
<listingxml:id="animal-dog-example">
@@ -353,7 +353,7 @@ class Dog extends Animal {
353
353
</listing>
354
354
355
355
<p>
356
-
<strong>Upcasting (Implicit):</strong> Upcasting is casting a subclass instance to a superclass reference type. This is always safe because a subclass object is guaranteed to have all the methods and properties of its superclass. Therefore, upcasting is done implicitly by the compiler.
356
+
<strong>Upcasting (Implicit):</strong> Upcasting is casting a subclass instance to a superclass reference type. This is always safe because a subclass object is guaranteed to have all the methods and properties of its superclass. Therefore, upcasting is done implicitly by the compiler as <xrefref="upcasting-example"text="type-global"/>.
357
357
</p>
358
358
<listingxml:id="upcasting-example">
359
359
<programinteractive="activecode"language="java">
@@ -388,7 +388,7 @@ void main() {
388
388
<strong>Downcasting (Explicit):</strong> Downcasting is casting a superclass reference back to its original subclass type. This is potentially unsafe because the superclass reference might not actually point to an object of the target subclass. You must perform an explicit cast. If you cast to the wrong type, Java will throw a <c>ClassCastException</c> at runtime.
389
389
</p>
390
390
<p>
391
-
To safely downcast, you should first check the object's type using the <c>instanceof</c> operator.
391
+
To safely downcast, you should first check the object's type using the <c>instanceof</c> operator as shown in <xrefref="downcasting-example"text="type-global"/>.
392
392
</p>
393
393
<listingxml:id="downcasting-example">
394
394
<programinteractive="activecode"language="java">
@@ -421,7 +421,7 @@ void main() {
421
421
</listing>
422
422
423
423
<p>
424
-
In this example, we first create a <c>Dog</c> object and assign it to an <c>Animal</c> reference (upcasting). Then, we check if the <c>Animal</c> reference is actually pointing to a <c>Dog</c> object before downcasting it back to a <c>Dog</c> reference.
424
+
In <xrefref="downcasting-example"text="type-global"/>, we first create a <c>Dog</c> object and assign it to an <c>Animal</c> reference (upcasting). Then, we check if the <c>Animal</c> reference is actually pointing to a <c>Dog</c> object before downcasting it back to a <c>Dog</c> reference.
0 commit comments