Handling the toString() method response in Java — Android

Joseph Ridge
4 min readJun 21, 2022

--

While learning some concept that involved implementing parcelables in android , came across some interesting challenge upon viewing the contents of the Spinner Widget.

Before we proceed, the general idea behind the application was, creating a note keeper that has the following functionalities :

  • Add new note, a note is composed of a topic , title and content.
  • Select category of topic to save or place the new note under.
  • View all notes.

Some special features :

  • If the client or user are on the AddNewNote Activity, and press the back button the note will save the existent note, if it has content else it will not save but just navigate back on the back stack.

This was the initial output:

Android Application example displaying non overridden toString() method on a spinner or drop down selecter

This response is neither an error nor a bug, but rather the response of the default toString() method.

The toString() method is generally used when one wants to represent an object as a string or basically return a string representation of an object. However, to attain a custom response one has to override the toString() method and customize it to your current use case.

Example Use Case :

You are building an application that meant for an article publishing firm, one of the data models or objects required is that of an article. The main contents of the Article object are the title and content. We shall start by creating a class name it Article :

Create your main class, or rather the entry point to your application. At this point we will create our firstArticle object and pass in the respective parameters:

Time for JVM to ‘shine’, run your application ….

This build was run on Vs Code, you can get how to run it here or use some common tools like :

pick your tool then run the application.

Your expected response :

__________________________________________My Article Details : Article@251a69d7__________________________________________

The output above is a combination of the class name ( Article ) and the hexadecimal representation of the object hashCode() response.

This particular output is a result of the default toString() method :

Overriding the toString()

In this case we can return the title of the Article and use it as our response. Hence in the Article class we created earlier we can add this override inside it.

run the application once more and your output will be :

____________________________________________My Article Details : Some Piece of Pie ...____________________________________________

The new Article class will look like this :

Output on the actual device after toString() override :

Useful terminologies Used in this article :

  • JVM : Java Virtual Machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are compiled by bytecode. You can read more on this from freecodecamps’ article .
  • Class : A class is a blueprint used to create and object. Think of this like having a sketch/ guide of how to make a Cup. The sketch can be given to someone else and used by them to make their own cup and customize it if need be.
  • Object : An instance of a class. From the prior example , think of this like you have had your sketch / guide on how to make a cup, now you decide to make multiple cups or a cup with a unique shape, size, color or whatever attribute that has been given in the cup sketch/ guide.
  • Hashcode : Numeric representation of an objects’ contents so as to provide an alternate mechanism to identify it, by default the hashcode() method returns an integer that represents the internal memory address of the object .

Feel free to lay feedback through comments, claps or both . ☺️

Thank you very much ..

--

--

Joseph Ridge

Software Engineer| Data Scientist | Artist (visual) | Rider| . #Live_Love_Laugh