This might be old puzzle, someone asked this question on Java Ranch. I think it's a pretty neat & tough puzzle why don't you folks give it a try!
class Equals {
public static void main(String args[]){
<sometype> x = <somevalue>;
System.out.println(x == x) //This should print false
}
}
Replace the placeholders <sometype> and <somevalue> with any construct or value so that the System.out.println() prints out a false. No other changes/additions/deletions are permitted.
Looking for a hint? I think one might just show up on twitter soon.
Got an answer? Leave your answer here.
Well, I missed the previous puzzle...
ReplyDeleteThis one is quite simple
double x = 0.0/0.0;
System.out.println(x == x);
NaN is always different than NaN and 0.0/0.0 is not a number :)
double x = Double.NaN;
ReplyDeleteor
float x = Float.NaN;
should do - here isNaN needs to be used instead to be able to determine if a floating point value is "not a number". Well of course the trick above would do also, in fact that's what the implementation of isNaN() is based on.
double x = Double.NaN
ReplyDeletedouble x = Double.NaN;
ReplyDeleteAccording to JLS NaN is unordered and always make equality operator return false.
ReplyDeletepublic static void main(String[] args){
// double x = Double.NaN
float x = Float.NaN;
System.out.println(x == x);
}
Maybe not what you have in mind:
ReplyDeleteObject x = new Object(){
Object get(){
System.out.println(false);
System.exit(0);
return this;
}
}.get();
I will think about another solution if I have time...
double x = Double.NaN;
ReplyDeleteThis is not soo abuzz:
ReplyDeleteObject x = new Object(){
{
System.out.println(false);
System.exit(0);
}
};
are u kidding in both cases(Primitive and Object type )it printed true
ReplyDeleteplz let me know my email id is nsureshreddy@live.com
this is what i compiled and run
class Equals {
public static void main(String args[]){
int x = 10;// String x="10"
System.out.println(x == x); //This should print false
}
}
float x = Float.NaN;
ReplyDeletedouble x = Double.NaN;
ReplyDeleteclass Equals {
ReplyDelete2 public static void main(String args[]){
3 float x = Float.Nan;
4 System.out.println(x == x) //This should print false
5 }
6 }
class Equals {
ReplyDeletepublic static void main(String args[]){
float x = Float.Nan;
System.out.println(x == x) }
}