001package algs11; 002public class XAutoboxingValueOf { 003 public static String concat(String s, String t) { 004 return (s + t); 005 } 006 public static void main (String[] args) { 007 { 008 System.out.print ("\nString Dog: "); 009 String x1 = "Dog"; 010 String x2 = "Dog"; 011 String x3 = new String ("Dog"); 012 String x4 = new String ("Dog"); 013 014 System.out.format ("%-5b ", x1 == x2); 015 System.out.format ("%-5b ", x1 == x3); 016 System.out.format ("%-5b ", x3 == x4); 017 018 String x3i = x3.intern (); 019 String x4i = x4.intern (); 020 System.out.format ("%-5b ", x1 == x3i); 021 System.out.format ("%-5b ", x3i == x4i); 022 } 023 { 024 System.out.print ("\nString ...: "); 025 String x1 = "Antidisestablishmentarianism"; 026 String x2 = "Antidisestablishmentarianism"; 027 String x3 = new String ("Antidisestablishmentarianism"); 028 String x4 = new String ("Antidisestablishmentarianism"); 029 030 System.out.format ("%-5b ", x1 == x2); 031 System.out.format ("%-5b ", x1 == x3); 032 System.out.format ("%-5b ", x3 == x4); 033 034 String x3i = x3.intern (); 035 String x4i = x4.intern (); 036 System.out.format ("%-5b ", x1 == x3i); 037 System.out.format ("%-5b ", x3i == x4i); 038 } 039 { 040 System.out.print ("\nString + : "); 041 String x1 = "hi" + "mom"; 042 String x2 = "hi" + "mom"; 043 String x3 = new String ("hi" + "mom"); 044 String x4 = new String ("hi" + "mom"); 045 046 System.out.format ("%-5b ", x1 == x2); 047 System.out.format ("%-5b ", x1 == x3); 048 System.out.format ("%-5b ", x3 == x4); 049 050 String x3i = x3.intern (); 051 String x4i = x4.intern (); 052 System.out.format ("%-5b ", x1 == x3i); 053 System.out.format ("%-5b ", x3i == x4i); 054 } 055 { 056 System.out.print ("\nString concat: "); 057 String x1 = concat ("hi", "mom"); 058 String x2 = concat ("hi", "mom"); 059 String x3 = new String (concat ("hi", "mom")); 060 String x4 = new String (concat ("hi", "mom")); 061 062 System.out.format ("%-5b ", x1 == x2); 063 System.out.format ("%-5b ", x1 == x3); 064 System.out.format ("%-5b ", x3 == x4); 065 066 String x3i = x3.intern (); 067 String x4i = x4.intern (); 068 System.out.format ("%-5b ", x1 == x3i); 069 System.out.format ("%-5b ", x3i == x4i); 070 } 071 { 072 System.out.print ("\nCharacter a: "); 073 Character x1 = 'a'; 074 Character x2 = 'a'; 075 Character x3 = new Character ('a'); 076 Character x4 = new Character ('a'); 077 078 System.out.format ("%-5b ", x1 == x2); 079 System.out.format ("%-5b ", x1 == x3); 080 System.out.format ("%-5b ", x3 == x4); 081 082 Character x3i = Character.valueOf (x3); 083 Character x4i = Character.valueOf (x4); 084 System.out.format ("%-5b ", x1 == x3i); 085 System.out.format ("%-5b ", x3i == x4i); 086 } 087 { 088 System.out.print ("\nCharacter \u13A7: "); 089 Character x1 = '\u13A7'; 090 Character x2 = '\u13A7'; 091 Character x3 = new Character ('\u13A7'); 092 Character x4 = new Character ('\u13A7'); 093 094 System.out.format ("%-5b ", x1 == x2); 095 System.out.format ("%-5b ", x1 == x3); 096 System.out.format ("%-5b ", x3 == x4); 097 098 Character x3i = Character.valueOf (x3); 099 Character x4i = Character.valueOf (x4); 100 System.out.format ("%-5b ", x1 == x3i); 101 System.out.format ("%-5b ", x3i == x4i); 102 } 103 { 104 System.out.print ("\nInteger 0: "); 105 Integer x1 = 0; 106 Integer x2 = 0; 107 Integer x3 = new Integer (0); 108 Integer x4 = new Integer (0); 109 110 System.out.format ("%-5b ", x1 == x2); 111 System.out.format ("%-5b ", x1 == x3); 112 System.out.format ("%-5b ", x3 == x4); 113 114 Integer x3i = Integer.valueOf (x3); 115 Integer x4i = Integer.valueOf (x4); 116 System.out.format ("%-5b ", x1 == x3i); 117 System.out.format ("%-5b ", x3i == x4i); 118 } 119 { 120 System.out.print ("\nInteger 12: "); 121 Integer x1 = Integer.valueOf(12); 122 Integer x2 = Integer.valueOf(12); 123 Integer x3 = new Integer (12); 124 Integer x4 = new Integer (12); 125 126 System.out.format ("%-5b ", x1 == x2); 127 System.out.format ("%-5b ", x1 == x3); 128 System.out.format ("%-5b ", x3 == x4); 129 130 Integer x3i = Integer.valueOf (x3); 131 Integer x4i = Integer.valueOf (x4); 132 System.out.format ("%-5b ", x1 == x3i); 133 System.out.format ("%-5b ", x3i == x4i); 134 } 135 { 136 System.out.print ("\nInteger 13297: "); 137 Integer x1 = 13297; 138 Integer x2 = 13297; 139 Integer x3 = new Integer (13297); 140 Integer x4 = new Integer (13297); 141 142 System.out.format ("%-5b ", x1 == x2); 143 System.out.format ("%-5b ", x1 == x3); 144 System.out.format ("%-5b ", x3 == x4); 145 146 Integer x3i = Integer.valueOf (x3); 147 Integer x4i = Integer.valueOf (x4); 148 System.out.format ("%-5b ", x1 == x3i); 149 System.out.format ("%-5b ", x3i == x4i); 150 } 151 { 152 System.out.print ("\nDouble 0.0: "); 153 Double x1 = 0.0; 154 Double x2 = 0.0; 155 Double x3 = new Double (0); 156 Double x4 = new Double (0); 157 158 System.out.format ("%-5b ", x1 == x2); 159 System.out.format ("%-5b ", x1 == x3); 160 System.out.format ("%-5b ", x3 == x4); 161 162 Double x3i = Double.valueOf (x3); 163 Double x4i = Double.valueOf (x4); 164 System.out.format ("%-5b ", x1 == x3i); 165 System.out.format ("%-5b ", x3i == x4i); 166 } 167 } 168}