aspose file tools*
The moose likes Beginning Java and the fly likes System.out.println( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.out.println("t is an Object is: " + t instanceof Object);   " Watch "System.out.println("t is an Object is: " + t instanceof Object);   " New topic
Author

System.out.println("t is an Object is: " + t instanceof Object);

Christian Orphall
Greenhorn

Joined: Aug 13, 2003
Posts: 4
Hi,
I have found a strange issue:
System.out.println("t is an Object is: " + t instanceof Object);
System.out.println("t is an Object is: " + (t instanceof Object));
is not the same!
The first line produces "true", the second line "t is an Object is: true"
A little amazing, any idea ?
Chris
class test {
private int x; // some stuff
public static void main (String[] args) {

test t = new test();

System.out.println("t is an Object is: " + t instanceof Object);
System.out.println("t is an Object is: " + (t instanceof Object));
}

}
produces the output:

true
t is an Object is: true
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hello Christian, welcome to JavaRanch!
In the first instance: "t is an Object is: " + t instanceof Object, the grouping is: ("t is an Object is: " + t) instanceof Object. The object referred to by t is coerced to its String value and that value is joined onto the end of "t is an Object is: ", resulting in another String. That String is, of course, an instance of Object. So you get the result true printed.
In the second case you are evaluating in a different order: the object referred to by t is an instance of Object, so you get a boolean true. The boolean value true is coerced to the string "true" and that is appended onto "t is an Object is: ", resulting in "t is an Object is: true". That last String is what gets printed.
So no suprises here, really


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Christian Orphall
Greenhorn

Joined: Aug 13, 2003
Posts: 4
Thank you!
Chris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.out.println("t is an Object is: " + t instanceof Object);