• 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

why is the output strange..

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Run this program and we get extra "Testclass" in the ouput.
Can anybody explain where is it coming from?


public class Test {

public static String text = new String();

public static void main(String args[]){

System.out.println(text.getClass());

System.out.println(text.getClass()+ " unknown " + new Test().test());
}

public String test(){
System.out.print(getClass());
return "";
}

}

Thanks,
Gagan
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. use code tags.

2. This is the output i am getting:
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Order of execution:
- System.out.println(text.getClass()) // prints "class java.lang.String" with an enter
- retrieve text.getClass() // returns a Class object for String
- append that Class object with " unknown "; the Class object is turned into a String using its toString method, and the result is "class java.lang.String unknown "
- call new Test().test()
- inside that method, print out getClass() // prints "class Test" without an enter
- inside that method, return ""
- take the return value and append it to the previous String. It is know "class java.lang.String unknown "
- print out that String // prints "class java.lang.String unknown " with an enter

Since one of the print outs misses an enter (print is called, not println) the "class Test" and "class java.lang.String unknown " are printed directly after each other.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Rob,

try this version of your program:




output:


Does this answer your query?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic