Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
No matter what kind of value you are working within Java, you can typically output that value as a string, simply by concatenating it to some other value. To demonstrate this, I have declared eight variables, one each of the primitive data types, character, Boolean, byte and so on. Each of these is using a literal value as its initial value. Now I'll place the cursor after the declarations and use System.out.println and first I'll output the character. I'll save and run the application and I get the letter z, that's pretty expected.
Now one of the great things that Java does for you is that when you want to translate something into a string, it's usually fairly automatic. I am going to copy and then paste this line seven times and I'll change the values that I am outputting in each of these lines using the Boolean, the byte, the short, the integer, the long, the float, and the double. I'll save and run the application again and there is the result. Notice that all the values are output almost exactly as they were set. The one exception is the double which had so many digits before the decimal that Java automatically translated it for me.
When you output values using the out object's print or println commands, these are usually automatically translated to strings. But the same translation happens whenever you concatenate values. Let' take one of these values, the short and I am going to output the short value, concatenate it to a string. I'll use System.out.println ("The value of s is -- and then I'll use the plus operator and the value s. I'll save the changes and run the application and I get what you would expect, The value of s is 32000.
Now, in some languages, it matters when you are concatenating whether the first value is a string or whether the first value is a number. In some languages, if the first value is a number, then the plus operator will be used for math, but if the first value is a string then it will be used for concatenation. So let's test that in Java. I'll take this println command and copy and paste it and I'll reverse the order. Instead of starting with the string, I'll start with the numeric value, s + " is the value of s. In some languages, this would cause an error because the first value was a number and the plus operator should be seen as a mathematical plus rather than as a string concatenation plus, but in Java, the rule is that if there is even one string involved then the whole thing will be turned into a string.
I'll run the application and I'll see that that works as well. You can also combine math and string concatenation. If an expression starts with a mathematical operation, such as 2+2, the math will be executed first. The result will then be converted to a string and concatenated to the rest of the expression. If you put the math at the end of the expression though, everything will be treated as a string. Finally, complex objects are also translated to strings automatically. If you declare a string and then output a string, that's pretty simple.
You are just getting the value of the string object, but what about dates. I'll create a new Date object. I'll use the Date class and a variable name of myDate = new Date and then I'll use System.out.println ("The new date is -- and then I'll output myDate. If you see any errors, that means that you have to include a special notation at the top of the screen called a package. The Date class is member of a package that has to be explicitly imported.
I'll talk about class importing later, but for now, all you need to do is place the cursor after the data type Date and press Ctrl+Space. Then, choose this version of the Date class, Date in java.util, double click it and at the top of the screen, you'll see a statement added import java.util.date. That's an instruction to the compiler that says, when I refer to the Date class, this is the version I mean. Now I'll save my changes and run the application and I get a long string, the string representation of the current date and time.
So how is this happening? Well, every complex class in Java has a special method called toString. Let's look at the documentation for the Date class. I'll double-click date and then go to Dynamic Help. I'll click on Javadoc for java.util. date and maximize that screen, I'll click the Method list, scroll down and here is the toString method. Every class has this toString method and the implementation of the method is going to be different from one class to another.
When you pass a complex object into System.out.println or otherwise concatenate the object to an existing string, Java automatically looks for this method, toString, executes it, takes the return value, and that's what will become a part of the string. You'll see in later videos when you create your own complex classes that you can use this model pretty effectively for debugging and other tools in your application. When you create your own custom classes and you give them a toString method, you will be able to take instances of those classes, pass them into these methods that require strings and the translation will happen automatically.
So that's a look at how values are translated from their native data types into strings in Java. Again, primitives translate automatically, but complex objects depend on the toString method which is implemented differently for each class.
Get unlimited access to all courses for just $25/month.Become a member