02: Java basic grammar (a)

Java basic grammar

Java keywords and reserved words

Keyword (Keyword)


Keyword definitions and characteristics

defined: Java language is given a special meaning to the word
characteristics: keywords are all lowercase letters
Notes:
1) to true, false, null, not keywords! It is literal.
2) main is not a keyword, is a special word, JVM may be identified, the main function is a fixed format, as the program entry.

Reserved words (Reserved Word)

Use existing Java version yet, but future versions may use keywords to avoid these reserved words (goto, const) when naming identifiers

Identifiers and naming rules

To classes, methods, variables from the name , all they can from the name of the call identifier.
Define the legal rules for identifiers:
1) must start with a letter or an underscore or $ symbol, the remaining characters can be letters, numbers, symbols, and $ underlined.
2) only contains two special characters that underscore _ and dollar sign $. Does not allow any other special characters.
3) can not begin with numbers.
4) can not use Java keywords and reserved words, but can contain keywords and reserved words.
5) Java strictly case-sensitive.
6) Identifiers can not contain spaces.
* Use hump nomenclature, in order to improve readability, see the name to know Italian. Java in the name of the specification: [Reference: Alibaba Java Development Manual .pdf]
commonly used commands specifications:
1) the package name (package): multi-word all the letters are lowercase. xxxyyyzzz
2) class name Interface name: multi-word, the first letter of all words capitalized. XxxYyyZzz
3) variable and function names: multi-word, lowercase first letter of the first word, the second word began to capitalize the first letter of each word. xxxYyyZzz
4) Constant name: All the letters are capitalized. Each word is underlined when connecting more than words. XXX_YYY_ZZZ
* In order to improve readability, it is recommended to see to know the name of Italy. In Java using Unicode character set, so the identifier can also use Chinese characters to the statement, it is not recommended.

Java comments in

For the annotation caption interpreter is a comment. Benefits comments: improve the readability of the code.
Java comments in the format [three kinds]:

> for single-line and multi-line comments, annotated text will not be JVM (java virtual machine) interpreted. [See source file size attribute as Comparison intuitive understanding]
> annotation to the document, the java unique annotation, annotation wherein the parsed content may be provided by the JDK Javadoc tool, generating a page file embodied in the form of the program documentation. Notes is a programmer must have good programming practice. Their thoughts by commenting sorted out first, with the code to reflect. Because the code is only a reflection of ideological form only.

Variables and constants

Variable ( Variable )

A memory storage space, you can save the current data. During program execution, the value can be changed.
Using Variables Notes
1) must be declared and reused (not repeat the same statement in a variable scope after initialization)!
2) the variable must have a clear type (Java is a strongly typed language).
3) there is a variable scope (where the variables declared in the beginning of the {} block end). Variable scopes as small as possible.
4) local variable must be initialized before use! Member variables after the object is created with a default value, it can be directly used.
5) the local variables defined in the method is created when the process is loaded.
Local variables: internal variables defined in the method, called local variables (variables which code blocks).
Characteristics of local variables: Local variables must be defined after use; memory local variables allocated on the stack.
Why should define the variables: the same type used to keep constant storage, and can be used repeatedly
use variables Note:
(Effective between the pair {}) 1, variable scope
2, the format defined variables: variable name = data type initial value => X = int. 3;
. 3, naming local variables: first character lowercase, if a plurality of words, each word starts from the first character of the second word capitalized.
4, the final state variable naming conventions: All all uppercase characters, a plurality of words, separated by an underscore.

constant

在程序运行过程中,其值不可以改变的量。
Java中常量的分类
1)整数常量:所有整数
2)小数常量:所有小数
3)布尔(boolean)型常量:较为特有,只有两个数值。true false。
4)字符常量。将一个数字字母或者符号用单引号( ' ' )标识。
5)字符串常量。将一个或者多个字符用双引号(" ")标识。
6)null常量。只有一个数值就是:null.
Java中对于整数:有四种表现形式。
二进制:0,1 ,满2进1
八进制:0-7 ,满8进1. 用0开头表示。
十进制:0-9 ,满10进1.
十六进制:0-9,A-F,满16进1. 用0x开头表示。(数字0,并不是字母O)
注意事项:
1)字面量、常量和变量的运算机制不同,字面量、常量由编译器计算,变量由运算器处理,目的是为了提高效率。
2)不管是常量还是变量,必须先定义,才能够使用。即先在内存中开辟存储空间,才能够往里面放入数据。
3)不管是常量还是变量,其存储空间是有数据类型的差别的,即有些变量的存储空间用于存储整数,有些变量的存储空间用于存储小数。

Java的数据类型(基本数据类型、引用数据类型)

Java语言是强类型语言,对于每一种数据都定义了明确的具体数据类型,在内存中分配了不同大小的内存空间




