• Post Reply Bookmark Topic Watch Topic
  • New Topic

code explaination  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want some explanation on my code

This is my Object class



and I am making call from some other class method in my case it is readPersons()
here is code




I want to know what is purpose of toString() method in this code

Thanks
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case toString() is redundant. System.out.println() will call an object's toString() method for you.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand it clearly can you please give any link to in which this concept is link or will explain more?
 
Tim Cooke
Marshal
Posts: 4048
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to assume that as you don't understand the code then it is not your code. Please can you QuoteYourSources (<- click link and read) and say where you got it? It's a copyright thing so please treat the request with importance. Thanks
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait, the toString() in Person? The comments tell you what is happening:

//Overriding toString to be able to print out the object in a readable way
//when it is later read from the file.

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a simple example on toString() method and explanation

Implementing toString method in java is done by overriding the Object’s toString method. The java toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object.

http://www.javabeginner.com/learn-java/java-tostring-method

code is basically making object into more readable form so that when some one reads it would be easy.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite a good example there.
Never write \n unless you have been told to use the line feed character. You may have to get a LINE_END constant
public static final String LINE_END = System.getProperty("line.separator");
This is because the line end on Windows (and older Macs) is not \n

You are probably better off using the + operator. Write
return firstName + LINE_END + lastName + LINE_END + age;
There is an optimisation so that method may at some time in the future run faster than creating a StringBuilder.
I usually don't append a line end to the toString method. Then one can use the print and println methods to display it with or without new line.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!