IT Band of Brothers Grammar Java data types 3

Character

In Java, the data type for strings is char. However, C / C ++ programmers beware: The Java char with the C or C ++, char is different. In C / C ++ in, char width is 8 bits. In Java is not the case. Instead, Java uses Unicode character representation. Unicode defines a fully international character set can represent all the characters of all human languages. unicode is the unity of dozens of character sets, such as Latin characters, Greek characters, Arabic characters, Cyrillic character set, the character set Hebrew, Japanese character sets, Korean character sets and Chinese character sets. For this purpose, Unicode requires 16 bits wide. Therefore, in Java char is a 16-bit type. Char range is 0 to 65536. No negative char values. Standard ASCII character set range from 0 to 127 remains; the extended 8-bit character set ISO-Latin-1, which is the range of 0 to 255. Since Java is designed to allow programmers to write programs in the world can be used, then the use of Unicode character representation is reasonable. Of course, for English, German, Spanish or French language such use Unicode to some extent, will reduce efficiency because such characters are represented in eight languages ​​can easily use. But in order to obtain portability in the world and the price to pay.

Here is a program demonstrates char variable usage:

public class CharDemo{

    public static void main(String[] args){

         char c1 = 97;

         char c2 = 'a';

         System.out.println("c1: " + c1);

         System.out.println("c2: " + c2);

    }

}

Compile and run this program, the console displays a message shown in FIG.

960a1922b7ea49d0946c1f174383746c.png

Run Results FIG. 1 CharDemo


注意,c1被赋值为整数97,该数值是与字母'a'对应的ASCII(以及Unicode)值。前面提到过,ASCII字符集占用Unicode字符集中前127个值。因此,在其它语言中对字符使用的所有“旧式技巧”,在Java中依然管用。

尽管char被设计成容纳Unicode字符,但它可以用作整数类型,可以对char类型的变量执行算术运算。例如,可以将两个字符相加到一起,或者增加字符变量的值。分析下面的程序:

public class CharDemo2{

    public static void main(String[] args){

         char c1 = 'b';

         System.out.println("c1: " + c1);

         char c2 = 'a' + 1;

         System.out.println("c2: " + c2);

    }

}

编译并运行这个程序,控制台将显示如图2所示的信息。

在该程序中,首先将'b'赋值给c1并输出,字符b在ASCII和Unicode字符集中的编码都是98,接下来声明了char变量c2,使用字符a加1初始化了c2变量,我们知道字符a在ASCII和Unicode字符集中的编码是97,那么97加1就是字符b在ASCII和Unicode中的字符编码,所以最后输出了字符b。

注意,在Java的正式规范中,char被当作整数类型,这意味着它和int、short、byte和long位于同一分类中。然而,因为char类型的主要用途是表示unicode字符,所以通常考虑将char放到单独的分类中。


eb9c5b5f49b44bf984e3bb0357263ef8.png

图2  CharDemo2运行结果


布尔型

There is a Java called basic boolean data type is used to represent a logical value. He is only one of two possible values: true or false. All relational operations (e.g., a <b) return the value of this type. Such conditional expressions for if and control statements for. Also you need to boolean type.

The following program demonstrates the boolean type:

public class BooleanDemo{

    public static void main(String[] args){

         boolean b = false;

         System.out.println("b: " + b);

         b = true;

         System.out.println("b: " + b);

         if (b) System.out.println ( "code is executed");

         b = false;

         if (b) System.out.println ( "This code will not be executed");

         System.out.println("10>9么? :" + (10 > 9));

    }

}

Compile and run this program, the console displays information as shown in Fig.

About this program has three interesting things to note. First it can be seen, when the output value of the boolean method the println (), shown is true or false. Secondly, if a control statement, the value of the boolean variable itself is sufficient. Do not need to write if statements like this:

if(b == true) …


0d2224e5a93d45918cd9b896ba147a65.png

Run Results FIG. 3 BooleanDemo

Finally, relational operators (e.g. <) output is a boolean value. This is why the expression 10> 9 displays the true reason. In addition, 10> 9 surrounding parentheses are necessary because operator + ratio> having higher priority.


Guess you like

Origin blog.51cto.com/14311187/2415647