官术网_书友最值得收藏!

Char variables

Programs that manipulate numbers are all well and good, but quite often, we want to be able to work with text and words as well. To help us do this, Java defines the character, or char, the primitive type. Characters are the smallest entity of text that you can work with on a computer. We can think of them to start off with as single letters.

Let's create a new project; we'll call it Characters.java. We'll start our program by simply defining a single character. We'll call it character1 and we'll assign to it the value of uppercase H:

package characters; 
 
public class Characters { 
    public static void main(String[] args) { 
        char character1 = 'H'; 
    } 
} 

Just as we have to use some extra syntax when defining a floating-point number explicitly, we need some extra syntax when defining a character. To tell Java that we're explicitly declaring a character value here, we surround the letter we would like to assign to our variable with two single quotation marks. The single quotation marks, as opposed to double quotation marks, let Java know that we're working with a character or a single letter, as opposed to trying to use an entire string. Characters can only have single entity values. If we attempt to assign the value of Hi to character1, NetBeans and Java would both let us know that that's not a valid option:

Now, let's move on and write a somewhat convoluted program that will nonetheless work pretty well for our example purposes. Let's define five characters. We'll call them character1 through character5. We'll assign each one of them with one of the five letters of the word "Hello," in that order. When these characters are printed together, our output will show Hello. In the second portion of our program, let's use System.out.print() to display these letters on the screen. The System.out.print() code works just the same as System.out.println(), except that it doesn't add a carriage return at the end of our line. Let's have the last command as println so that our output is separated from all of the additional text presented in our console:

package characters; 
 
public class Characters { 
 
    public static void main(String[] args) { 
        char character1 = 'H'; 
        char character2 = 'e'; 
        char character3 = 'l'; 
        char character4 = 'l'; 
        char character5 = 'o'; 
        System.out.print(character1); 
        System.out.print(character2); 
        System.out.print(character3); 
        System.out.print(character4); 
        System.out.println(character5); 
    } 
} 

If we run this program, it greets us. It says Hello and then there is some additional text:

That's pretty straightforward.

Now let me show you something that will give us a little insight into how our computer thinks about characters. It turns out that we can set the value of character1 not only by explicitly declaring the capital letter H between two single quotation marks, but also by giving it an integer value. Each possible character value has a corresponding number that we can use in lieu of it. If we replace H with the value of 72, we're still going to print out Hello. If we were to use the value 73, one greater than 72, instead of the capital H, we'll now get the capital letter I, as I is the letter that follows H.

We have to make sure we don't put 72 between single quotation marks. The best case scenario is that Java recognizes that 72 is not a valid character and that it looks more like two characters, then our program won't compile. If we use a single digit number surrounded by single quotation marks, our program would compile just fine, but we would get the completely unexpected output of 7ello.

So how do we figure out the numeric value of characters? Well, there's a universal lookup table, the ASCII table, which maps characters to their numeric values:

In this section, we've been dealing with columns 1 (Dec) and columns 5 (Chr), which have the decimal number and the character that they are mapped to. You'll notice that while many of these characters are letters, some are keyboard symbols, numbers, and other things, such as tabs. As far as programming languages are concerned, new lines, tabs, and back spaces are all character elements.

To see this in action, let's try replacing some of the characters in our program with the decimal value 9, which should correspond to a tab character. If we replace the middle three letters of our word with tabs, as output, we should expect H, three tabs, and o:

package characters; 
 
public class Characters { 
 
    public static void main(String[] args) { 
        char character1 = 'H'; 
        char character2 = 9; 
        char character3 = 9; 
        char character4 = 9; 
        char character5 = 'o'; 
        System.out.print(character1); 
        System.out.print(character2); 
        System.out.print(character3); 
        System.out.print(character4); 
        System.out.println(character5); 
    } 

The following is the output of the preceding code:

主站蜘蛛池模板: 辛集市| 凤山县| 托克逊县| 比如县| 长岛县| 盐城市| 隆安县| 观塘区| 亳州市| 金门县| 县级市| 大田县| 永修县| 建始县| 青阳县| 交口县| 昔阳县| 巨鹿县| 阿勒泰市| 新和县| 湟源县| 淳化县| 江北区| 上饶县| 台州市| 白城市| 阿拉善右旗| 淮滨县| 临江市| 镇康县| 茂名市| 长丰县| 雷山县| 横山县| 通州市| 新龙县| 阿拉善盟| 石渠县| 大同县| 特克斯县| 海门市|