目录
一、字面常量
我们都知道,固定不变的值就称作常量
字面常量的分类:
- 字符串常量:由" "括起来的,比如“2023”、“hello”、“你好呀”。
- 整形常量:程序中直接写的整数,比如:100、1000
- 浮点数常量:程序中直接写的小数,比如:3.14、0.49
- 字符常量:由' '括起来的单个字符,比如:‘A’、‘1’
- 布尔常量:只有true和false两种
- 空常量:null
public class Test1 {
public static void main(String[] args) {
System.out.println("Hello world!");
System.out.println(100);
System.out.println(3.14);
System.out.println('B');
System.out.println(true);
System.out.println(false);
}
}
二、数据类型
Java中数据类型主要有两类:基本数据类型和引用数据类型(这里就先不介绍了)
基本数据类型从总的上来说有四类:整型、浮点型、字符型和布尔型
细分的话是有八种: 字节型(byte)、短整型(short)、整型(int)、长整型(long)、单精度浮点数(float)、双精度浮点数(double)、字符型(char)、布尔型(boolean)
数据类型 | 关键字 | 内存占用 | 范围 |
字节型 | byte | 1 字节 | -128 ~ 127 |
短整型 | short | 2 字节 | -32768 ~ 32767 |
整型 | int | 4 字节 | ~ |
长整型 | long | 8 字节 | ~ |
单精度浮点数 | float | 4 字节 | 有范围,一般不关注 |
双精度浮点数 | double | 8 字节 | 有范围,一般不关注 |
字符型 | char | 2 字节 | 0 ~ 65535 |
布尔型 | boolean | 没有明确规定 | true 和 false |
在这里我们可以通过代码来看每一种数据类型的范围:
public class Test1 {
public static void main(String[] args) {
byte a=12;
System.out.println(a);
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
}
}
public class Test1 {
public static void main(String[] args) {
short a = 15;
System.out.println(a);
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
}
}
public class Test1 {
public static void main(String[] args) {
int a = 12;
System.out.println(a);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
}
}
public class Test1 {
public static void main(String[] args) {
long a = 100;
System.out.println(a);
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
}
}
public class Test1 {
public static void main(String[] args) {
float a = 1.5f;
System.out.println(a);
System.out.println(Float.MIN_VALUE);
System.out.println(Float.MAX_VALUE);
}
}
public class Test1 {
public static void main(String[] args) {
double a = 1.5;
System.out.println(a);
System.out.println(Double.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
}
}
public class Test1 {
public static void main(String[] args) {
char a = 'A';
System.out.println(a);
System.out.println(Character.MIN_VALUE);
System.out.println(Character.MAX_VALUE);
}
}
public class Test1 {
public static void main(String[] args) {
boolean a = true;
System.out.println(a);
a = false;
System.out.println(a);
}
}
注:
- 无论是16位系统还是32位系统,int都占用4个字节,long都占用8个字节
- 整型和浮点型都是带有符号的(二进制表示时,第一位都是符号位,之后的才是数值位)
- 整型默认为int型,浮点型默认为double
- 字符串属于引用类型(后续再详细介绍)
三、变量
(一)概念
有一些量的值一直保持不变,而有一些量的值却是始终在变化的,例如我们的身高、年龄等,在Java程序中,我们将经常改变的量称为变量。
(二)语法格式
定义变量的语法格式:
数据类型 变量名 = 初始值;
int a = 10;
double b = 5.12;
char c = 'C';
boolean d = false;
int a1 = 10,a2 = 20,a3 = 30;//可以在一行定义多个相同类型的变量
(三)类型
数据类型就是用来定义不同变量的,所以不同变量的表示范围在数据类型处已经演示过,这里就不再赘述了,仅进行简单的补充
1. 整型变量
(1)整型变量
定义整型变量的两种方式:
//方式一
int a = 10;
System.out.println(a);
//方式二
int a;
a = 10; //在使用变量前为其赋值
System.out.println(a);
值得注意的是,使用方式二的时候容易造成错误
//在使用变量前未赋值,造成编译错误
int a;
System.out.println(a);
a = 10;
- 在定义整型变量时建议使用方式一定义,没有合适的初始值可设置为0
- 设置初始值时不能超出int的表示范围(在“数据类型”处有运行演示),否则会导致溢出
- int占4个字节
- int的表示范围是 -2^31 ~ 2^31-1
- 变量在使用前切记要赋值,否则会导致编译错误
- int的包装类是Integer
(2)长整型变量
long a = 10l; //l与1不好区分
long b = 12L;
- 长整型变量在数字后加l或L以示区分,建议加L
- long占8个字节
- long的表示范围是 -2^63 ~ 2^63-1
- long的包装类是Long
(3)短整型变量
short a = 10;
- short占2个字节
- short的表示范围是 -32768 ~ 32767(-2^15 ~ 2^15-1)
- short的包装类是Short
(4)字节型变量
byte a = 10;
- byte占1个字节
- byte的表示范围是-128 ~ 127(-2^7 ~ 2^7-1)
- byte的包装类是Byte
2.浮点型变量
浮点型变量与整型变量的存储方式不同,不能单纯的以2^n来计算,这里不做研究
(1)双精度浮点型
示例一:
public class Test {
public static void main(String[] args) {
int a = 1;
int b = 2;
System.out.println(a/b);
}
}
在Java中, int 除以 int 的值仍然是 int(会直接舍弃小数部分),想得到小数,需要使用double类型
上述例子中只需要将变量a、b中的一个类型变为double类型或变量a、b均变为double类型就可以得到0.5的运行结果
public class Test {
public static void main(String[] args) {
double a = 1;
double b = 2;
System.out.println(a/b);
}
public static void main2(String[] args) {
double a = 1;
int b = 2;
System.out.println(a/b);
}
public static void main1(String[] args) {
int a = 1;
double b = 2;
System.out.println(a/b);
}
}
示例二:
public class Test {
public static void main(String[] args) {
double num = 1.1;
System.out.println(num*num);
}
}
在我们的认知中1.1*1.1=1.21,但是在程序中运行时结果却不相同
这是因为小数没有精确的值,只能说精确到某一位上
- double占8个字节
- double的包装类是Double
(2)单精度浮点型
public class Test {
public static void main(String[] args) {
float f = 1.25f;
System.out.println(f);
}
}
- float在数字后需加f或F(否则默认是double)
- float占4个字节
- float的包装类是Float
3.字符型变量
public class Test {
public static void main(String[] args) {
char a = '1'; //数字字符
char b = 'A'; //大写字母
char c = '前'; //汉字
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
也可以不使用单引号,此时与ASCII码表相对应
public class Test {
public static void main(String[] args) {
char a = 99;
System.out.println(a);
}
}
- Java 中使用 单引号 + 单个字母 的形式表示字符字面值
- char不能赋值负数
- char占2个字节
- char的包装类是Character
4.布尔型变量
public class Test {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println(a);
System.out.println(b);
}
}
- boolean 类型的变量只有两种取值, true 表示真, false 表示假
- Java 的 boolean 类型和 int 不能相互转换, 不存在 1 表示 true, 0 表示 false 这样的用法
- 没有明确规定boolean占几个字节
- boolean的包装类是Boolean
(四)类型转换与提升
1.类型转换
在Java中,当参与运算的数据类型不一致时,就会进行类型转换。
Java中类型转换主要分为两类:自动类型转换(隐式) 和 强制类型转换(显式)。
(1)自动类型转换(隐式)
自动类型转换:代码不需要经过任何处理,在代码编译时,编译器会自动进行处理。
适用范围:数据范围小的转为数据范围大的时会自动进行
public class Test {
public static void main(String[] args) {
int a = 15;
long b = 20L;
b = a;
System.out.println(a);
System.out.println(b);
}
}
(2)强制类型转换(显式)
强制类型转换:当进行操作时,代码需要经过一定的格式处理,不能自动完成。
适用范围:数据范围大的转为数据范围小的
可能会造成精度丢失
public class Test {
public static void main(String[] args) {
int a = 15;
long b = 10L;
a = (int)b;
System.out.println(a);
}
}
public class Test {
public static void main(String[] args) {
int a = 10;
float f = 12.5f;
a = (int)f; //丢失小数部分
System.out.println(a);
}
}
强制类型转换不一定能成功,不相干的类型不能相互转换
2.类型提升
(1)int与long之间:int提升为long
public class Test {
public static void main(String[] args) {
int a = 10;
long b = 15L;
//int的4个字节提升为long的8个字节,然后与long的8个字节相加
int c = (int)(a+b);//强制类型转换,需要冒风险
long d = a + b; //两个8个字节相加后的类型为long
System.out.println(c);
System.out.println(d);
}
}
(2)byte与byte的运算
public class Test {
public static void main(String[] args) {
byte a = 4;
byte b =9;
//byte不足4个字节,运算时要先提升为4个字节
byte c = (byte)(a+b);
System.out.println(c);
}
}
小结:
- 不同类型的数据混合运算,范围小的会提升为范围大的
- 对于short、byte这类不足4个字节的类型,要先提升为4个字节的int,然后再进行计算
四、字符串类型
在Java中使用String类定义字符串类型
public class Test {
public static void main(String[] args) {
String a = "hello";
String b = "world";
System.out.println(a);
System.out.println(b);
System.out.println(a+" "+b);//+表示将左右两个字符串拼接起来
}
}
某些情况下需要在字符串与整型数字之间进行转换
1.int转为String
public class Test {
public static void main(String[] args) {
int num = 10;
String str1 = num + " ";
String str2 = String.valueOf(num);
System.out.println(str1);
System.out.println(str2);
}
}
2.String转为int
public class Test {
//String转为int
public static void main(String[] args) {
String str = "100";
int num = Integer.parseInt(str);
System.out.println(num);
}
}