数据类型的作用:
1)决定了占用内存空间的大小。
2)底层的存储的格式
3)决定了最终显式的形式。
*数据的具体类型有2种指定的方式:显式指定,隐式指定(通常是针对字面值。由编译器默认指定)
字符类型
char 类型:可以当作整数来对待。 无符号的短整型。没有负数.取值范围 [0-65535]
字符集:字符的集合。字符集合中的每一个字符都对应着一个唯一的整数。
1) ASCII:单字节字符集。只用到了一个字节的后7位。128个字符。
2) ISO8859-1:西欧使用的字符集,单字节字符集。256个字符。兼容了ASCII。
3) GB2312:双字节字符集。兼容了ASCII。主要收录了中文简体。
4) GBK:兼容了 gb2312.增加了繁体中文,还有各个少数民族的符号。
5) UTF-8:三字节的字符集。兼容了 ASCII。
6) Unicode:万国码。java 使用的字符集。jvm 使用的。保证所有的国家的符号都能识别。
转义字符(7种常用的转义字符)
\t: 水平制表符 对应着键盘上的Tab键。
作用: \t占用的半角的个数为[1-8]。从\t包括之前的输出的内容。总共占用的半角的位数必须是8的倍数。\t用来补齐8个半角位的倍数的。
\b:退格符: 对应着键盘上的 Backspace 键。
\r: 回车符: 让光标回到当前行的行首,后续的输出从行首输出。
\n:换行符: 让光标去下一行。Enter 键对应两个字符:\r\n 让光标去到下一行的行首。
\' : 字符单引号。
\" :字符双引号。
\\ :字符反斜杠
常用的基本数据类型

package com.boom.test;

/**
 * 基本数据类型代码演示
 * @author xiaoyin
 *
 */
public class Test {

    public static void main(String[] args) {
        // 数据类型 变量名 = 初始化值
        byte b = 3;
        System.out.println("输出byte类型=>b=" + b);
        short s = 4000;
        System.out.println("输出short类型=>s=" + s);
        int i = 12;
        System.out.println("输出int类型=>i=" + i);
        long l = 12345678;
        System.out.println("输出long类型=>l=" + l);
        long l2 = 541646465464465564l;/* 数值较大取值末尾+l */
        System.out.println("输出long类型=>l2=" + l2);
        // float f= 2.3;/*报错:单精度*/
        float f2 = 2.3f;/* 强制转换f */
        System.out.println("输出float类型=>f2=" + f2);
        double d = 2.5;
        System.out.println("输出double类型=>d=" + d);
        char ch = 'C';
        System.out.println("输出char类型=>ch=" + ch);
        boolean b1 = true;
        b1 = false;
        System.out.println(b1);

    }

}
基本数据类型代码演示

基本数据类型转换


方式:自动类型转换(也叫隐式类型转换), 强制类型转换(也叫显式类型转换)
类型转换:java 不支持 不同类型之间的运算。如果有不同类型之间的数据做运算的话,那么首先第一步做同类型转换。编译器来完成这种转换的。自动的完成的。
转换的规则
1:所有的 byte、short、char 的值将被提升到int型。
2:如果一个操作数是long型,计算结果就是long型;
3:如果一个操作数是float型,计算结果就是float型;
4:如果一个操作数是double型,计算结果就是double型。System.out.println(6 % 123456L);// 2.0
5:boolean类型不可以转换为其他的数据类型
上述的转换都是 编译器 来完成的。自动完成的。在编译期完成类型的转换,也就是将小类型的数据 当作大类型的数据来使用(如果需要)。
注意:1 和 2 自动转换 是 绝对安全的。 3、4 有可能存在精度的损失。java 语法上这样是允许的。
自动类型提升

强制类型转换(可能会丢失精度) 语法:(强制转换为的类型)(被转换的数据);

package com.boom.test;

/**
 * 数据类型转换
 * 
 * @author xiaoyin
 *
 */
public class Test {

    public static void main(String[] args) {
        int x = 2;
        byte b = 1;
        x = x + b;// 占用较小的自动提升为int型
        System.out.println("x=" + x);// x=3

        byte b1 = 2;
        b1 = (byte) (b1 + 5);// 强制类型转换
        System.out.println("b1=" + b1);
        // a=65+1=66 =>b
        System.out.println("转换后输出字符:" + (char) ('a' + 1));// 强制转换为char 输出b

        byte b2 = 3;
        byte b3 = 7;
        int s;/* 强制转换 */
        s = b2 + b3;// s=10
        System.out.println("s=" + s);

        // Integer.MIX_VALUE int类型最大最小值
        int max = Integer.MAX_VALUE;
        int min = Integer.MIN_VALUE;
        System.out.println("int_MAX=" + max + "\n" + "int_MIN=" +min); 

    } 

}
Data type conversion

Corresponding to the basic data types packaging

Guess you like

Origin www.cnblogs.com/cao-yin/p/11457363.html