Categories
5G Network
Agile
Amazon EC2
Android
Angular
Ansible
Arduino
Artificial Intelligence
Augmented Reality
AWS
Azure
Big Data
Blockchain
BootStrap
Cache Teachniques
Cassandra
Commercial Insurance
C#
C++
Cloud
CD
CI
Cyber Security
Data Handling
Data using R
Data Science
DBMS
Design-Pattern
DevOps
ECMAScript
Fortify
Ethical Hacking
Framework
GIT
GIT Slack
Gradle
Hadoop
HBase
HDFS
Hibernate
Hive
HTML
Image Processing
IOT
JavaScript
Java
Jenkins
Jira
JUnit
Kibana
Linux
Machine Learning
MangoDB
MVC
NGINX
Onsen UI
Oracle
PHP
Python
QTP
R Language
Regression Analysis
React JS
Robotic
Salesforce
SAP
Selenium
Service Discovery
Service Now
SOAP UI
Spark SQL
Testing
TOGAF
Research Method
Virtual Reality
Vue.js
Home
Q&A
Agile
AWS
JAVA
Hadoop
Blockchain
Cache
Bootstrap
DevOps
PHP
Data Science
Python
SQL
Testing
Prolog
XML
Ask a Question
tostring method
Home
JAVA
tostring method
asked
Sep 3, 2019
in
JAVA
by
Robin
Q:
tostring method
net
Answer
▼ Show
1
Answer
0
votes
answered
Sep 4, 2019
by
john ganales
Object toString() Method in Java
Every class in java is child of Object class either directly or indirectly. Object class contains toString() method. We can use toString() method to get string representation of an object. Whenever we try to print the Object reference then internally toString() method is invoked. If we did not define toString() method in your class then Object class toString() method is invoked otherwise our implemented/Overridden toString() method will be called.
Syntax of Object class toString() method:
public String toString()
{
return getClass().getName()+"@"+Integer.toHexString(hashCode());
}
filter_none
edit
play_arrow
brightness_4
// Java program to illustrate
// working of toString() method
class Best_Friend {
String name;
int age;
String college;
String course;
String address;
Best_Friend
(String name, int age, String college, String course, String address)
{
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
public static void main(String[] args)
{
Best_Friend b =
new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
System.out.println(b);
System.out.println(b.toString());
}
}
Output:
[email protected]
[email protected]
Explanation:In the above program, we create an Object of Best_Friend class and provide all the information of a friend. But when we try to print the Object, then we are getting some output which is in the form of
[email protected]
_in_Hexadeciaml_form. If we want the proper information about Best_friend object, then we have to override toString() method of Object class in our Best_Friend class.
filter_none
edit
play_arrow
brightness_4
// Java program to illustrate
// working of toString() method
class Best_Friend {
String name;
int age;
String college;
String course;
String address;
Best_Friend
(String name, int age, String college, String course, String address)
{
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
public String toString()
{
return name + " " + age + " " + college + " " + course + " " + address;
}
public static void main(String[] args)
{
Best_Friend b =
new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
System.out.println(b);
System.out.println(b.toString());
}
}
Output:
Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
NOTE:In all wrapper classes, all collection classes, String class, StringBuffer, StringBuilder classes toString() method is overriden for meaningful String representation. Hence, it is highly recommended to override toString() method in our class also.
filter_none
edit
play_arrow
brightness_4
// Java program to illustrate
// working of toString() method
import java.util.*;
class Best_Friend {
String name;
int age;
String college;
String course;
String address;
Best_Friend
(String name, int age, String college, String course, String address)
{
this.name = name;
this.age = age;
this.college = college;
this.course = course;
this.address = address;
}
public static void main(String[] args)
{
Best_Friend b =
new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu");
System.out.println(b);
String s = new String("Gulpreet Kaur");
System.out.println(s);
Integer i = new Integer(21);
System.out.println(i);
ArrayList l = new ArrayList();
l.add("BIT");
l.add("M.TECH");
System.out.println(l);
}
}
Output:
[email protected]
Gulpreet Kaur
21
[BIT, M.TECH]
JAVA
Learn More with Madanswer
Related questions
0
votes
Q: Name A Few Different Return Types Of A Controller Action Method?
asked
Aug 18, 2019
in
MVC Language
by
sheetalkhandelwal
#mvc-5-vs-mvc-4
#mvc
#mvc-mvc
#mvc-ado-
net
#mvc-mobile-app-development
#mvc-5-sample-project-with-database
#mvc-extensions
#asp
net-mvc-web-
hosting
#microsoft-asp
net-mvc-2
#asp-mvc-
#microsoft-mvc-4
#mvc-
net-framework
#framewok-tutorial
#framework7-interview-question
0
votes
Q: List Out Few Different Return Types Of A Controller Action Method?
asked
Aug 18, 2019
in
MVC Language
by
sheetalkhandelwal
#mvc-5-vs-mvc-4
#mvc
#mvc-mvc
#mvc-ado-
net
#mvc-mobile-app-development
#mvc-5-sample-project-with-database
#mvc-extensions
#asp
net-mvc-web-
hosting
#microsoft-asp
net-mvc-2
#asp-mvc-
#microsoft-mvc-4
#mvc-
net-framework
0
votes
Q: How to use Newtonsoft or JSON.net for serializing JSON data?
asked
Jul 19, 2020
in
JSON
by
SakshiSharma
#newtonsoft-json
net
#json-serialisation
#serializing-json
0
votes
Q: What is ASP.Net Framework?
asked
Apr 27, 2020
in
IONIC
by
SakshiSharma
#asp-
net
net-framework
0
votes
Q: C# 8.0 features
asked
Apr 8, 2020
in
C Sharp
by
GeorgeBell
net
framework
microsoft
0
votes
Q: C# 8.0 new features
asked
Dec 8, 2019
in
Dot Net
by
Sinaya
net
framework
microsoft
0
votes
Q: What is mmWave frequencies?
asked
Sep 2, 2019
in
5G Network
by
Venkatshastri
#5g
gauges
#nexus
5g
net
connection
sverige
discussion
optical
network
nodes
dangers
snopes
...