Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 79667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:10:54+00:00 2026-05-10T21:10:54+00:00

I used to use the implicit call of toString when wanting some debug info

  • 0

I used to use the implicit call of toString when wanting some debug info about an object, because in case of the object is null it does not throw an Exception.

For instance:

System.out.println('obj: '+obj); 

instead of:

System.out.println('obj: '+obj.toString()); 

Is there any difference apart from the null case?
Can the latter case work, when the former does not?

Edit:
What exactly is done, in case of the implicit call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:10:55+00:00Added an answer on May 10, 2026 at 9:10 pm

    There’s little difference. Use the one that’s shorter and works more often.

    If you actually want to get the string value of an object for other reasons, and want it to be null friendly, do this:

    String s = String.valueOf(obj); 

    Edit: The question was extended, so I’ll extend my answer.

    In both cases, they compile to something like the following:

    System.out.println(new StringBuilder().append('obj: ').append(obj).toString()); 

    When your toString() is implicit, you’ll see that in the second append.

    If you look at the source code to java, you’ll see that StringBuilder.append(Object) looks like this:

    public StringBuilder append(Object obj) {     return append(String.valueOf(obj)); } 

    where String.valueOf looks like this:

    public static String valueOf(Object obj) {     return (obj == null) ? 'null' : obj.toString(); } 

    Now, if you toString() yourself, you bypass a null check and a stack frame and go straight to this in StringBuilder:

    public StringBuilder append(String str) {     super.append(str);     return this; } 

    So…very similar things happens in both cases. One just does a little more work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that has the invariant that an object must be constructed
I have a few questions about the INTENT of variables within a subroutine in
I have a generics class that I used to write data to IsolatedStorage. I
I would like to use CommonLibrary .NET in my project but I have legacy
I know there are some advantages in using page forwarding as it is the
I've been looking through the N3291 working draft of C++0x. And I was curious
I just finished reading Restful Web Services and Nobody Understands REST or HTTP and
my Class: public static class Global { public static void TextBoxEmpty<T>(T ContainerControl) where T
Could anyone provide a comparison or specific details of how is template instantiation handled
I'm not actually sure how to ask the question so here's what I'm doing:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.