• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Christian Orphall
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!
Chris
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic