From e498cf6f6057a0f6c49b0b55c875412a8ddf8098 Mon Sep 17 00:00:00 2001 From: Chris Newland Date: Sat, 31 Jan 2015 12:02:42 +0000 Subject: [PATCH] Rewrote PolymorphismTest example from Animal to Coin so that literals can be more easily spotted in the assembly. --- .../resources/examples/PolymorphismTest.java | 54 +++++++------------ 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/src/main/resources/examples/PolymorphismTest.java b/src/main/resources/examples/PolymorphismTest.java index 9da2b3cd..4f4316e6 100644 --- a/src/main/resources/examples/PolymorphismTest.java +++ b/src/main/resources/examples/PolymorphismTest.java @@ -1,58 +1,42 @@ public class PolymorphismTest { - public interface Animal - { - void speak(); - } + public interface Coin { void deposit(); } - public static int woofs = 0, miaows = 0, moos = 0; + public static int moneyBox = 0; - public class Dog implements Animal - { - public void speak() { woofs++; } - } + public class Nickel implements Coin { public void deposit() { moneyBox += 5; } } - public class Cat implements Animal - { - public void speak() { miaows++; } - } + public class Dime implements Coin { public void deposit() { moneyBox += 10; } } - public class Cow implements Animal - { - public void speak() { moos++; } - } + public class Quarter implements Coin { public void deposit() { moneyBox += 25; } } - public PolymorphismTest() - { - Animal dog = new Dog(); - Animal cat = new Cat(); - Animal cow = new Cow(); + public PolymorphismTest() { + Coin nickel = new Nickel(); + Coin dime = new Dime(); + Coin quarter = new Quarter(); - Animal creature = null; + Coin coin = null; // change the variable maxImplementations to control the inlining behaviour // 2 = bimorphic dispatch - the method call will be inlined - // 3 = polymorphic dispatch - the method call will not be inlined + // 3 = megamorphic dispatch - the method call will not be inlined final int maxImplementations = 2; - for (int i = 0; i < 100000; i++) - { - switch(i % maxImplementations) - { - case 0: creature = dog; break; - case 1: creature = cat; break; - case 2: creature = cow; break; + for (int i = 0; i < 100000; i++) { + switch(i % maxImplementations) { + case 0: coin = nickel; break; + case 1: coin = dime; break; + case 2: coin = quarter; break; } - creature.speak(); + coin.deposit(); } - System.out.println("Woofs:" + woofs + " Miaows:" + miaows + " Moos:" + moos); + System.out.println("moneyBox:" + moneyBox); } - public static void main(String[] args) - { + public static void main(String[] args) { new PolymorphismTest(); } } \ No newline at end of file