数据类型
关键字
关键字一共50个,其中const和goto是保留字。
此外,还有3个特殊值:true,false,null。它们看起来像关键字,但从技术角度,它们是特殊的布尔值和空值。
变量概念
变量就是用来存储数据的
变量定义方式
三种方式
方式1: 先声明 再赋值
方式2: 连声明 带赋值 写为一条语句
方式3: 同时声明多个同类型的变量(了解) 实际开发中不推荐使用 因为阅读性较差
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
// 方式1: 先声明 再赋值
int a;
a = 100;
System.out.println("变量a的取值为:" + a);
// 方式2: 连声明 带赋值 写为一条语句
int b = 123;
System.out.println("变量b的取值为:" + b);
// 未赋值的局部变量是不能使用的
int c, d, e = 55, f, g = 66;
System.out.println("变量e的取值为:" + e);
System.out.println("变量g的取值为:" + g);
System.out.println("变量c的取值为:" + c); // 报错
System.out.println("变量d的取值为:" + d); // 报错
System.out.println("变量f的取值为:" + f); // 报错
}
}
数据类型
注意:Java是强类型语言,变量的类型必须与数据的类型一致。
基本数据类型
整数类型
整数类型四种
byte
1个字节8位 数值范围 -128 ~ 127(1位是正[0]负[1] 2的7次方)
short
2个字节16位 数值范围 -32768 ~ 32767 (1位是正负 2的15次方)
int
4个字节32位 数值范围 -2147483648 ~ 2147483647 (1位是正负 2的31次方)
long
8个字节64位
byte取值范围?最大取值的原因?
-128 ~127
因为一个字节占8位,每一个都只能存储一个0或者一个1,最大的取值为每一位都存储一个1。
还要注意 计算机以首位(最高位)作为符号位 0表示正数 1表示负数,
所以 最大的取值为0111 1111 转换为十进制为127
Java中的每一个数据都有其对应的数据类型
int为整数的默认类型,如需为long类型赋值
如果取值范围超出了int 则需要在值的后面追加 "L"
如果取值范围在int范围以内 则可加 可不加
一个汉字 2 个字节
一个字母 1 个字节
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
byte b1 = 100;
System.out.println("b1 = " + b1);
byte b2 = -123;
System.out.println("b2 = " + b2); // soutv 自动生成打印上一行变量的输出语句
//byte b3 = -129; // 报错 超出范围
short s1 = 8956;
System.out.println("s1 = " + s1);
short s2 = -23451;
System.out.println("s2 = " + s2);
// short s3 = -32769; // 报错 超出范围
int i1 = 895645;
System.out.println("i1 = " + i1);
int i2 = -561245;
System.out.println("i2 = " + i2);
//int i3 = 2147483648; // 报错 超出范围
// int为整数的默认类型,如需为long类型赋值
// 如果取值范围超出了int 则需要在值的后面追加“L”
// 如果取值范围在int范围以内 则可加 可不加
long l1 = 567854651;
System.out.println("l1 = " + l1);
long l2 = -45127856;
System.out.println("l2 = " + l2);
long l3 = 2147483648L;
System.out.println("l3 = " + l3);
long l4 = 100;
}
}
浮点类型
浮点类型 : 这两种类型都是近似值 不是精确值 如需精确值 使用后续学习的 BigDecimal
类
float类型赋值时,需要在数值后加"F"。
double类型赋值时,超出float类型取值范围,需要在数值后加"D"
double为浮点数的默认类型,如需为float类型赋值,需要在数值后加"F"
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
float f1 = -340000000000000000000000000000000000000F;
System.out.println("f1 = " + f1);
float f2 = -0.0000000000000000000000000000000000000000000014F;
System.out.println("f2 = " + f2);
float f3 = 0.00014F;
System.out.println("f3 = " + f3);
float f4 = 340000000000000000000000000000000000000F;
System.out.println("f4 = " + f4);
// 给double类型赋值 如果取值范围超出了float的取值范围 必须在末尾加上D 推荐大写
double d1 = 2.5;
System.out.println("d1 = " + d1);
double d2 = 3.6;
System.out.println("d2 = " + d2);
}
}
long类型和float类型取值(谁大)
为什么 float 类型的字节数 小 但是取值比long类型大
这是因为 float 和 long 的数据存储方式和编码机制不同。尽管 float 的字节数更小(4 字节),它能够表示比 long 更大的范围,主要是因为 浮点数采用了科学计数法表示,而 long 是直接存储整数的值。
为什么 float 的范围更大?
指数部分:float 的 8 位指数(加上偏移值)允许表示 $2^{-126}$ 到 $2^{127}$ 之间的数,因此可以覆盖极大范围的值。
科学计数法:使用科学计数法表示数值,可以通过指数轻松扩展数值的范围,而不需要像 long 那样每一位都直接表示具体的值。
布尔类型
boolean类型 仅能描述true 或者 false
boolean类型可以直接赋值 true 或者 false 也可以赋值 最终结果为true或者false 的表达式
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
boolean bl1 = true;
System.out.println("bl1 = " + bl1);
boolean bl2 = false;
System.out.println("bl2 = " + bl2);
int a = 10;
int b = 20;
boolean bl3 = a > b;
System.out.println("bl3 = " + bl3);
boolean bl4 = a < b;
System.out.println("bl4 = " + bl4);
}
}
字符类型
char只能保存任意 一个
字符,使用 单引号
包裹一个内容
赋值方式
赋值方式1 直接使用英文的 `单引号` 包裹 一个 内容
赋值方式2 直接赋值在0~65535以内的整数
赋值方式3 使用英文单引号包括十六进制的Unicode字符值 必须以杠u开头
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
// 赋值方式1 直接使用英文的单引号包括一个内容
char ch1 = 'a';
System.out.println("ch1 = " + ch1);
char ch2 = '1';
System.out.println("ch2 = " + ch2);
char ch3 = '中';
System.out.println("ch3 = " + ch3);
char ch4 = '!';
System.out.println("ch4 = " + ch4);
char ch5 = '~';
System.out.println("ch5 = " + ch5);
// 赋值方式2 直接赋值在0~65535以内的整数
// ASCII 美国标准信息交换码 0 ~ 127
char ch6 = 65;
System.out.println("ch6 = " + ch6); // A
char ch8 = 66;
System.out.println("ch8 = " + ch8); // B
char ch9 = 67;
System.out.println("ch9 = " + ch9); // C
char ch7 = 97;
System.out.println("ch7 = " + ch7); // a
char ch10 = 48;
System.out.println("ch10 = " + ch10); // 0
// 中文汉字 以及 中文标点符号 将参考Unicode编码表 Unicode编码表是一个十六进制的编码表
// 记录了世界上绝大多数国加的语言 中文的取值范围是 : \u4e00(19968) ~ \u9fa5(40869)
char ch11 = 20013;
System.out.println("ch11 = " + ch11);
char ch12 = 20320;
System.out.println("ch12 = " + ch12);
char ch13 = 19967;
System.out.println("ch13 = " + ch13);
char ch14 = 19968;
System.out.println("ch14 = " + ch14);
char ch15 = 19969;
System.out.println("ch15 = " + ch15);
char ch16 = 40869;
System.out.println("ch16 = " + ch16);
// 赋值方式3 使用英文单引号包括十六进制的Unicode字符值 必须以杠u开头
char c1 = '\u4e2d';
System.out.println("c1 = " + c1);
char c2 = '\u4e00';
System.out.println("c2 = " + c2);
}
}
引用数据类型
String类型
String
类型 String是JDK提供的一个类,属于引用数据类型
,任何英文双引号
包括的内容 都是字符串
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "1234";
String str3 = "月";
String str4 = "Aqua";
String str5 = "\"\"";
char ch1 = '\''; // char只能保存任意 一个 字符
System.out.println("str1 = " + str1);
System.out.println("str2 = " + str2);
System.out.println("str3 = " + str3);
System.out.println("str4 = " + str4);
System.out.println("str5 = " + str5);
System.out.println("ch1 = " + ch1);
}
}
类
数组
对象
集合
接口
枚举
类型转换
自动提升 手动下降
自动类型提升
自动类型转换:自动类型提升
自动转换的条件
1.两种类型要相互兼容:数值与数值类型之间相互兼容。 例如short 兼容 byte 、 int兼容short等等
2.目标类型(等号左边)取值范围大于源类型(等号右边)取值范围
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
// byte 转 short
byte b1 = 10;
short s1 = b1;
System.out.println("s1 = " + s1);
// byte 转 int
short s2 = 2356;
int i1 = s2;
System.out.println("i1 = " + i1);
// int 转 long
int i2 = 895645;
long l1 = i2;
System.out.println("l1 = " + l1);
// long 转 float
long l2 = 56231245L;
float f1 = l2;
System.out.println("f1 = " + f1);
// float转double
float f2 = 3.5F;
double d1 = f2;
System.out.println("d1 = " + d1);
// byte 转 short int long float double
byte b2 = 100;
short e = b2;
int a = b2;
long b = b2;
float c = b2;
double d = b2;
}
}
涉及运算符操作时,触发自动自动类型提升
进行算数运算时:
两个操作数有一个为double,计算结果提升为double。
如果操作数中没有double,有一个为float,计算结果提升为float。
如果操作数中没有float,有一个为long,计算结果提升为long。
如果操作数中没有long,有一个为int,计算结果提升为int。
如果操作数中没有int,均为short或byte或者char,计算结果仍旧提升为int。
总结:多个操作数进行数学计算,结果提升为这些操作数中取值范围最大的类型,如果均为byte、short、char则结果提升为int类型。
特殊:任何类型与String相加(+)时,实为拼接,其结果为String。
强制类型转换
强制类型转换条件
两种类型要相互兼容:数值与数值之间相互兼容。
目标类型(等号左边)取值范围小于源类型(等号右边)。
除了将浮点类型强制转换为整数类型后,小数点之后全部抹去,其他的都能完整保存
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
// short类型 强转为 byte
short s1 = 127;
byte b1 = (byte)s1;
System.out.println("short类型 强转为 byte " + b1); // 127
// int类型 强转为 short
int i1 = 100;
short s2 = (short)i1;
System.out.println("s2 = " + s2); //100
// long类型 强转为 int
long l1 = 123;
int i2 = (int)l1;
System.out.println("i2 = " + i2); // 123
// float类型 强转为 long
// 将浮点类型强制转换为整数类型 小数点之后全部抹去
float f1 = 3.5F;
long l2 = (long)f1;
System.out.println("l2 = " + l2); // 3
// double类型 强转为 float
double d1 = 3.5;
float f2 = (float)d1;
System.out.println("f2 = " + f2); //3.5
// double类型 强转为 long int short byte char
double d2 = 2.5;
long l3 = (long)d2;
int i3 = (int)d2;
short s3 = (short)d2;
byte b3 = (byte)d2;
char ch1 = (char)d2;
}
}
强制类型转换特殊情况
强制类型转换特殊情况:将超出目标类型的数值进行转换
取值范围大强制转换取值范围小时,二进制数据会高位舍弃,低位保留
布尔类型不可与其他类型转换
示例1
定义一个 short类型的变量 short s1 = 257 他的二进制代码表示
short 0000 0001 0000 0001
强制类型转换为 byte类型 byte b1 = (byte) s1
二进制数据会高位舍弃,低位保留,所以b1的十进制为1
byte 0000 0001
示例2
定义一个 short类型的变量 short s2 = 128 他的二进制代码表示
short 0000 0000 1000 0000
强制类型转换为 byte类型 byte b2 = (byte) s2
二进制数据会高位舍弃,低位保留,所以b2的十进制为 -128
byte 1000 0000
原码 反码 补码
计算机统一以补码的形式表示所有的整数。
定律:正数的原,反,补码都一样(三码合一)
正数
补码
表示不需要计算(三码合一)负数
补码
表示需要计算,具体是:先将其对应的正数的二进制表示(原码),然后除符号位以外每一个都取反,然后加 1。负数特殊情况:最小的负数是推算计算,类似于取极限,例如-128,是比最小-127还小1,即-128=-127-1
正数和负数都是通过
补码
的形式在(计算机存储)中正数和负数的取值使用
原码
(方便人类计算成十进制)
原码、反码和补码的区别
符号位:二进制的最高位
原码:一个整数的二进制表示,包括符号位和数值部分。对于正数,符号位为 0,数值部分与无符号整数相同。对于负数,符号位为 1,数值部分与无符号整数相同(0除外)。
反码:对于正数,反码就是它的原码。对于负数,反码是将其原码中的数值部分逐位取反(符号位不变)。
补码:对于正数,补码和原码、反码一样。对于负数,补码是反码加 1。
示例
假设我们以 8 位为例 byte 类型
表示数字 +5
原码:00000101
反码:00000101
补码:00000101
比如对于 -5
原码:10000101(人类计算)
反码:11111010
补码:11111011(计算机存储)
从补码的角度看负数的大小
补码 1 0000000 就是最小的负数
补码 1 1111111 就是最大的负数
-1 的原码是 1000 0001
-1 的反码是 1111 1110
-1 的补码是 1111 1111
-128的补码是由-127-1推算过来的
127的原码是 0111 1111
-127的原码 1111 1111
-127的反码 1000 0000
-127的补码 1000 0001 (计算机存储)
-128的补码 1000 0000 (计算机存储)
+128的原码 byte类型不能表示 +128,最大只有+127(0111 1111)
如果硬要 +1 则
127 + 1 = -128
127的原码是 0111 1111
127的补码是 0111 1111 (计算机存储)
+1操作后
127 + 1 是 1000 0000 (计算机存储) => -128
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
// s1 原码 0000 0001 0000 0001
// s1 反码 0000 0001 0000 0001
// s1 补码 0000 0001 0000 0001
short s1 = 257;
// b1 原码 0000 0001
// b1 反码 0000 0001
// b1 补码 0000 0001
byte b1 = (byte)s1;
System.out.println("b1 = " + b1); // 1
// s2 原码 0000 0000 1000 0000
// s2 反码 0000 0000 0111 1111
// s2 补码 0000 0000 1000 0000
short s2 = 128;
// b2 补码 1000 0000 (计算机存储的)
// b2 反码 1111 1111
// b2 原码 1000 0000 => -128 (人类计算的)
byte b2 = (byte)s2;
System.out.println("b2 = " + b2); // -128
// s3 原码 0000 0000 1000 0001
// s3 反码 0000 0000 1000 0001
// s3 补码 0000 0000 1000 0001
short s3 = 129;
// b3 补码 1000 0001 (计算机存储的)
// b3 反码 1111 1110
// b3 原码 1111 1111 => -127 (人类计算的)
byte b3 = (byte) s3;
System.out.println("b3 = " + b3); // -127
}
}
为什么Java中 byte 类型的取值 为 -128~127 而不是 -128~128
Java 中 byte 类型的取值范围是 -128 到 127,而不是 -128 到 128,原因与它的二进制表示和计算机中整数的存储方式有关。
byte 类型是一个 8 位的有符号整数类型。在计算机中,整数的表示通常使用 补码 方式。
1. 8 位二进制数的补码表示
在 8 位补码中,最高位(即第 8 位)是符号位,用来表示正数或负数。
剩余的 7 位用于表示数值大小。
2. 补码表示法的原理
正数的二进制表示和无符号整数相同。
负数的表示通过补码形式来表示,具体是:先将其对应的正数的二进制表示取反(每一位取反),然后加 1。
3. 字节的范围
8 位二进制数的最大值是 11111111(即 255),如果按照无符号整数来解读,则范围是 0 到 255。
但由于 Java 中的 byte 是有符号的,因此它的值范围被拆分为负数和正数:
最大正数:01111111(127)
最小负数:10000000(-128)
这就是为什么 byte 类型的取值范围是 -128 到 127。
运算符
算数运算符
算数运算符
+ - * / %
自增和自减
++表示自增1
--表示自减1
注意:
++ 或者 -- 如果是单独作为一条语句书写 在前在后没有区别
如果不是单独作为一条语句书写 在前在后有区别
如果++ 或者 -- 在前, 先执行++ 或者 -- ,再执行其他的
如果++ 或者 -- 在后, 先执行其他的,再执行++或者--
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a + b); // 30
System.out.println(a - b); // -10
System.out.println(a * b); // 200
System.out.println(a / b); // 0
int c = 5;
System.out.println(a % c); // 求余数 注意跟除法的区别
System.out.println("-----------------------------------------------------");
// ++ 和 --
// ++表示自增1
// --表示自减1
// ++ 或者 -- 如果是单独作为一条语句书写 在前在后没有区别
// 如果不是单独作为一条语句书写 在前在后有区别
// 如果++ 或者 -- 在前, 先执行++ 或者 -- ,再执行其他的
// 如果++ 或者 -- 在后, 先执行其他的,再执行++或者--
int d = 10;
d++; // 结果等同于 d = d + 1;
System.out.println("d = " + d); // 11
int e = 10;
++e;
System.out.println("e = " + e); // 11
int f = 10;
f--;
System.out.println("f = " + f); // 9
int g = 10;
--g;
System.out.println("g = " + g); // 9
System.out.println("-----------------------------------------------------");
int h = 10;
int i = h++;
System.out.println("i = " + i); // 10
System.out.println("h = " + h); // 11
System.out.println("-----------------------------------------------------");
int j = 10;
int k = ++j;
System.out.println("k = " + k); // 11
System.out.println("j = " + j); // 11
}
}
赋值运算符
= 直接赋值
+= 求和后赋值
-= 求差后赋值
*= 求积后赋值
/= 求商后赋值
%= 求余后赋值
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
int a = 10;
a += 10; // 结果 a = a + 10; 20
System.out.println("a = " + a);
a -= 5; // a = a - 5; 15
System.out.println("a = " + a);
a *= 10; // a = a * 10; 150
System.out.println("a = " + a);
a /= 2; // a = a / 2; 75
System.out.println("a = " + a);
a %= 3; // a = a % 3; 0
System.out.println("a = " + a);
System.out.println("-----------------------------------");
short s1 = 10;
s1 += 10; // 这里JVM帮我们实现 隐式 的类型转换
System.out.println("s1 = " + s1);
short s2 = 10;
s2 = (short) (s2 + 10); // 这里因为是我们完整书写表达式进行计算 和 赋值 所以 需要手动类型转换
}
}
关系运算符
关系运算符最终的结果为布尔类型
, 表示两个操作数或者两个表达式之间的关系是否成立。
> 大于
< 小于
>= 大于等于
<= 小于等于
== 等于
!= 不等于
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a > b); // false
System.out.println(a < b); // true
System.out.println(a >= b); // false
System.out.println(a <= b); // true
System.out.println(a == b); // false
System.out.println(a != b); // true
}
}
逻辑运算符
&& 短路与 两个操作数同时为真,结果为真
|| 短路或 两个操作数,有一个为真,即为真
! 非 取反
& 与 要求两个或者多个条件同时成立 则最终结果为true
没有短路的效果 不管前边的条件结果如何 都将执行完所有的条件
&& 短路与 要求两个或者多个条件同时成立 则最终结果为true
短路与 有短路的效果 如果前边的条件不成立 则后续的条件不再执行
| 或 要求两个或者多个条件 至少有一个成立 则最终结果为true
没有短路的效果 不管前边的条件结果如何 都将执行完所有的条件
|| 短路或 要求两个或者多个条件 至少有一个成立 则最终结果为true
短路或 有短路的效果 如果前边的条件 已经成立 则后续的不再执行
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
System.out.println(a < b & b > c ); // true
System.out.println(a < b && b > c ); // true
System.out.println("-------------------------------------");
System.out.println(a < b & b < c); // false
System.out.println(a < b && b < c); // false
System.out.println("-------------------------------------");
System.out.println(a < b | b > c ); // true
System.out.println(a < b || b > c ); // true
System.out.println("-------------------------------------");
System.out.println(a < b | b < c); // true
System.out.println(a < b || b < c); // true
System.out.println("-------------------------------------");
System.out.println(a > b | b > c); // true
System.out.println(a > b || b > c); // true
System.out.println("-------------------------------------");
System.out.println(a > b | b < c); // false
System.out.println(a > b || b < c); // false
System.out.println("-------------------------------------");
boolean flag1 = true;
System.out.println(!flag1);
boolean flag2 = false;
System.out.println(!flag2);
}
}
三元运算符
A ? B : C A 成立,表达式的值取B,否则取C
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
int age = 19;
System.out.println(age >= 18 ? "成年了" : "未成年");
int a = 1;
int b = a > 5 ? 55 : 66;
System.out.println("b = " + b);
}
}
位运算符
如果最高位是1 则移动完以后 空缺位补1
如果最高位是0 则移动完以后 空缺位补0
<< 左移 : 左移几位表示乘以2的几次方 (有可能会出现负数的情况)
>> 右移 : 右移几位表示除以2的几次方
>>> 无符号右移 不管最高位是0还是1 空缺位统一以0填充 即 最终的结果都是一个正数
应用场景:比如我们要获取某个元素应该存放在数组中的位置,可以使用无符号右移,因为无符号右移绝对不可能得到一个负数,而数组的下标也不为负数
& 与
两个数的相同二进制位进行与运算,都为1则结果为1,否则其他情况最终结果都为0。
| 或
两个数的相同二进制位进行或运算,只要有一个为1,或者两个都为1,则结果为1,其他的情况结果为0。
^ 异或
两个数的相同二进制位进行异或运算,不同则结果为1,相同则结果为0。
~ 取反
包括符号位在内二进制数,每一位取反,是0改为1,是1改为0。默认数字没有声明类型,则是int类型4字节32位
public class TestBitOperator {
public static void main(String[] args) {
int a = 1;
System.out.println(a << 1); // 2
System.out.println(a << 2); // 4
System.out.println(a << 3); // 8
System.out.println(a << 4); // 16
System.out.println(a << 31); // -2147483648
System.out.println(a << 32); // 1 相当于没有移动
System.out.println("------------------------------------------");
System.out.println(8 >> 1);
System.out.println(8 >> 2);
System.out.println(8 >> 3);
System.out.println(8 >> 4);
System.out.println(-20 >> 1); // -10
System.out.println(-20 >> 2); // -5
System.out.println("------------------------------------------");
System.out.println(8 >>> 1);
System.out.println(8 >>> 2);
System.out.println(8 >>> 3);
System.out.println(8 >>> 4);
System.out.println(-20 >>> 1); //
System.out.println(-20 >>> 2); //
System.out.println("------------------------------------------");
System.out.println(25 & 21); // 17
System.out.println(25 | 21); // 29
System.out.println(25 ^ 21); // 12
System.out.println(~5);
System.out.println(~-6);
}
}
进制
二进制以 0B开头
八进制以 0 开头
十六进制以 0X 开头
示例
package com.yuluochenxiao.test;
public class FunctionTest {
public static void main(String[] args) {
//整数的赋值方式 4种方式
// 方式1 二进制 以0B开头
int a = 0B11;
System.out.println("a = " + a); // 3
// 方式2 八进制 以0开头
int b = 011;
System.out.println("b = " + b); // 9
// 方式3 十六进制 以0X开头
int c = 0X4e2d;
System.out.println("c = " + c); // 20013
}
}
转义字符
转义字符 : 可以用于保存一些特殊符号 或者 实现一些 特殊的效果
\n 换行符
\t 缩进
\\ 反斜线
\' 单引号
\" 双引号
标识符命名规范
凡是需要自定义名称的内容都属于标识符
注意:标识符名称定义不要与Java关键字、保留字冲突
标识符的构成:
简写:字(字母) 下(下划线) 美(美元符号) 人(人民币符号) 数(数字)骆驼(驼峰命名)
详细说明:标识符名称可以以字母 、下划线、美元符号、人民币符号开头,可以包含数字,不能以数字开头。
实际开发中只推荐使用单词。
类名
类名:由一个或者多个单词组成 ,每个单词首字母大写
举例: Student StringBuffer StringBuilder (大写驼峰)
变量名
变量名: 由一个或者多个单词组成 ,每个单词首字母小写
方法名、参数名、成员变量、局部变量都统一使用 lowerCamelCase 风格
举例:studentName studentAge personWorkAge (小写驼峰)
规范
见名知义 有意义
实际开发中只推荐使用单词
Java开发手册 阿里巴巴
https://www.w3cschool.cn/alibaba_java/alibaba_java-s7k33eq1.html
常量
程序中无法改变的数据 称之为常量
package com.yuluochenxiao.test;
/**
* 常量: 程序中无法改变的数据 称之为常量
*/
public class TestConstant {
public static void main(String[] args) {
int a = 100;
a = 1;
System.out.println(100); // 100 就是常量
System.out.println(1.5); // 1.5 就是常量
System.out.println(2.5F);
System.out.println('a');
System.out.println(true);
System.out.println(false);
System.out.println("false");
}
}
局部变量
在方法内定义的变量称为局部变量
局部变量 | 描述 |
---|---|
定义位置: | 定义在方法体内 |
作用范围: | 离当前变量最近的大括号以内 |
重名问题: | 重合的作用范围以内 不能重名 |
默认值: | 局部变量没有默认值 必须先赋值才能使用 |
生命周期: | 随着方法的入栈(压栈)而生效 随着方法的出栈(弹栈)而死亡 |
存储位置: | 基本数据 类型存在栈中 ,引用数据类型 名字在栈 值在堆 |
方法中的局部变量:随着方法的入栈而生效,栈内会开辟一个内存空间
如果变量的类型是基本数据类型,数据存储在栈中
如果变量的类型是引用数据类型,然后会在堆中开辟一个空间,把数据存储在堆中,栈中存储的是堆中的地址。
栈中的引用,指向堆中的空间(也叫做指针)
示例
public class TestLocalVariable {
public static void main(String[] args) {
int a = 100;
int b = 100;
// int d ;
// System.out.println("d = " + d);
if(a > b){
System.out.println(a);
System.out.println(b);
int c = 123;
}else{
int c = 123;
System.out.println(a);
System.out.println(b);
}
}
}
存储位置
方法的生命周期:随着方法的入栈而生效,随着方法的出栈而死亡
变量的存储位置:基本数据类型存储在栈中,引用数据类型名字在栈中,值在堆中