How To Create A Tostring Method In Java
Everything You Need To Know About ToString In Java
Last updated on Jun 17,2021 14.5K Views
Java is a versatile programming language and has inspired many to pursue its implemenation as a career. People wanting to Java often start with the basics and get lost in the variety concepts it offers. This article on toString in Java will introduce you to a basic but a fairly important topic.
Following are pointers to be discussed in this article,
- toStringInJava
- Example For toString
- Necessary Overriding
So let us get started with the first topic of this article,
toString in Java
So what exactly is this method? Object class is the parent class in Java. It contains the toString method. The toString method is used to return a string representation of an object. If any object is printed, the toString() method is internally invoked by the java compiler. Else, the user implemented or overridden toString() method is called.
Here are some of the advantages of using this method.
Advantage
If you override the toString() method of the Object class, it will return values of the object, hence you are not required to write a lot of code.
Example For toString
public class Employee{ int id; String name; String city; Employee(int id, String name, String city){ this.id=id; this.name=name; this.city=city; } public static void main(String args[]){ Employee e1=new Employee(01,"Ari","NewYork"); Employee e2=new Employee(02,"Jon","Chicago"); System.out.println(e1);//compiler writes here s1.toString() System.out.println(e2);//compiler writes here s2.toString() } }
Output:
Employee@6d06d69c
Employee@7852e922
The code prints the HashCode values of the objects in the example.
Let us try and fine tune our approach in the next part of this article,
Necessary Overriding
Overriding is necessary to return the user specified values:
public class Employee{ int id; String name; String city; Employee(int id, String name, String city){ this.id=id; this.name=name; this.city=city; } public String toString(){//overriding the toString() method return id+" "+name+" "+city; } public static void main(String args[]){ Employee e1=new Employee(01,"Ari","NewYork"); Employee e2=new Employee(02,"Jon","Chicago"); System.out.println(e1); System.out.println(e2); } }
Output:
1 Ari NewYork
2 Jon Chicago
Hence, this is the procedure to be followed while using the toString method in Java.
Thus we have come to an end of this article on 'toString in Java'. If you wish to learn more, check out theJava Online Course by Edureka, a trusted online learning company. Edureka's Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.
Recommended videos for you
Node JS : Steps to Create Restful Web App
Watch Now
Microsoft SharePoint-The Ultimate Enterprise Collaboration Platform
Watch Now
Create Restful Web Application With Node.js Express
Watch Now
PHP & MySQL : Server-side Scripting Language for Web Development
Watch Now
MS .Net – An IntelliSense Way of Web Development
Watch Now
Effective Persistence Using ORM With Hibernate
Watch Now
Node JS Express: Steps to Create Restful Web App
Watch Now
Learn Perl-the Jewel of Scripting Languages
Watch Now
Microsoft SharePoint 2013 : The Ultimate Enterprise Collaboration Platform
Watch Now
Building Application With Ruby On Rails Framework
Watch Now
A Day In The Life Of A Node.js Developer
Watch Now
Microsoft .NET Framework : An IntelliSense Way of Web Development
Watch Now
NodeJS – Communication and Round Robin Way
Watch Now
Portal Development and Text Searching with Hibernate
Watch Now
Service-Oriented Architecture With Java
Watch Now
Hibernate-the ultimate ORM framework
Watch Now
Recommended blogs for you
Top 10 popular Java IDE: Choose the Best IDE for Java
Read Article
Struts 2 Tutorial – One Stop Solution for Beginners
Read Article
How To Implement Matrix Multiplication In Java?
Read Article
Java Regex – What are Regular Expressions and How to Use it?
Read Article
forEach Loop in JavaScript: One Stop Solution for beginners
Read Article
What is a Do while loop in Java and how to use it?
Read Article
Java Collections – Interface, List, Queue, Sets in Java With Examples
Read Article
Express.js Tutorial – One Stop Solution for Beginners
Read Article
How to remove index.php from URL in CodeIgniter?
Read Article
How To Create A Tostring Method In Java
Source: https://www.edureka.co/blog/tostring-in-java/
Posted by: damianoupinedegs.blogspot.com
0 Response to "How To Create A Tostring Method In Java"
Post a Comment