Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
ToString
Introduction To Java Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Introduction To Java Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
Another string conversion method that will prove useful to you in the future is the toString() method, which takes almost any object in Java and converts it to a String. Let's take a look at this example.
ToStringExample.java
package exlcode;
public class ToStringExample {
public static int exampleVariableOne = 11;
public static double exampleVariableTwo = Math.PI;
public static String exampleVariableThree = Integer.toString(exampleVariableOne);
public static String exampleVariableFour = Double.toString(exampleVariableTwo);
public static void main(String[] args) {
System.out.println(exampleVariableThree);
System.out.println(exampleVariableFour);
}
}
This method is useful when you want to print certain expressions. Although primitive data types that we have covered up to this point would print without needing to be converted into a String, there will be other objects we will master, such as arrays, that cannot be print directly, so keep the toString method in mind as we move forward and learn about arrays.