GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Useless toString on String — CodeQL query help documentation

Useless toString on String¶

ID: java/useless-tostring-call
Kind: problem
Security severity: 
Severity: recommendation
Precision: high
Tags:
   - quality
   - maintainability
   - useless-code
Query suites:
   - java-code-quality.qls
   - java-security-and-quality.qls

Click to see the query in the CodeQL repository

There is no need to call toString on a String because it just returns the object itself. From the Java API Specification entry for String.toString():

public String toString()

This object (which is already a string!) is itself returned.

Recommendation¶

Do not call toString on a String object.

Example¶

The following example shows an unnecessary call to toString on the string name.

public static void main(String args[]) {
	String name = "John Doe";
	
	// BAD: Unnecessary call to 'toString' on 'name'
	System.out.println("Hi, my name is " + name.toString());
	
	// GOOD: No call to 'toString' on 'name'
	System.out.println("Hi, my name is " + name);
}

References¶

  • Java API Specification: String.toString().

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.