From the course: Java 8+ Essential Training: Syntax and Structure

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Convert primitive values to strings

Convert primitive values to strings - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Convert primitive values to strings

- [Instructor] I've described previously how to append primitive values to strings, but there are some tricks to know about. To get the default representation of a primitive value, you don't need any special syntax, just append it. I'll create an integer variable that I'll name result, and I'll get its value by adding together 10 plus 12. I get 22, now I'll create a string that I'll call answer, and I'll get that value with the answer is, and then I'll append result. And I get the string, the answer is 22. So far, so good. As long as there's at least one string in a series of values, the conversion happens automatically. It doesn't matter where the string is in position. So for example, I can do this, string howMany, and I'll assign it starting with a number, and then I'll append a string. And that works fine. Because there's at least one string in the series of values, the entire thing is turned into a string. But if you try to do the same thing with a series of numbers, it won't…

Contents