Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Java allows you to represent strings in one of two ways as complex objects or as single character. When you are working with single characters you can work with either a primitive data type or with a wrapper class for the data type. I am working in a project named characters which you can find in the 04 chapter of the exercise files. In order to declare variable that uses the primitive card data type you start off with the keyword car or char. You don't spell out the whole word character, it just those first four letters then you assign a variable name as usual and then you can either assign something that returns a character or you can assign a literal character.
Literal character values are wrapped in single quotes, never double quotes. I am going to set the value of this character to a numeric value of one because I want to show you that when you pass in a numeric value but rapid in those quotes it's no longer really a number and it can't easily be turned into a number. It can be converted to another variable that's a number, but this variable will always be a character. Now I am going to copy and paste this line of code a couple of times and I am going to rename the second 2 variable as c2 and c3 and change their values to 2 and 3.
Now I am going to output those values to the console. System.out.print and then I'll pass the values c1. notice I am using print and not println because I want to output this but not pass in a line feed at the end. Now I'll copy and paste that line of code a couple of times, and I will output c2 and c3 and I'll change the last one to println. I'll save the changes and run the application and there is the result. 1, 2, 3 three characters output right next to each other.
You can also set a character variable using a Unicode literal. Unicode is a way of describing individual characters in the alphabet using numeric codes. You can look up the numeric codes for Unicode on the website Unicode.org, that's u-n-i-c-o-d-e.o-r-g. I have looking up the value of the currency symbol the dollar sign and I found that it's a four digit number of 0024, here is how you could use that code to set a char variable in Java. I once again start off with the char data type and I'll name this variable dollar.
To set the literal value with Unicode start with the pair of single quotes as before but prefix the four digit numeric with \u, then I'll pass in 0024. Next I'll add another system.out. print at the beginning of the string and I will output dollar. I will save the changes and run the application and there is the result. The dollar sign followed by the 1, 2 and 3. Again these are four individual characters not a single string but from the application users point of view it looks like just a string.
As all with primitive data types there is a wrapper class available that you can use to accomplish all sorts of things. The name of the wrapper class for char is character. Now I've already brought up the documentation for this class it's a member of the Java.link package and is always available to your code. I'll go to the list of methods that are part of the character class and show you that there are all sorts of tools for analyzing or changing values. I'll go to the bottom of this listing and show you that there is a method called two uppercase. It's marked as static which means I call it from the class not from an instance of the class.
We'll talk about static properties and methods later on, but for now whenever you see the world static just note that you call it from the class name not from an instantiated copy of the class. You pass in and instance of the char variable. So now I'll go back to my code and I am going to go down to the bottom of the code and add some more characters. I'll start off with char a1=a char a2= b and char a3=c.Now I'll make a copy of these three print commands, copy them downward and I'll change the values that are being output to a1, a2, and a3.
I'll save the changes and run the application and show that I am getting a, b, c. now I'll use the two uppercase method of the character class. For each of these values I'll place the cursor inside the print method and call Character.toUppercase and I wrap the method called around the value that I am outputting. I'll make a copy of that line of code that will be easier than we typing all that a couple of times then I'll paste it in a couple of times, change the variables and change print to print line for the last command.
I'll save my changes and run the application and now I am outputting the uppercase versions of those character values. Notice I am not changing the original values, I am only modifying them as I output them. So the original lowercase versions are still available. So that's a look at how to use the char also sometimes pronounced char, char, or char data type. Once again it represents a single alphanumeric character the literal value is always wrapped in single quotes never double quotes and in your literal values you can use either the real character or the Unicode equivalent.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97164 Viewers
61 Video lessons · 84457 Viewers
71 Video lessons · 68665 Viewers
56 Video lessons · 101148 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.