第一章.java基本语法
1.1关键字
定义:
被Java语言赋予了特殊含义,用做专门用途的字符串(单词)
特点:
关键字中所有字母小写
1.2保留字
定义:
现有Java版本尚未使用,但是以后可能会作为Java关键字使用(goto const)
1.3标识符
定义:
是可以自己起名字的地方都叫标识符(类名,变量名,方法名,接口名~~~~)。
命名规则(不遵守,编译不可以通过):
1.由26个英文字母大小写,0~9,_或$组成
2. 数字不可以开头
3. 不可以使用关键字和保留字,但能包含关键字和保留字
4. Java中严格区分大小写,长度无限制
5. 标识符不能包含空格
命名规范:(不遵守,编译可以通过。建议遵守,方便使用):
- 包名:多单词组成时,所有字母都小写:xxyyzz
- 类名,接口名:多单词组成时,所有单词的首字母都大写:XxYyZz
- 变量名,方法名:多单词组成时,第一个单词首字母小写,第二个单词开始每个单词首字母大写:xxYyZz
- 常量名:所有字母都大写,多单词时每个单词用下划线链接:XX_YY_ZZ
1.4变量
格式:
变量类型 变量名 = 变量值
注意:
- Java中每个变量必须先声明,后使用。
- 使用变量名来访问这块区域的数据。
- 变量的作用域:其定义在{}内。
- 变量只有在其作用域内才有效。
- 同一个的作用域内,不能定义重名的变量
分类(按数据类型分):
java的整型常量默认为 int 型,声明long型常量须后加‘l’或‘L’
java 的浮点型常量默认为double型,声明float型常量,须后加‘f’或‘F’
字符型变量的三种表现形式:
1.字符常量是用单引号(‘ ’)括起来的单个字符。例如:char c1 = ‘a’; char c2 = ‘中’; char c3 = ‘9’;
2. Java中还允许使用转义字符‘\’来将其后的字符转变为特殊字符型常量。例如:char c3 = ‘\n’; // '\n’表示换行符
3.直接使用 Unicode 值来表示字符型常量:‘\uXXXX’。其中,XXXX代表一个十六进制整数。如:\u000a 表示 \n
布尔型:boolean
. 只能取两个值之一:true false
. 常常在条件判断,循环结构中使用
1.5基本数据类型之间的运算规则
1.自动类型提升:
2.强制类型转换
自动类型转换的逆过程,将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符:(),但可能造成精度降低或溢出,格外要注意。
public class Test1 {
public static void main(String[] args) {
short s = 1;
//s = s+1;//错
s =(short)( s+1);
System.out.println(s);
}
}
1.6String
public class Test1 {
public static void main(String[] args) {
String s1="王祖贤";
String s2="很美";
System.out.println(s1 + s2);//王祖贤很美
}
}
1.7进制
1.8运算符
- 算术运算符 + - * / % ++ – +(连接)
- 赋值运算符 = (+= -= *= /=)
(+= -= *= /=)不会改变变量本身数据类型
public class Test1 {
public static void main(String[] args) {
short i=10;
i+=2;
//i=i+2;错误(数据类型)
System.out.println(i);//12
}
}
- 比较运算符 == != < > <= >=
- 逻辑运算符 & && | || ! ^(只适用boolean)
(&& ||)只要前面的可以确定结果就不用计算后面的了
public class Test1 {
public static void main(String[] args) {
boolean b = false;
int num=10;
if(b &&(num++>0)){
System.out.println(num);
}else{
System.out.println(num);
}
}
}
//结果为10
public class Test1 {
public static void main(String[] args) {
boolean b = false;
int num=10;
if(b &(num++>0)){
System.out.println(num);
}else{
System.out.println(num);
}
}
}
//结果为11
- 位运算符 <<(左移) >>(右移) >>>(无符号右移) &(与) |(或) ^(异或) ~(取反运算)
位运算是直接对整数的二进制进行的运算
位运算符应用:m = k ^ n = (m ^ n) ^ n(两个数交换值)
- 三元运算符
public class Test1 {
public static void main(String[] args) {
int i1=12;
int i2=15;
int i3=52;
int max=(i1>i2) ? i1 : i2;
int max1=(max>i3) ? max : i3;
System.out.println(max1);
}
}
//结果为52
运算符优先级
只有单目运算符、三元运算符、赋值运算符是从右向左运算的。
1.9Scanner(从键盘输入数据)
实现步骤:
1.导包:import java.util.Scanner;
2.Scanner的实例化:Scanner scan = new Scanner(System.in)
3.调用Scanner类的相关方法,来获取指定类型的变量
举例:
import java.util.Scanner;//步骤一
public class Test1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);//步骤二
System.out.print("请输入你的姓名:");
String name = scan.next();//步骤三
System.out.print("请输入你的年龄:");
int age = scan.nextInt();//步骤三
System.out.print("请输入你的身高:");
float height= scan.nextFloat();//步骤三
System.out.print("请输入你的体重:");
double weight= scan.nextDouble();//步骤三
System.out.print("你是否单身?(true /false)");
Boolean love= scan.nextBoolean();//步骤三
}
}
输出结果:
请输入你的姓名:王祖贤
请输入你的年龄:18
请输入你的身高:178
请输入你的体重:100
你是否单身?(true /false)true
1.10if-else
格式:
1.
if(条件表达式){
代码块
}
//代码块只有一行时,{}可以去掉。
2.
if(条件表达式){
代码块
}else{
代码块
}
3.
if(条件表达式){
代码块
}
else if(条件表达式){
代码块
}else{
代码块
举例:
public class Test1 {
public static void main(String[] args) {
int heartBeats = 78;//心跳
if(heartBeats <60 || heartBeats >100){
System.out.println("你需要去医院。");
}
System.out.println("健康");
int age =23;
if(age < 18){
System.out.println("你未成年。");
}else{
System.out.println("你成年了。");
}
if(age < 0){
System.out.println("你输入错误.");
}else if(age < 18){
System.out.println("你未成年。");
}else{
System.out.println("你成年了。");
}
}
}
输出结果:
健康
你成年了。
你成年了。
if()
if()
else//else遵循就近原则
1.11switch-case
格式:
switch(表达式){
case 常量1:
执行语句1;
break;
case 常量2:
执行语句2;
break;
.....
default:
执行语句n;
break;
}
说明:
1.根据switch表达式中的值,依次匹配各case中的常量,一旦匹配成功,则会进入相应的case中,调用执行语句,直到遇到break结束。
2.遇到break就跳出switch-case循环。
3.switch中的表达式只能是如下5种类型:byte short char int 枚举类型 String类型
4.case后只能声明变量,不能声明范围
5.break是可选的
6.default:相当于if-else结构中的else , defualt是可选的,位置也是可选的
1.12for循环
举例:
package day002;
public class Test1 {
public static void main(String[] args) {
for(int i = 0;i < 10;i++){
System.out.println(i);
}
}
}
for循环中break关键字的使用:
一旦在循环中执行break,就跳出循环。
1.13while
格式:
初始化条件;
while(循环条件){
循环体;
迭代条件;
}
举例:
package day003;
public class WhileTest {
public static void main(String[] args) {
int i = 1;//初始化条件
while(i <= 10){
if(i % 2 == 0){
System.out.println(i);
}
i++;//迭代条件
}
}
}
1.14do-while
格式:
初始化条件;
do{
循环体;
迭代条件;
}while(循环条件);
说明:
do-while至少会执行一次循环体
举例:
package day003;
public class WhileTest {
public static void main(String[] args) {
int i = 1;
do{
if(i % 2 == 0){
System.out.println(i);
}
i++;
} while(i <= 10);
}
}
关于循环说明:
1.不在循环条件部分限制循环次数的结构用:for (;;)或while(true)
2.结束循环有几种方式:方式一:循环条件部分返回false
方式二:在循环体中,执行break
1.15获取随机数
步骤:
double value = Math.random();//只能是double类型
System.out.println(value);
会随机生成一个double类型的数,范围[0.0,1.0)
举例:
package day001;
public class MathTest {
public static void main(String[] args) {
double value = Math.random();
System.out.println(value);
}
}
** 例题:**
如何随机获取两位整数
package day001;
public class MathTest {
public static void main(String[] args) {
int value = (int)(Math.random() * 90 +10);
System.out.println(value);
}
}
1.16break和continue的区别
相同点
都是结束的最近的一层循环
后面不能声明执行语句
都可以结束指定标志的一层循环
不同点
break 在switch-case 循环结构中使用,表示结束当前循环
continue 在循环结构中使用,表示结束当次循环
相同点举例
break 表示结束当前循环
continue 表示结束当次循环
package day004;
public class Test1 {
public static void main(String[] args) {
for(int i = 1;i <= 10;i++){
if(i % 4 == 0){
break;
}
System.out.println(i);
}
}
}
输出结果
1
2
3
package day004;
public class Test1 {
public static void main(String[] args) {
for(int i = 1;i <= 10;i++){
if(i % 4 == 0){
continue;
}
System.out.println(i);
}
}
}
输出结果
1
2
3
5
6
7
9
10
后面不能声明执行语句
package day004;
public class Test1 {
public static void main(String[] args) {
for(int i = 1;i <= 10;i++){
if(i % 4 == 0){
continue;
// System.out.println("王祖贤");错误
}
System.out.println(i);
}
}
}
不同点举例
都可以结束指定标志的一层循环 .
package day004;
public class Test1 {
public static void main(String[] args) {
label:for(int i = 1;i <= 4;i++){
for(int j = 1;j <= 10;j++) {
if (j % 4 == 0) {
continue label;
}
System.out.print(j);
}
}
}
}
输出结果
123123123123
package day004;
public class Test1 {
public static void main(String[] args) {
label:for(int i = 1;i <= 4;i++){
for(int j = 1;j <= 10;j++) {
if (j % 4 == 0) {
break label;
}
System.out.print(j);
}
}
}
}
输出结果
123