Skip to content

Commit 1089a3a

Browse files
committed
made blocks of code interactive
1 parent f9aea95 commit 1089a3a

1 file changed

Lines changed: 85 additions & 24 deletions

File tree

source/ch3_javadatatypes.ptx

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -296,27 +296,48 @@ public class TempConv {
296296
<p>
297297
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.
298298
</p>
299-
<pre>
300-
int myInt = 10;
301-
double myDouble = myInt; // Automatic casting from int to double
302-
</pre>
303-
299+
300+
<listing xml:id="implicit-typecasting">
301+
<program interactive="activecode" language="java">
302+
<code>
303+
void main() {
304+
int myInt = 10;
305+
double myDouble = myInt; // Automatic casting from int to double
306+
307+
System.out.println(myDouble);
308+
}
309+
</code>
310+
</program>
311+
</listing>
312+
304313
<p>
305314
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.
306315
</p>
307-
<pre>
308-
double originalDouble = 9.78;
309-
int castedInt = (int) originalDouble; // Explicitly casts double to int. The value of castedInt is now 9.
310-
</pre>
316+
<listing xml:id="explicit-typecasting">
317+
<program interactive="activecode" language="java">
318+
<code>
319+
void main() {
320+
double originalDouble = 9.78;
321+
int castedInt = (int) originalDouble; // Explicitly casts double to int. The value of castedInt is now 9.
322+
323+
System.out.println(castedInt);
324+
}
325+
</code>
326+
</program>
327+
</listing>
311328

312329
<p>
313330
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.
314331
</p>
315332
<p>
316333
Let's imagine we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
317334
</p>
318-
<pre>
335+
336+
<listing xml:id="animal-dog-example">
337+
<program language="java">
338+
<code>
319339
class Animal {
340+
320341
public void makeSound() {
321342
System.out.println("The animal makes a sound.");
322343
}
@@ -327,37 +348,77 @@ class Dog extends Animal {
327348
System.out.println("The dog barks!");
328349
}
329350
}
330-
</pre>
351+
</code>
352+
</program>
353+
</listing>
331354

332355
<p>
333356
<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.
334357
</p>
335-
<pre>
358+
<listing xml:id="upcasting-example">
359+
<program interactive="activecode" language="java">
360+
<code>
361+
class Animal {
362+
public void makeSound() {
363+
System.out.println("The animal makes a sound.");
364+
}
365+
}
366+
367+
class Dog extends Animal {
368+
public void bark() {
369+
System.out.println("The dog barks!");
370+
}
371+
}
372+
373+
void main() {
336374
// A Dog object is created, but the reference is of type Animal.
337375
// This is implicit upcasting.
338-
Animal myAnimal = new Dog();
376+
Animal myAnimal = new Dog();
339377

340-
myAnimal.makeSound(); // This is valid, as makeSound() is defined in Animal.
378+
myAnimal.makeSound(); // This is valid, as makeSound() is defined in Animal.
341379

342380
// myAnimal.bark(); // This would cause a compile-time error!
343381
// The compiler only knows about the methods in the Animal reference type.
344-
</pre>
382+
}
383+
</code>
384+
</program>
385+
</listing>
386+
345387
<p>
346388
<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.
347389
</p>
348390
<p>
349391
To safely downcast, you should first check the object's type using the <c>instanceof</c> operator.
350392
</p>
351-
<pre>
352-
// 'myAnimal' is an Animal reference, but it points to a Dog object.
353-
if (myAnimal instanceof Dog) {
354-
// The check passed, so this downcast is safe.
355-
Dog myDog = (Dog) myAnimal;
356-
357-
// Now we can access methods specific to the Dog class.
358-
myDog.bark(); // This is now valid.
393+
<listing xml:id="downcasting-example">
394+
<program interactive="activecode" language="java">
395+
<code>
396+
class Animal {
397+
public void makeSound() {
398+
System.out.println("The animal makes a sound.");
399+
}
359400
}
360-
</pre>
401+
402+
class Dog extends Animal {
403+
public void bark() {
404+
System.out.println("The dog barks!");
405+
}
406+
}
407+
408+
void main() {
409+
410+
Animal myAnimal = new Dog();
411+
412+
// 'myAnimal' is an Animal reference, but it points to a Dog object.
413+
if (myAnimal instanceof Dog myDog) {
414+
// Now we can access methods specific to the Dog class.
415+
myDog.bark();
416+
}
417+
}
418+
419+
</code>
420+
</program>
421+
</listing>
361422

362423
<p>
363424
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.

0 commit comments

Comments
 (0)