Fixing Java puzzlers

This commit is contained in:
Andrey Breslav
2011-11-11 20:57:53 +03:00
parent 66c1f56892
commit 30d90371dc
38 changed files with 1584 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
exPuzzlers
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AntConfiguration">
<defaultAnt bundledAnt="true" />
</component>
</project>
+21
View File
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="?*.properties" />
<entry name="?*.xml" />
<entry name="?*.gif" />
<entry name="?*.png" />
<entry name="?*.jpeg" />
<entry name="?*.jpg" />
<entry name="?*.html" />
<entry name="?*.dtd" />
<entry name="?*.tld" />
<entry name="?*.ftl" />
</wildcardResourcePatterns>
<annotationProcessing enabled="false" useClasspath="true" />
</component>
</project>
+5
View File
@@ -0,0 +1,5 @@
<component name="CopyrightManager">
<settings default="">
<module2copyright />
</settings>
</component>
+5
View File
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
+51
View File
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="JavadocGenerationManager">
<option name="OUTPUT_DIRECTORY" />
<option name="OPTION_SCOPE" value="protected" />
<option name="OPTION_HIERARCHY" value="true" />
<option name="OPTION_NAVIGATOR" value="true" />
<option name="OPTION_INDEX" value="true" />
<option name="OPTION_SEPARATE_INDEX" value="true" />
<option name="OPTION_DOCUMENT_TAG_USE" value="false" />
<option name="OPTION_DOCUMENT_TAG_AUTHOR" value="false" />
<option name="OPTION_DOCUMENT_TAG_VERSION" value="false" />
<option name="OPTION_DOCUMENT_TAG_DEPRECATED" value="true" />
<option name="OPTION_DEPRECATED_LIST" value="true" />
<option name="OTHER_OPTIONS" value="" />
<option name="HEAP_SIZE" />
<option name="LOCALE" />
<option name="OPEN_IN_BROWSER" value="true" />
</component>
<component name="ModuleEditorState">
<option name="LAST_EDITED_MODULE_NAME" />
<option name="LAST_EDITED_TAB_NAME" />
</component>
<component name="ProjectResources">
<default-html-doctype>http://www.w3.org/1999/xhtml</default-html-doctype>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_5" assert-keyword="true" jdk-15="true" project-jdk-name="1.6" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="SvnConfiguration" maxAnnotateRevisions="500">
<option name="USER" value="" />
<option name="PASSWORD" value="" />
<option name="mySSHConnectionTimeout" value="30000" />
<option name="mySSHReadTimeout" value="30000" />
<option name="LAST_MERGED_REVISION" />
<option name="MERGE_DRY_RUN" value="false" />
<option name="MERGE_DIFF_USE_ANCESTRY" value="true" />
<option name="UPDATE_LOCK_ON_DEMAND" value="false" />
<option name="IGNORE_SPACES_IN_MERGE" value="false" />
<option name="DETECT_NESTED_COPIES" value="true" />
<option name="CHECK_NESTED_FOR_QUICK_MERGE" value="false" />
<option name="IGNORE_SPACES_IN_ANNOTATE" value="true" />
<option name="SHOW_MERGE_SOURCES_IN_ANNOTATE" value="true" />
<option name="FORCE_UPDATE" value="false" />
<myIsUseDefaultProxy>false</myIsUseDefaultProxy>
</component>
</project>
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/exPuzzlers.iml" filepath="$PROJECT_DIR$/exPuzzlers.iml" />
</modules>
</component>
</project>
+5
View File
@@ -0,0 +1,5 @@
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
+125
View File
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
File diff suppressed because one or more lines are too long
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
+49
View File
@@ -0,0 +1,49 @@
namespace std
fun <T> array(vararg array : T) = array
fun array(vararg array : Byte) = array
fun array(vararg array : Char) = array
fun array(vararg array : Short) = array
fun array(vararg array : Int) = array
fun array(vararg array : Long) = array
fun array(vararg array : Double) = array
fun array(vararg array : Float) = array
fun Any?.identityEquals(other : Any?) = this === other
namespace io {
import java.io.*
fun print(message : Any?) { System.out?.print(message) }
fun print(message : Int) { System.out?.print(message) }
fun print(message : Long) { System.out?.print(message) }
fun print(message : Byte) { System.out?.print(message) }
fun print(message : Short) { System.out?.print(message) }
fun print(message : Char) { System.out?.print(message) }
fun print(message : Boolean) { System.out?.print(message) }
fun print(message : Float) { System.out?.print(message) }
fun print(message : Double) { System.out?.print(message) }
fun print(message : CharArray) { System.out?.print(message) }
fun println(message : Any?) { System.out?.println(message) }
fun println(message : Int) { System.out?.println(message) }
fun println(message : Long) { System.out?.println(message) }
fun println(message : Byte) { System.out?.println(message) }
fun println(message : Short) { System.out?.println(message) }
fun println(message : Char) { System.out?.println(message) }
fun println(message : Boolean) { System.out?.println(message) }
fun println(message : Float) { System.out?.println(message) }
fun println(message : Double) { System.out?.println(message) }
fun println(message : CharArray) { System.out?.println(message) }
private var systemIn : InputStream? = null // Unfortunately, System.in may change
private var stdin : BufferedReader? = null // This may introduce leaks of system.in objects...
fun readLine() : String? {
if (stdin == null || systemIn != System.`in`) {
stdin = java.io.BufferedReader(java.io.InputStreamReader(System.`in`))
systemIn = System.`in`
}
return stdin?.readLine()
}
}
@@ -0,0 +1,14 @@
package _02_expressive._01_Oddity;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 5). Pearson Education (USA). Kindle Edition.
*/
public class Oddity {
public static boolean isOdd(int i) {
return i % 2 == 1;
}
public static void main(String[] args) {
System.out.println(isOdd(-1));
}
}
@@ -0,0 +1,19 @@
namespace change
import java.math.BigDecimal
import std.io.*
fun main(args : Array<String>) {
// Easy to make BigDecimals user-friendly
println(
"2.00".bd - "1.00"
)
}
val String.bd = BigDecimal(this)
fun BigDecimal.minus(other : BigDecimal) = this.subtract(other)
fun BigDecimal.minus(other : String) = subtract(other.bd) // this can be omitted
@@ -0,0 +1,11 @@
package _02_expressive._02_Time_for_a_Change;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 7). Pearson Education (USA). Kindle Edition.
*/
public class Change {
public static void main(String args[ ] ) {
System.out.println(2.00 - 1.10);
}
}
@@ -0,0 +1,20 @@
package _02_expressive._02_Time_for_a_Change;
import java.math.BigDecimal;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 7). Pearson Education (USA). Kindle Edition.
*/
public class ChangeSolutions {
public static void main(String args[ ] ) {
// Poor solution - still uses binary floating-point!
System.out.printf("%.2f%n", 2.00 - 1.10);
// Calculation in cents
System.out.println((200 - 110) + " cents");
// BigDecimal
System.out.println(new BigDecimal("2.00"). subtract(new BigDecimal("1.10")));
}
}
@@ -0,0 +1,12 @@
package _02_expressive._03_Long_Division;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 9). Pearson Education (USA). Kindle Edition.
*/
public class LongDivision {
public static void main(String[] args) {
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
}
}
@@ -0,0 +1,15 @@
namespace long.division
import std.io.*
fun main(args : Array<String>) {
// Problematic case does not compile
// val MICROS_PER_DAY_ : Long = 24 * 60 * 60 * 1000 * 1000;
// val MILLIS_PER_DAY_ : Long = 24 * 60 * 60 * 1000
// Solution:
val MICROS_PER_DAY : Long = 24.lng * 60 * 60 * 1000 * 1000;
val MILLIS_PER_DAY : Long = 24.lng * 60 * 60 * 1000
println(MICROS_PER_DAY / MILLIS_PER_DAY)
}
@@ -0,0 +1,12 @@
package _02_expressive._03_Long_Division;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 9). Pearson Education (USA). Kindle Edition.
*/
public class LongDivisionSolution {
public static void main(String[] args) {
final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
}
}
@@ -0,0 +1,10 @@
namespace long.division.solution
import std.io.*
fun main(args : Array<String>) {
val MICROS_PER_DAY : Long = 24.lng * 60 * 60 * 1000 * 1000;
val MILLIS_PER_DAY : Long = 24.lng * 60 * 60 * 1000
println(MICROS_PER_DAY / MILLIS_PER_DAY)
}
@@ -0,0 +1,11 @@
package _02_expressive._04_Elementary;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 11). Pearson Education (USA). Kindle Edition.
*/
public class Elementary {
public static void main(String[] args) {
System.out.println(12345 + 5432l);
}
}
@@ -0,0 +1,11 @@
namespace elementary
import std.io.*
fun main(args : Array<String>) {
// Problematic case does not compile
// println(12345 + 5432l)
// Correct syntax:
println(12345 + 5432.lng)
}
@@ -0,0 +1,11 @@
package _02_expressive._04_Elementary;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 11). Pearson Education (USA). Kindle Edition.
*/
public class ElementarySolution {
public static void main(String[] args) {
System.out.println(12345 + 5432L);
}
}
@@ -0,0 +1,11 @@
package _02_expressive._05_The_Joy_of_Hex;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 13). Pearson Education (USA). Kindle Edition.
*/
public class JoyOfHex {
public static void main(String[] args) {
System.out.println(Long.toHexString(0x100000000L + 0xcafebabe));
}
}
@@ -0,0 +1,8 @@
namespace multicast
import std.io.*
fun main(args : Array<String>) {
println(-1.byt.chr.int)
println((-1).byt.chr.int)
}
@@ -0,0 +1,11 @@
package _02_expressive._06_Multicast;
/**
* @author abreslav
*/
public class Mutlicast {
public static void main(String[] args) {
System.out.println((int) (char) (byte) -1);
}
}
@@ -0,0 +1,16 @@
package _02_expressive._07_Swap_Meat;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 17). Pearson Education (USA). Kindle Edition.
*/
public class CleverSwap {
public static void main(String[] args) {
int x = 1984; // (0x7c0)
int y = 2001; // (0x7d1)
x ^= y ^= x ^= y;
System.out.println("x = " + x + "; y = " + y);
// Solution: swap with a temp variable
// Do not assign to the same variable more than once in a single expression.
}
}
@@ -0,0 +1,10 @@
namespace cleverswap
import std.io.*
fun main(args : Array<String>) {
var x = 1
// Do not assign to the same variable more than once in a single expression.
// Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 19). Pearson Education (USA). Kindle Edition.
// x += (y += z)
}
@@ -0,0 +1,13 @@
package _02_expressive._08_Dos_Equis;
/**
* @author abreslav
*/
public class DosEquis {
public static void main(String[] args) {
char x = 'X';
int i = 0;
System.out.print(true ? x : 0);
System.out.print(false ? i : x);
}
}
@@ -0,0 +1,15 @@
namespace dos.equis
import std.io.*
fun main(args : Array<String>) {
// Problematic case does not compile
val x = 'X'
val i = 0
print(if (true) x else 0)
print(if (false) 0 else x)
// Mixed computations do not compile:
// val int : Int = if (true) x else 0
// val char : Char = if (false) 0 else x
}
@@ -0,0 +1,13 @@
package _02_expressive._09_Tweedledum;
/**
* @author abreslav
*/
public class Tweedledum {
public static void main(String[] args) {
byte x = 1;
byte i = 1;
x += i;
// x = x + i;
}
}
@@ -0,0 +1,13 @@
package _02_expressive._10_Tweedledee;
/**
* @author abreslav
*/
public class Tweedledee {
public static void main(String[] args) {
Object x = 1;
String i = "1";
// x += i;
x = x + i;
}
}
@@ -0,0 +1,12 @@
package _03_character._11_The_Last_Laugh;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 25). Pearson Education (USA). Kindle Edition.
*/
public class LastLaugh {
public static void main(String args[]) {
System.out.print("H" + "a");
System.out.print('H' + 'a');
}
}
@@ -0,0 +1,12 @@
namespace last.laugh
import std.io.*
fun main(args : Array<String>) {
print("H" + "a")
// Problematic case does not compile
// System.out?.print('H' + 'a')
// Solution
print('H'.toString() + 'a')
}
@@ -0,0 +1,19 @@
package _03_character._12_ABC;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 27). Pearson Education (USA). Kindle Edition.
*/
public class Abc {
public static void main(String[] args) {
String letters = "ABC";
char[] numbers = {'1', '2', '3'};
System.out.println(letters + " easy as " + numbers);
// Solution:
System.out.println(letters + " easy as ");
System.out.println(numbers);
// or
System.out.println(letters + " easy as " + String.valueOf(numbers));
}
}
@@ -0,0 +1,18 @@
package _03_character._13_Animal_Farm;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 29). Pearson Education (USA). Kindle Edition.
*/
public class AnimalFarm {
public static void main(String[] args) {
final String pig = "length: 10";
final String dog = "length: " + pig.length();
System.out.println("Animals are equal: " + pig == dog);
// Solution:
System.out.println("Animals are equal: " + pig.equals(dog));
// or
System.out.println("Animals are equal: " + (pig.intern() == dog.intern()));
}
}
@@ -0,0 +1,16 @@
namespace animal.farm
import std.io.*
import std.*
fun main(args : Array<String>) {
val pig = "length: 10";
val dog = "length: " + pig.length;
println("Animals are equal: " + pig == dog);
// Solution:
println("Animals are equal: " + (pig == dog));
// Note:
println("Animals are equal: " + (pig identityEquals dog));
}