java基本语法

1.注释

1.1单行注释

// 快捷键 ctrl + /数组

1.2多行注释

/*this

*/ 快捷键是:crtl+shift+/spa

1.3文档注释

/** * * * */ 快捷键是:/**+enter接口

主要做用就是给类或者方法,变量生成具体文档说明的时候使用文档

2.关键字

有特殊意义的单词字符串

注意:保留字包含了关键字input

abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return strictfp short static super switch synchronized this throw throws transient try void volatile while数学

3.基本数据类型(4类8种)

3.1整形(默认全部的整型都是int类型)

3.1.1 byte(1)it

-128——127class

3.1.2 short(2)

3.1.3int(4)

3.1.4long(8)

通常声明long类型的时候赋值都要在数字后面加上L

3.2浮点型(默认浮点型类型为double)

3.2.1float(4)

给float类型赋值的时候后面须要加上F

3.2.2 double(8)

3.3 字符型char(2)

字符在计算机中是不存在在,其实都是由数字关系进行比对转换而来的

ASCII 97 a 98 b

65 A

3.4 布尔型(boolean(大小由JVM决定))

结果只能是true或者 false

4.引用数据类型

4.1 类

4.2 接口

4.3 数组

5.数据类型的转换

5.1 自动的数据类型转换

小的数据类型转换为大的数据类型

5.2 强制数据类型转换

浮点型与整型之间的转换

注意转换以后会丢失精度

6.变量

6.1变量赋值

数据类型 变量名 = 值; int m = 10;

6.2标识符(变量名,类名,方法名)

1.不能和关键字冲突 2.只能出现字母数字,$和下划线_,而且首个字符不能是数字

3.见名知意

4.驼峰规则

变量名和方法名:首字母小写,若是有多个单词,其余单词首字母大写

类名:首字母大写,若是有多个单词,其余单词首字母大写

注意:标识符大小写敏感 int a = 19; int A = 20;a和A是不同的

7.运算符

7.1 算术运算符

+-*/

取模(取余)运算符 %

Scanner input = new Scanner(System.in);

 System.out.println("请输入一个四位整数:");

int num = input.nextInt();

System.out.println("你输入的是:" + num);

int ge = num % 10;

int shi = num / 10 % 10;

int bai = num / 100 % 10;

int qian = num / 1000;

System.out.println("" + ge + shi + bai + qian);

7.2赋值运算符

= 右边的值赋值给左边

+=

a += 2; //a = a + 2;

-= a -= 4; // a = a - 4; *=

/= %=

a *= 4; // a = a * 4 a /= 4;

 a %= 4;

7.3 关系运算符

关系运算符 > < >= <= == != 注意:关系运算符 最后的结果都是布尔值 Scanner input = new Scanner(System.in);

 System.out.println("请输入第一个数:");
int a = input.nextInt();

System.out.println("请输入第二个数:");
int b = input.nextInt();

System.out.println(a != b);

7.4 逻辑运算符

逻辑运算符(短路运算符) && || & | 最终返回的也是boolean的值 通常逻辑运算符都会和关系运算符结合一块儿使用

 && 当条件所有知足的时候才会返回true,不然就返回false
并且因为是短路运算,只有第一个条件可以直接获得答案,那么就不会再执行后面的结果

boolean b = 10 > 8 && 9 < 98 && 8 < 10; System.out.println(b); Scanner input = new Scanner(System.in); System.out.println("请输入小明语文成绩:"); int yuwen = input.nextInt(); System.out.println("请输入小明的数学成绩:"); int math = input.nextInt(); boolean flag = yuwen >= 90 && math >= 95; System.out.println(flag);

 || 只要有一个条件知足返回就是true,因为是短路运算,若是第一个条件知足,后面的就不用再运算了

boolean b = 10 > 8 || 8 < 7; System.out.println(b); Scanner input = new Scanner(System.in); System.out.println("请输入第一个数:"); int a = input.nextInt(); System.out.println("请输入第二个数:"); int b = input.nextInt(); boolean flag = ( a + b >= 10 ) && ( a / b > 4 ); System.out.println(flag);

7.5 布尔运算符

&& & || | ^ !

7.6 移位运算符

<< >>(有符号) >>>(无符号)

一个整数(先转换为2进制)向右移动一位 00101101 变为01011010

有符号:11111100 变为11111110

无符号:11111100 变为01111110

7.7自增自减运算符

++ --

++= --= 增量或者减量当即发生

=++ =-- 增量或者减量在语句中变量使用后发生

7.8 其余

正负号 new insteanceOf () [] .

8.运算符优先级

同一优先级的运算符,结合次序由结合方向所决定。(意思是从左到右)

简单记就是: ! > 算术运算符 > 关系运算符 > && > || > 赋值运算符

记不住,就打上()

9.String字符串型

注意:String类型并非基本数据类型中的一种

9.1字符串的赋值

String str = "中国真棒!"; System.out.println(str);

9.2字符串的拼接( + 在字符串中叫作字符串拼接符)

注意:只有有字符串的出现 + 就会被认为是字符串拼接符

String a = "中国,"; String b = "棒棒棒!,牛批!"; String c = a + b; System.out.println(c);

10.包

11.转义字符 \

相关文章
相关标签/搜索