aspose file tools*
The moose likes Beginning Java and the fly likes difference in System.out.println() vs. assigning a String 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 "difference in System.out.println() vs. assigning a String" Watch "difference in System.out.println() vs. assigning a String" New topic
Author

difference in System.out.println() vs. assigning a String

justin wall
Greenhorn

Joined: Jul 30, 2001
Posts: 8
I'm baffled by this. Why do I get and error with the second line and not the first?
System.out.println("INSERT INTO _columns(name,table) VALUES('" + loop.next() + "','" + newSA.tables.get(loopCount) + "')");
String q1 = ("INSERT INTO _columns(name,table) VALUES('" + loop.next() +"','" + newSA.tables.get(loopCount) + "')");
(newSA.tables is a list)
I get this with the second line, but not the first:
java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:423)
at tangoParser.parseFile(tangoParser.java:156)

Thanks,
Justin Wall
kyle amburn
Ranch Hand

Joined: Jul 29, 2001
Posts: 64
Hi Justin-
When you use the System.out.println() method, the method parses the paramaters and calls the toString() method of the paramater object when required. When you declare a String literal (or a String object) this is not done. Therefore you need to append the toString() to the object such as loop.next().toString() whenever the value returned is not a primative type or a String object itself. Also if the object being returned is of Object type but has a String as the actual object, you need to make an explicit cast.
Hope this helps,
Kyle
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Originally posted by kyle amburn:
When you use the System.out.println() method, the method parses the paramaters and calls the toString() method of the paramater object when required. When you declare a String literal (or a String object) this is not done.

On the contrary, when a String is contatenated to another Object, the toString() method of that Object is called during the contatenation. You do not need to explicitly call toString() on that Object.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference in System.out.println() vs. assigning a String