SlideShare ist ein Scribd-Unternehmen logo
1 von 2
6R8NJ3Importance of toString() in java
>> Saturday, October 20, 2012
Tweet
Object class of Java is having predefined toString() method. This method by default Object class
calls implicitly when an object created. Overriding toString manually is nothing but
implementing this method in our class.The java toString() method is used when we need a string
representation of an object. It is defined in Object class.
The toString() method is useful for debugging. By default, when an object is printed out in a
print stream like System.out, the toString() method of the object is automatically called.
While develping the code the developers used to check the object properties are getting through
the object or not. For this print statement will be useful to quick test in the console.
public String toString()
Returns: a string representation of the object.
Let us take a Person class and try to print the person object using the System.out.println()
statement.
package blog.javabynataraj;
//@author Muralidhar N
class Person{
public String fname;
public String lname;
Person(String fn,String ln){
this.fname=fn;
this.lname=ln;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}
public class ToStringTest {
public static void main(String[] args) {
Person p = new Person("murali","dhar");
System.out.println(p);
}
}
The output will be the class name@hexadecimal
here the Object class's method toString returning
getClass().getName()+'@'+Integer.toHexString(hashCode())
But here we assumed that the firstname and the lastname will be print. But it is not done. This is
the magic toStirng method.
But in the below program we can print the firstname and lastname of person Object. What we are
going to do here is just overriding the toString method in Person Class.
package blog.javabynataraj;
//@author Muralidhar N
class Person{
public String fname;
public String lname;
Person(String fn,String ln){
this.fname=fn;
this.lname=ln;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String toString(){
return (getClass()+" FirstName: "+fname+" LastName: "+lname);
}
}
public class ToStringTest {
public static void main(String[] args) {
Person p = new Person("murali","dhar");
System.out.println(p);
}
}
See the output of this:
Now you can achieve this by overriding the default toString() method inside Person class to
return the contents of the instance of Person.

Weitere ähnliche Inhalte

Was ist angesagt?

Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享LearningTech
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi Kant Sahu
 
Arrays string handling java packages
Arrays string handling java packagesArrays string handling java packages
Arrays string handling java packagesSardar Alam
 
Python Closures Explained | What are Closures in Python | Python Closures
Python Closures Explained | What are Closures in Python | Python ClosuresPython Closures Explained | What are Closures in Python | Python Closures
Python Closures Explained | What are Closures in Python | Python ClosuresIntellipaat
 
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Ayes Chinmay
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Yeardezyneecole
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Ayes Chinmay
 
Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...
Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...
Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...Subhajit Sahu
 
The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.1 book - Part 69 of 180The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.1 book - Part 69 of 180Mahmoud Samir Fayed
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in javaMahmoud Ali
 

Was ist angesagt? (20)

Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Oop lecture7
Oop lecture7Oop lecture7
Oop lecture7
 
Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Arrays string handling java packages
Arrays string handling java packagesArrays string handling java packages
Arrays string handling java packages
 
JVM and OOPS Introduction
JVM and OOPS IntroductionJVM and OOPS Introduction
JVM and OOPS Introduction
 
Python Closures Explained | What are Closures in Python | Python Closures
Python Closures Explained | What are Closures in Python | Python ClosuresPython Closures Explained | What are Closures in Python | Python Closures
Python Closures Explained | What are Closures in Python | Python Closures
 
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
Internet and Web Technology (CLASS-15) [JAVA Basics] | NIC/NIELIT Web Technol...
 
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. NagpurCore & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
Core & Advance Java Training For Beginner-PSK Technologies Pvt. Ltd. Nagpur
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 
4. method overloading
4. method overloading4. method overloading
4. method overloading
 
Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...
Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...
Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...
 
The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.1 book - Part 69 of 180The Ring programming language version 1.5.1 book - Part 69 of 180
The Ring programming language version 1.5.1 book - Part 69 of 180
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 

Andere mochten auch (8)

Lecture5 2
Lecture5 2Lecture5 2
Lecture5 2
 
Sockets
SocketsSockets
Sockets
 
Tcp
TcpTcp
Tcp
 
1
11
1
 
Lecture7
Lecture7Lecture7
Lecture7
 
Lecture6
Lecture6Lecture6
Lecture6
 
Amcat+syllabus+and+sample+papers
Amcat+syllabus+and+sample+papersAmcat+syllabus+and+sample+papers
Amcat+syllabus+and+sample+papers
 
Amcat placement questions
Amcat placement questionsAmcat placement questions
Amcat placement questions
 

Ähnlich wie Rd

Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in javaHarish Gyanani
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodecamp Romania
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfirshadkumar3
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Assignment 7
Assignment 7Assignment 7
Assignment 7IIUM
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdfalliedscorporation
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Preexisting code, please useMain.javapublic class Main { p.pdf
Preexisting code, please useMain.javapublic class Main {    p.pdfPreexisting code, please useMain.javapublic class Main {    p.pdf
Preexisting code, please useMain.javapublic class Main { p.pdfrd1742
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 

Ähnlich wie Rd (20)

Oop
OopOop
Oop
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in java
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical Groovy
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
CHAPTER 3 part2.pdf
CHAPTER 3 part2.pdfCHAPTER 3 part2.pdf
CHAPTER 3 part2.pdf
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
3. Section 3 � Complete functionality on listing screen (1.5 marks.pdf
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Preexisting code, please useMain.javapublic class Main { p.pdf
Preexisting code, please useMain.javapublic class Main {    p.pdfPreexisting code, please useMain.javapublic class Main {    p.pdf
Preexisting code, please useMain.javapublic class Main { p.pdf
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 

Rd

  • 1. 6R8NJ3Importance of toString() in java >> Saturday, October 20, 2012 Tweet Object class of Java is having predefined toString() method. This method by default Object class calls implicitly when an object created. Overriding toString manually is nothing but implementing this method in our class.The java toString() method is used when we need a string representation of an object. It is defined in Object class. The toString() method is useful for debugging. By default, when an object is printed out in a print stream like System.out, the toString() method of the object is automatically called. While develping the code the developers used to check the object properties are getting through the object or not. For this print statement will be useful to quick test in the console. public String toString() Returns: a string representation of the object. Let us take a Person class and try to print the person object using the System.out.println() statement. package blog.javabynataraj; //@author Muralidhar N class Person{ public String fname; public String lname; Person(String fn,String ln){ this.fname=fn; this.lname=ln; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } } public class ToStringTest { public static void main(String[] args) { Person p = new Person("murali","dhar"); System.out.println(p); } }
  • 2. The output will be the class name@hexadecimal here the Object class's method toString returning getClass().getName()+'@'+Integer.toHexString(hashCode()) But here we assumed that the firstname and the lastname will be print. But it is not done. This is the magic toStirng method. But in the below program we can print the firstname and lastname of person Object. What we are going to do here is just overriding the toString method in Person Class. package blog.javabynataraj; //@author Muralidhar N class Person{ public String fname; public String lname; Person(String fn,String ln){ this.fname=fn; this.lname=ln; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLname() { return lname; } public void setLname(String lname) { this.lname = lname; } public String toString(){ return (getClass()+" FirstName: "+fname+" LastName: "+lname); } } public class ToStringTest { public static void main(String[] args) { Person p = new Person("murali","dhar"); System.out.println(p); } } See the output of this: Now you can achieve this by overriding the default toString() method inside Person class to return the contents of the instance of Person.