Sunday, June 28, 2009

Puzzle 37 – Fun with Strings (BirthDay Blues)

Language – Java | Type – Concept | Last date 1-Jul-2009 9:00 p.m. IST | Points 3

It’s my birthday today (June 28th) – so I got one of those Happy Birthday puzzles I have been saving up for so long!
Strings are one of the most used Java classes – so how well do we know Strings? Here another puzzle on Strings.

package com.twisters;
class Stingify{
public static void main(String args[]){
String firstOne
= new String("Happy Birthday");
String secondOne
= new String("Happy Birthday");
/* No change to the line below! */
System.out.println(
"First String is equal to second : " + (firstOne == secondOne));
}
}

The first string is pretty much equal to the second. I think it’s fair that the output should be First String is equal to second: true.

Just a few simple conditions:
1. Don’t make any changes to the line which has the print statement
2. Three semicolons are more than enough in this program. No additional semicolons.
3. The usual rule – add as many characters that you like but no deleting characters. Commenting any of the existing lines of code is equivalent to deleting it.

P.S. - I think there might be couple of hints and rambling coming across on Twitter.
P.S.2 - I still looking for folks who want to test drive the Java Treasure Hunt. Any takers?

Got an answer? Do leave it here.

16 comments:

  1. class Stingify {
    public static void main(String args[]) {
    String firstOne = new String("Happy Birthday");
    String secondOne = new String("Happy Birthday").valueOf(firstOne);
    /* No change to the line below! */
    System.out.println("First String is equal to second: " + (firstOne == secondOne));
    }
    }

    ReplyDelete
  2. Happy Birthday! Have a nice party :-)


    Can you please merge the points of SlimMo and Stefan (Stephen), because I just changed my nickname...both have the same ID: 13802280639197311708

    Best regards
    Stefan

    ReplyDelete
  3. String firstOne = new String("Happy Birthday").intern();
    String secondOne = new String("Happy Birthday").intern();

    Too easy :(

    ReplyDelete
  4. We have to use the canonical form of the two strings to make them identical:

    String firstOne = new String("Happy Birthday").intern();
    String secondOne = new String("Happy Birthday").intern();

    And last but not least: happy birthday to you!

    ReplyDelete
  5. package com.twisters;
    class Stingify{
    public static void main(String args[]){
    String firstOne = new String("Happy Birthday").intern();
    String secondOne = new String("Happy Birthday").intern();
    /* No change to the line below! */
    System.out.println("First String is equal to second : " + (firstOne == secondOne));
    }
    }

    ReplyDelete
  6. It's really easy today :P

    public static void main(String args[]){
    String firstOne = new String("Happy Birthday");
    String secondOne = firstOne = new String("Happy Birthday");
    /* No change to the line below! */
    System.out.println("First String is equal to second : " + (firstOne == secondOne));
    }

    ReplyDelete
  7. String firstOne = new String("Happy Birthday").intern();
    String secondOne = new String("Happy Birthday").intern();

    ReplyDelete
  8. The obvious solution:

    String firstOne = new String("Happy Birthday").intern();
    String secondOne = new String("Happy Birthday").intern();

    And an alternative:
    String firstOne = new String("Happy Birthday").getClass().getName();
    String secondOne = new String("Happy Birthday").getClass().getName();

    or this:
    String firstOne = Boolean.valueOf(new String("Happy Birthday")).toString();
    String secondOne = Boolean.valueOf(new String("Happy Birthday")).toString();

    but my favorite is:

    String firstOne = "new String(\"Happy Birthday\")";
    String secondOne = "new String(\"Happy Birthday\")";


    And the sneakiest one is (no changes to any existing line of code and a circumvention of the semicolon rule):

    String firstOne = new String("Happy Birthday");
    String secondOne = new String("Happy Birthday");
    if ((firstOne=secondOne) == secondOne)
    /* No change to the line below! */
    System.out.println("First String is equal to second : " + (firstOne == secondOne));

    ReplyDelete
  9. String secondOne = firstOne = new String("Happy Birthday")

    ReplyDelete
  10. public static void main(String[] args) {
    String firstOne = new String("Happy Birthday");
    String secondOne = new String("Happy Birthday") == firstOne ? : firstOne;
    /* No change to the line below! */
    System.out.println("First String is equal to second : " + (firstOne == secondOne));
    }

    ReplyDelete
  11. String secondOne = new String("Happy Birthday").intern();

    ReplyDelete
  12. One more from one of my colleagues
    String firstOne = new String("Happy Birthday").valueOf(false);
    String secondOne = new String("Happy Birthday").valueOf(false);

    ReplyDelete
  13. I heard you wanted a second solution :

    String firstOne = new String("Happy Birthday").equals(false)?null:null;
    String secondOne = new String("Happy Birthday").equals(false)?null:null;
    /* No change to the line below! */
    System.out.println("First String is equal to second : " + (firstOne == secondOne));


    It's pretty much the same :)

    ReplyDelete
  14. And I almost forgot the internal representation :

    String firstOne = new String("Happy Birthday").intern();
    String secondOne = new String("Happy Birthday").intern();
    /* No change to the line below! */
    System.out.println("First String is equal to second : " + (firstOne == secondOne));

    ReplyDelete
  15. Line 5 should become:

    String secondOne = new String("Happy Birthday").valueOf(firstOne);

    Happy Birthday by the way =)

    ReplyDelete

Solution for this question?