File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes why is the output strange.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why is the output strange.." Watch "why is the output strange.." New topic

why is the output strange..

ashu Suri

Joined: Oct 22, 2008
Posts: 18
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()+ " unknown " + new Test().test());

public String test(){
return "";


salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

1. use code tags.

2. This is the output i am getting:

My Website: [] Cool your mind:[] My Sally:[]
Rob Spoor

Joined: Oct 27, 2005
Posts: 19908

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.

How To Ask Questions How To Answer Questions
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

I agree with Rob,

try this version of your program:


Does this answer your query?
I agree. Here's the link:
subject: why is the output strange..