Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use Integer.toString() instead of just printing the Integer?

I was following a Java tutorial and it was having me do something like:

int k = 5;
System.out.println("The number is " + Integer.toString(k));

So like a good sport I followed along, but I can't help but wonder, why use that method at all? Seems like a lot of extra typing to get the same functionality as:

int k = 5;
System.out.println("The number is " + k);

Is there a good reason to use the toString() method for non-string data types? And in my specific example, are there any int values that can't be auto toString'ed?

like image 987
Mike Avatar asked Jan 02 '13 17:01

Mike


People also ask

What is the use of integer toString () in Java?

toString() is an inbuilt method in Java which is used to return the String object representing this Integer's value. Parameters: The method does not accept any parameters. Return Value:The method returns the string object of the particular Integer value.

What is difference between integer toString and string valueOf?

toString() and String. valueOf() To summarize, there are no actual differences between these two methods, but we should understand the following points to avoid confusion. There is one extra call in the stack trace when we use String.

What does a toString () method returns as a string?

A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.

Can toString return an int?

Java Integer toString() Method The toString() method of Java Integer class returns a string equivalent of this Integer's value. This method gives the same result as Integer.


1 Answers

It's one of many "safe programming" practices you can adopt to improve clarity and prevent subtle errors which you or a future maintainer of your code could introduce. Take the following example:

private static void printMessage(int k) {
    System.out.println("number" + '=' + k);
}

output: number=5

Now suppose you want to print the number first and the text last. All I'm doing is swapping the first and last items in the string concatenation:

private static void printMessage2(int k) {
    System.out.println(k + '=' + "number");
}

output: 66number

Oops! The int value of = was added to k instead of being concatenated. One solution is to make the = into a String. Another solution is to explicitly convert the int to a String:

private static void printMessage2Safe(int k) {
    System.out.println(Integer.toString(k) + '=' + "number");
}   

output: 5=number

"Oh, but I'd never do that, anyway," you say. "Anyone in their right mind would have just made the = part of the other String." But what if you're implementing part of a basic calculator app, and you want to output the expression and its result? If you're really trying to be careful, I suppose you'll use StringBuilder.append() to concatenate the parts of the expression. But if not, then I hope you don't write the first implementation of the display method below.

public class Calculator {

    public static void main(String[] args) {
        display(2, '+', 3, 5);
        displaySafe(2, '+', 3, 5);
    }

    public static void display(int num1, char operator, int num2, int result) {
        System.out.println(num1 + operator + num2 + '=' + result);
    }

    public static void displaySafe(int num1, char operator, int num2, int result) {
        System.out.println(Integer.toString(num1) + operator + Integer.toString(num2) + "=" + result);
    }

}

output:

114
2+3=5
like image 194
rob Avatar answered Sep 25 '22 20:09

rob