I am surprised to see this behaviour.
Is it a bug or something?
for(Object obj = new Object(), Integer = new Integer(300);
obj.toString().length()>3;
System.out.println("on object's loop")) {
} //causes an infinite loop (not foreach loop, of course)
above code compiles and run fine without any reference to new Integer(300). Why so?
I am only interested in knowing why Integer = new Integer(300); is okay without any reference.
Object obj = new Object(), Integer = new Integer(300);
This creates two variables:
obj of type Object, which gets assigned to new Object().Integer (yes, that's the name of the variable) also of type Object, which gets assigned to new Integer(300).By the way this has nothing to do with the for-loop; that line would compile fine on its own. Now, if that , was really a ;, it would be a different story.
In general, we can construct valid statements of the form:
Type t1 = ..., t2 = ..., t3 = ..., ...;
which is equivalent to
Type t1 = ...;
Type t2 = ...;
Type t3 = ...;
...
I think he's asking why Integer = new Integer(300) works. – arshajii 2 mins ago
Integer is valid identifier name and its type is Object because of
Object obj = new Object(), Integer = new Integer(300);
Which is equivalent to
int a=2, b=4;
obj.toString() prints the String (consisting classname and hashcode), which has length > 3 so the infinite loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With