The toString method Assignment Help

Assignment Help: >> Objects in Java >> The toString method

The toString method:

Another method that all classes inherit from Object is the toString method. The purpose of this method is to produce a readable text representation of an object's contents. The method has the signature: 

public String toString()

The method that you inherit from Object produces the name of the class followed, after an @ symbol, by a numeric (hexadecimal) value computed using the object. 

The exception is that the literal null or any reference whose value is null is printed as follows:

null

This is unfortunately indistinguishable in printed form from the string "null", although that is completely different. For example, making use of a two-argument User constructor: 

public User(String anID, String anAddress)
{
numOfAccesses = 0;
userID = anID;
emailAddress = anAddress;
}

you might have written:

User a = new User("Tembo", "[email protected]");
System.out.println(a.toString());
The output would look something like this:

User@3fbdb0

This is not very useful output, although it does identify the class of the object whose toString method we invoked. In fact, it is not necessary to call the toString method in this context, as one version of the println method will attempt to convert its arguments to strings using their toString methods in any case. So you could have written: 

System.out.println(a); 

with the same effect.

If we want to have a particular form of output for our User objects, we can de?ne a toString method in the User class. It might look something like this: 

public String toString ()
{
return "User " + userID + ", Email " + emailAddress;
}
Now: User a = new User("Tembo", "[email protected]");
System.out.println(a);
produces the output:
User Tembo, Email [email protected]

The equals and toString methods are both worth implementing for almost any class you write. The behaviour of the methods is up to you: you decide what makes two objects of a class equal and you decide what kind of text output you would like your objects to produce when printed.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your The toString method homework and assignments? Live The toString method experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The toString method homework help, java assignment help and The toString method projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd