Click here for the previous tutorial.
In the last tutorial we learned about number variables. Now we will be looking at something incredibly similar. The char (Character) and String type variables.
Both of these types hold essentially anything that can be typed. The difference is that a char will hold a single keyboard character, and a String is a string of chars.
It is also important to note that these symbols stores in char or String types cannot be used for math. So a char with the keyboard character 5 stored within its memory can not be used interchangably with an int with the same value stored.
Declaring chars and Strings is just as easy as declaring number types.
char charName;
char charName = 'r';
String stringName;
String stringName = "Hello World";
You may have noticed that chars use single quotes, while Strings use doubles. This is simply a matter of syntax. Generally, Eclipse or any other compiler you might be using should let you know if you accidentally slip on this rule.
Additionally, you may have noticed that char is all lowercase, and String always begins uppercase. This is because char is what is considered a primitive data type, which means it exists at the basis of Java, and the computer itself understands how to allot memory for this variable. On the otherhand, declaring a String is creating a new String type object. The significance will be explained later, for now understand the proper declaration of each. (Note that even though String is capatilized, stringName still follows the camal case convention. This form is advised)
If you have already gotten through and understood the numbers section, then the use of these types of variables might already seem obvious. For example, we can make a String type, and pass it to anywhere you would normally use a double-quoted string of characters, such as the println method.
System.out.println(stringName);
You could do the same use for a char type, the only difference being that a char can only hold one character at a time, while a String can hold near infinite.
Another note on the println method, is that you can attach multiple arguments using a plus sign. In this case it technically works different from the addition operator, but uses the same symbol. So
System.out.println(stringName + stringName);
Would print "Hello WorldHelloWorld" with its current value of stringName = "Hello World". Of course, this looks ugly. We should at least put a space there, which could be accomplished by simply adding one in, in between a set of double quotes.
System.out.println(stringName + " " + stringName);
Now is will print out "Hello World Hello World" which is at least a little cleaner. you may have noticed that we actually found a minor use for the char type variable. We might add one into the code if we wanted, instead of the space in the arguments. Because each time we use the char it will be saving us from typing three characters, its name should be shorter than this length if it is to actually be useful.
char s = ' ';
Now we run into a new problem: this name is extremely non-descriptive. It may save us a bit of time if programming were just dependent on typing speed, but most of the difficulty actually comes in problem solving, and not just how fast someone can type keywords. A name like "space" might solve this problem, but now it takes more than the three characters that we are saving. It takes more time to type space than to type " ", so we should leave this variable out. It does not contribute to our workflow.
Now you are probably wondering what good a character even is. The answer: at this point, not much. They will be used later on, and will prove to be valuble. Say you wanted to print a grid to the console of a simple map. If it were to be randomly generated, this would be very difficult to assign characters to positions in a String and then print. Instead, it would print a series of chars. ~ might be water, space be land, and ^ be mountain regions. Currently we do not have the capabilities to accomplish this, but we will return to the ideas. Chars are not obsolete, so do not forget them.
Click here to coninue to the next tutorial.
Sample code explained here:
char charName;
charName = 'r';
String stringName;
stringName = "Hello World";
System.out.println(stringName + " " + stringName);
Remember that you can change variable names at anytime. You can declare and set a variable, use it, change it, and use it again.
String stringName = "Hello there!";
System.out.println(stringName);
stringName = "General Kenobi";
System.out.println(stringName);
Try it!