Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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,
So let us get started with the first topic of this article,
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.
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,
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 the Java 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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 5th October,2024 5th October SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co