1.关键字(keyword)和保留字(reserved word)
关键字定义:被Java语言赋予了特殊含义,用作专门用途的字符串(单词)
关键字特点:关键字中所有字母都是小写
保留字:目前还没赋予特定含义,以后有可能会赋予
2.标识符(identifier)
Java对各种变量、方法、类命名时使用的字符串叫标识符
凡是自己起名字的都叫标识符!
定义要求:
/*
标识符的使用:
1.凡是自己可以命名的都叫做标识符
例如:类名、变量名、包名...
2.命名规则:如果不遵守就编译不通过
*由26个英文字母大小写,0-9,_ $ 组成
*不能由数字开头
*Java中严格区分大小写,长度没有限制
*不能使关键字,但是可以包含关键字
*不能含有空格
命名的规范:如果不遵守,编译还是可以通过的,建议大家遵守
1.包名:多个单词所有的字母都是小写
2.类名、接口名:所有单词首字母单词大写,大驼峰
3.变量名、方法名:多单词组成时,就第一个单词小写,
第二个单词开始就首字母大写,例如xxxYyyOoo,小驼峰
4.常量名:所有字母都大写。多单词时,用下划线连接,例如SS_OO_PPP_JJ
在起名字时要起到见名知意
注意:因为Java采用的是unicode字符集,所以标识符也可以用中文,但是不建议使用。
*/
class IdentifierTest{
public static void main(String[] args){
int myNumber = 100;
System.out.println(myNumber);
}
}
3.变量
变量 = 变量类型+变量名+储存的值 Java是强类型变量:变量的类型必须确认!
/*
变量的使用
1.
Java定义变量的格式:变量类型 变量名(小驼峰) = 变量值;
2.声明:
变量必须先声明再使用,也就是说使用之前要定义+赋值好。
作用域:变量都定义在作用域内,只有在作用域内才是有效的,
出了作用域就失效了。
在同一个作用域内不能定义同名的变量。
*/
class VariableTest {
public static void main(String[] args) {
//变量的定义
int myAge = 12;
//变量的使用
System.out.println(myAge);
int myNumber;
myNumber = 10086;
System.out.println(myNumber);
}
}
3.1变量类型
/*
Java的数据类型
一、变量按照数据类型来划分:
①基本数据类型(8种)
数值型:
整数类型:byte(1个字节 = 8bite) short int long
浮点类型: float double
字符型:char
布尔型:boolean
②引用数据类型(3种)
类(class) - 字符串是一个类
接口(interface)
数组(array)
二、变量在类中声明的位置
成员变量
局部变量
*/
class VariableTest{
public static void main(String[] args) {
//1.整型:byte(1字节= 8bite) short(2字节) int(4字节) long(8字节)
//①byte范围:-128 ~ 127
byte b1 = 12;
byte b2 = -128;
//b2 = 128;//超出上述范围,编译不通过
System.out.println(b1);
//System.out.println(b2);
//② 声明:long型变量必须以l或L来结尾!
short s1 = 22;
int i2 = 56556;
long l6 = 5445464464L;
//③常用int来定义整型变量。
/*2.浮点类型: float(单精度,4字节,尾数精确到七位有效数字)
double(双精度,8字节,精度高),通俗来书就是带小数点的
*/
//①表示带小数点的数值
//②float表示数值的范围比long还大
//定义float类型变量时,必须以f或者F结尾
double f1 = 3.14F;
System.out.println(f1);
//通常定义浮点型使用double!
//3.字符型:char(一个字符等于两个字节)
//①定义char变量 通常 使用一对单引号 '', >> 内部只能写一个字符!
char c1 = 'a';
//编译不通过
//c1 = 'AB';
System.out.println(c1);
char c2 = '1';
char c3 = '中';
//②表示方式:1.声明一个字符 2.转义字符 3.直接使用Unicode值来表示字符型常量
char c5 = '\n';//换行符
System.out.print("hello" + c5);
System.out.println("world");
char c6 = '\t';//制表符
System.out.print("hello" + c6);
System.out.println("world");
//Unicode字符集:
char c7 = '\u0043';
System.out.println(c7);
//4.布尔型:boolean
//①只能够有两个值之一,true、false
//②常常在条件判断、循环结构中使用
boolean bb1 = true;
System.out.println(bb1);
boolean isMarried = true;
if(isMarried){
System.out.println("不能参加\"单身\"party");
}else{
System.out.println("多交朋友");
④通常用‘’
char cc = '1';
char cc1 = 97;// 输出cc1后,输出结果是a,在开发中很少见
// 因为97对应的ascl码是a
}
}
}
补充:char 字符型变量也可以不用单引号:
class ReviewTest {
public static void main(String[] args) {
char c1 = 'a';
char c2 = 97;
System.out.println(c2);//输出结果为a ,也就是说输出了97对应的阿斯克码值。
char c3 = 98;
char c4 = '5';
//上面两个5是不一样的
System.out.println(c3);//输出结果为5对应的阿斯克码值:b
}
}
3.2变量之间的运算规则:
1.自动类型提升:
/*
8种基本数据类型之间的运算规则:
前提:不包含第八种布尔型boolean,所以这里只讨论7中变量之间的运算
1.自动类型提升:
byte 、char、short--> int--> long--> float--> double
当容量小的数据类型变量和容量大的数据类型变量做运算时,结果自动提升为容量大的数据类型。
特别的:当byte 、char、short三种变量互相互相或者本身做运算时,结果是int类型!
2.强制类型转换:是自动类型提升的逆运算。
说明:此时的容量大小指的是,表示数的范围的大和小。比如:float的容量要大于long的容量。
*/
class VariableTes2{
public static void main(String[] args) {
byte b1 = 2;
int i1 = 12;
//编译不通过:
//byte i3 = b1 + i1;
int i3 = b1 + i1;
System.out.println(i3);
float f = b1 + i1;
System.out.println(f);
//****************************
char c1 = 'a';//97
int i5 = 10;
int i4 = c1 + i5;
System.out.println(i4);
short s2 = 10;
int s3 = s2 +c1;
}
}
2.强制类型转换:
/*
2.强制类型转换:是自动类型提升的逆运算。
1.需要使用强转符:()
2.注意点:强制类型转换 可能 会导致精度损失。
*/
class VariableTest3 {
public static void main(String[] args) {
//精度损失1:
double d1 =12.3;
int i1 = (int)d1;//截断操作!
System.out.println(i1);
//没有精度损失1:
long l1 = 16L;
short s1 = (short)l1;
System.out.println(l1);
//精度损失2:
int i2 = 128;
byte b1 = (byte)i2;
System.out.println(b1);//-128
}
}
补充两个可能遇到的情况>>定义float和long类型变量是直接在末尾加上F、L
class VariableTest4 {
public static void main(String[] args) {
//1.编码情况:
long l = 123465;
System.out.println(l);
//过大的整数,编译失败
//long l = 15554556658648645;//不加L,默认为int,超过了int的数值范围,所以报错了
//********************
float f1 = 12.3;
//2.编码情况2:
//对于整型常量,默认为int类型;
//对于浮点型常量,默认为double类型;
byte b = 12;
byte b1 = b + 1;
}
}