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
+85-24Lines changed: 85 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -296,27 +296,48 @@ public class TempConv {
296
296
<p>
297
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.
298
298
</p>
299
-
<pre>
300
-
int myInt = 10;
301
-
double myDouble = myInt; // Automatic casting from int to double
302
-
</pre>
303
-
299
+
300
+
<listingxml:id="implicit-typecasting">
301
+
<programinteractive="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
+
304
313
<p>
305
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.
306
315
</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
+
<listingxml:id="explicit-typecasting">
317
+
<programinteractive="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>
311
328
312
329
<p>
313
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.
314
331
</p>
315
332
<p>
316
333
Let's imagine we have a simple class hierarchy: an <c>Animal</c> superclass and a <c>Dog</c> subclass.
317
334
</p>
318
-
<pre>
335
+
336
+
<listingxml:id="animal-dog-example">
337
+
<programlanguage="java">
338
+
<code>
319
339
class Animal {
340
+
320
341
public void makeSound() {
321
342
System.out.println("The animal makes a sound.");
322
343
}
@@ -327,37 +348,77 @@ class Dog extends Animal {
327
348
System.out.println("The dog barks!");
328
349
}
329
350
}
330
-
</pre>
351
+
</code>
352
+
</program>
353
+
</listing>
331
354
332
355
<p>
333
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.
334
357
</p>
335
-
<pre>
358
+
<listingxml:id="upcasting-example">
359
+
<programinteractive="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() {
336
374
// A Dog object is created, but the reference is of type Animal.
337
375
// This is implicit upcasting.
338
-
Animal myAnimal = new Dog();
376
+
Animal myAnimal = new Dog();
339
377
340
-
myAnimal.makeSound(); // This is valid, as makeSound() is defined in Animal.
378
+
myAnimal.makeSound(); // This is valid, as makeSound() is defined in Animal.
341
379
342
380
// myAnimal.bark(); // This would cause a compile-time error!
343
381
// The compiler only knows about the methods in the Animal reference type.
344
-
</pre>
382
+
}
383
+
</code>
384
+
</program>
385
+
</listing>
386
+
345
387
<p>
346
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.
347
389
</p>
348
390
<p>
349
391
To safely downcast, you should first check the object's type using the <c>instanceof</c> operator.
350
392
</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
+
<listingxml:id="downcasting-example">
394
+
<programinteractive="activecode"language="java">
395
+
<code>
396
+
class Animal {
397
+
public void makeSound() {
398
+
System.out.println("The animal makes a sound.");
399
+
}
359
400
}
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>
361
422
362
423
<p>
363
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.
0 commit comments