- Java Programming for Beginners
- Mark Lassoff
- 792字
- 2021-07-02 15:22:46
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:

- Java程序設計與開發
- Hyper-V 2016 Best Practices
- C# 從入門到項目實踐(超值版)
- PostgreSQL技術內幕:事務處理深度探索
- HTML5 and CSS3 Transition,Transformation,and Animation
- Flutter跨平臺開發入門與實戰
- Working with Odoo
- Learning FuelPHP for Effective PHP Development
- Kotlin開發教程(全2冊)
- 快速入門與進階:Creo 4·0全實例精講
- 從0到1:HTML5 Canvas動畫開發
- Android應用開發實戰(第2版)
- Exploring SE for Android
- Visual Basic程序設計基礎
- Monitoring Docker