Skip to content
Fix Code Error

How to use the toString method in Java?

March 13, 2021 by Code Error
Posted By: Anonymous

Can anybody explain to me the concept of the toString() method, defined in the Object class? How is it used, and what is its purpose?

Solution

From the Object.toString docs:

Returns a string representation of the
object. In general, the toString
method returns a string that
“textually represents” this object.
The result should be a concise but
informative representation that is
easy for a person to read. It is
recommended that all subclasses
override this method.

The toString method for class Object
returns a string consisting of the
name of the class of which the object
is an instance, the at-sign character
`@’, and the unsigned hexadecimal
representation of the hash code of the
object. In other words, this method
returns a string equal to the value
of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Example:

String[] mystr ={"a","b","c"};
System.out.println("mystr.toString: " + mystr.toString());

output:- mystr.toString: [Ljava.lang.String;@13aaa14a
Answered By: Anonymous

Related Articles

  • Examples of GoF Design Patterns in Java's core libraries
  • How can I resolve Web Component Testing error?
  • TLS 1.3 server socket with Java 11 and self-signed…
  • Unable to run Robolectric and Espresso with a…
  • When I'm testing a web app by JUnit and Mockito I…
  • error LNK2005: ✘✘✘ already defined in…
  • ClassNotFoundException:…
  • Eclipse will not start and I haven't changed anything
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • Neither BindingResult nor plain target object for…
  • How do I include certain conditions in SQL Count
  • Jetpack Compose and Hilt Conflict
  • java IO Exception: Stream Closed
  • Gradle error: Execution failed for task…
  • SQLException: No suitable Driver Found for…
  • SecurityException: Permission denied (missing…
  • What is The Rule of Three?
  • Can't access Eclipse marketplace
  • Requested bean is currently in creation: Is there an…
  • Using enums in a spring entity
  • android studio 0.4.2: Gradle project sync failed error
  • Execution failed for task…
  • org.gradle.api.tasks.TaskExecutionException:…
  • How to convert java.util.Date to java.sql.Date?
  • How to parse JSON file with Spring
  • Configure hibernate to connect to database via JNDI…
  • What is a NullReferenceException, and how do I fix it?
  • Android- Error:Execution failed for task…
  • Android Studio 4.2.1 NullPointer Exception on startup
  • JPA Hibernate Persistence exception…
  • What's the difference between Instant and LocalDateTime?
  • Could not install Gradle distribution from…
  • Mock MQRFH2 header in JUnit Testing Error [MQRFH2…
  • How do I print my Java object without getting…
  • How do I install Java on Mac OSX allowing version switching?
  • ClassNotFoundException thrown
  • Launching Spring application Address already in use
  • UnsatisfiedDependencyException: Error creating bean…
  • Getting Dropwizard Client And Jersey/HTTP I/O Error…
  • Eclipse fails to open .vue files in Web Page Editor
  • How do you match strings in custom Instruments…
  • "Non-resolvable parent POM: Could not transfer…
  • org.springframework.beans.factory.NoSuchBeanDefiniti…
  • Reference - What does this regex mean?
  • HashMap with multiple values under the same key
  • Error inflating class android.support.v7.widget.Toolbar?
  • Vaadin Spring Boot - There was an exception while…
  • What is attr_accessor in Ruby?
  • java.lang.ClassNotFoundException: HttpServletRequest
  • How can I get a List from some class properties with…
  • XML Serialize generic list of serializable objects
  • java.lang.RuntimeException: Unable to instantiate…
  • How to add Typescript to a Nativescript-Vue project?
  • Plugin…
  • Extend Aurelia Validation Rules on a per class basis
  • Spring Boot - Cannot determine embedded database…
  • Problems using Maven and SSL behind proxy
  • How do I establish a link/relationship between objects?
  • Java ElasticSearch None of the configured nodes are…
  • Creating Vue Search Bar | How to hide/show data…
  • Spring Boot with ElasticSearch in Groovy: WebClient…
  • Spring schemaLocation fails when there is no…
  • When use ResponseEntity and @RestController for…
  • Injection of autowired dependencies failed;
  • Failed to execute goal…
  • What does "Could not find or load main class" mean?
  • How to obtain the start time and end time of a day?
  • Gradle: Execution failed for task ':processDebugManifest'
  • Address already in use: JVM_Bind
  • How do I sort an observable collection?
  • store struct of info in single linked list
  • C# group by a list contains another class
  • NullpointerException error while working with…
  • Multiple scenarios @RequestMapping produces JSON/XML…
  • Eclipse - Run in Debug Mode fails
  • Ember link-to throws error / does not clear screen…
  • What is a stack trace, and how can I use it to debug…
  • Java.lang.NoClassDefFoundError:…
  • Multipart File Upload Using Spring Rest Template +…
  • Vue.js 3: props type validation with custom type
  • RxJava Main Thread Always Crash
  • Spring Security exclude url patterns in security…
  • Why is lock(this) {...} bad?
  • Name [jdbc/mydb] is not bound in this Context
  • Vue - Deep watching an array of objects and…
  • What's causing my java.net.SocketException:…
  • Jackson cannot parse body of a POST request
  • Webpack build error
  • How to use java.net.URLConnection to fire and handle…
  • Aurelia binding to a function with parameter and…
  • VueJS return value from promise in method section of…
  • An Authentication object was not found in the…
  • Exception : AAPT2 error: check logs for details
  • org.springframework.beans.factory.BeanCreationExcept…
  • What are the new features in C++17?
  • How to get ResulSet multiple values into Java objects
  • No matching function for to call operator ++ overloading
  • How to disable SSL certificate checking with Spring…
  • Could not resolve placeholder in string value
  • Parcelable encountered IOException writing…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Access-Control-Allow-Origin Multiple Origin Domains?

Next Post:

Check if a string contains a string in C++

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error