Since we have been dealing with different types of variables, we might as well take a brief look at the String class. So let’s recall from last lesson, almost all of the primitive data types have to deal with numbers. It’s either variables that store whole numbers, or variables that store decimal numbers. The special one is char, you can store letters / symbols in it according to the ACSII code chart.
However, a char variable can only hold one character. What if you want to store a whole line? Like “I Love Cheese”? It is stupid if we have to do something like:
public class Calc {
public static void main(String[] args) {
char i = 'i';
//Yes, you can define a bunch of variables of the same type using this method
char l = 'l', o = 'o', v = 'v', e = 'e';
char c = 'c';
char h = 'h';
char s = 's';
char space = ' ';
//Why do we have to use "" in front of all the char variables?
//Why if we don't use "" it print out a number?
//Does this have something to do with casting type?
System.out.println(""+i+space+l+o+v+e+space+c+h+e+e+s+e);
}
}
Yeah, that is definitely not the right way to do it. So here’s where the String class comes in. Continue reading →