Skip to content

Commit 7e349a0

Browse files
committed
added listing in paragraphs
1 parent 1089a3a commit 7e349a0

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ public class TempConv {
294294
</p>
295295

296296
<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 <xref ref="implicit-typecasting" text="type-global"/>.
298298
</p>
299299

300300
<listing xml:id="implicit-typecasting">
301301
<program interactive="activecode" language="java">
302302
<code>
303-
void main() {
303+
void main() {
304304
int myInt = 10;
305305
double myDouble = myInt; // Automatic casting from int to double
306306

@@ -311,7 +311,7 @@ public class TempConv {
311311
</listing>
312312

313313
<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 <xref ref="explicit-typecasting" text="type-global"/>.
315315
</p>
316316
<listing xml:id="explicit-typecasting">
317317
<program interactive="activecode" language="java">
@@ -330,7 +330,7 @@ void main() {
330330
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.
331331
</p>
332332
<p>
333-
Let's imagine we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
333+
In <xref ref="animal-dog-example" text="type-global"/>, we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
334334
</p>
335335

336336
<listing xml:id="animal-dog-example">
@@ -353,7 +353,7 @@ class Dog extends Animal {
353353
</listing>
354354

355355
<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 <xref ref="upcasting-example" text="type-global"/>.
357357
</p>
358358
<listing xml:id="upcasting-example">
359359
<program interactive="activecode" language="java">
@@ -388,7 +388,7 @@ void main() {
388388
<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.
389389
</p>
390390
<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 <xref ref="downcasting-example" text="type-global"/>.
392392
</p>
393393
<listing xml:id="downcasting-example">
394394
<program interactive="activecode" language="java">
@@ -421,7 +421,7 @@ void main() {
421421
</listing>
422422

423423
<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 <xref ref="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.
425425
</p>
426426
</section>
427427

0 commit comments

Comments
 (0)