[Java] How to use the toString () method

Programming study diary

November 12, 2020 Summarize the toString method for converting numbers to strings.

What is the toString method?

Used to convert numeric type to String type. Specify the argument to be converted to a String type character string in the argument, and return the String type character string as the return value.

Convert a number to a string


public class Main {
  pubic static void main(String[] args) {
    int num1 = 1234;
    int num2 = 5678;
    System.out.println(num1 + num2);

    //Convert a number to a string
    String str1 = Integer.toString(num1);
    String str2 = Integer.toString(num2);
    System.out.println(str1 + str2);
  }
}

Execution result


6912
12345678

Override and use toString method

In order to use the toString method in your own class, you need to override it. Add @Override to override.

Override the toString method in your own class


class Age {
  int age = 21;
  @Override 
  public String toString() {
    return "age =" + age;
  }
}
public static Main() {
  public static void main(String89 args) {
    Age age = new age();
    String str = profile.toString();
    System.out.println(str);
  }
}

Execution result


age = 22

Use toString method in HashMap

The toString method is defined in the java.lang.Object class, so you can use it in any class. Therefore, the toString method may be used unknowingly.

python


import java.util.HashMap;
 
public class Main{
	public static void main(String[] args){
		HashMap<String, String> map = new HashMap<>();
		map.put("key1", "value1");
		map.put("key2", "value2");
		map.put("key3", "value3");
		System.out.println(map);
	}
}

Execution result


{key1=value1, key2=value2, key3=value3}

In this code, the object of java.util.HashMap is specified in the argument of System.out.println () at the end, and the code confirms the elements in the object. If a reference type other than java.lang.String is specified as an argument, it will be the same as writing System.out.println (map.toString ());, and the object's toString ( The character string is output by the) method.

References

[Introduction to Java] Converting numbers to strings with toString (explaining overrides) Useful usage of [Java] toString () method

Recommended Posts

[Java] How to use the toString () method
How to use the replace () method (Java Silver)
How to use the link_to method
How to use the include? method
How to use the form_with method
[Java] How to use join method
[Java] How to use the File class
[Java] How to use the hasNext function
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Rails] How to use the map method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java] How to use Map
[Java] How to use Map
[Java] How to use Thread.sleep to pause the program
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
Output of how to use the slice method
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
[Ruby basics] How to use the slice method
[Java] How to use compareTo method of Date class
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
How to use the wrapper class
[Processing × Java] How to use variables
[Ruby] How to use any? Method
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Ruby inject method
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
How to get the class name / method name running in Java
How to use the getter / setter method (in object orientation)
Multilingual Locale in Java How to use Locale
[Java] How to compare with equals method
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to install the legacy version [Java]
How to use Java Scanner class (Note)
How to get the date in java
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Rails] How to use helper method, confimartion
[Java] Learn how to use Optional correctly