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.
class Stingify {
ReplyDeletepublic 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));
}
}
Happy Birthday! Have a nice party :-)
ReplyDeleteCan 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
String firstOne = new String("Happy Birthday").intern();
ReplyDeleteString secondOne = new String("Happy Birthday").intern();
Too easy :(
We have to use the canonical form of the two strings to make them identical:
ReplyDeleteString firstOne = new String("Happy Birthday").intern();
String secondOne = new String("Happy Birthday").intern();
And last but not least: happy birthday to you!
package com.twisters;
ReplyDeleteclass 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));
}
}
It's really easy today :P
ReplyDeletepublic 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));
}
Oh, and happy birthday ;)
ReplyDeleteString firstOne = new String("Happy Birthday").intern();
ReplyDeleteString secondOne = new String("Happy Birthday").intern();
The obvious solution:
ReplyDeleteString 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));
String secondOne = firstOne = new String("Happy Birthday")
ReplyDeletepublic static void main(String[] args) {
ReplyDeleteString 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));
}
String secondOne = new String("Happy Birthday").intern();
ReplyDeleteOne more from one of my colleagues
ReplyDeleteString firstOne = new String("Happy Birthday").valueOf(false);
String secondOne = new String("Happy Birthday").valueOf(false);
I heard you wanted a second solution :
ReplyDeleteString 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 :)
And I almost forgot the internal representation :
ReplyDeleteString 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));
Line 5 should become:
ReplyDeleteString secondOne = new String("Happy Birthday").valueOf(firstOne);
Happy Birthday by the way =)