这个系列正在更新中,主要记录我在学习Java语言中的一些小笔记,希望对你有所帮助。
一、注释
注释就是在程序制定位置添加的说明性信息
分类:
- 单行:
//注释信息
- 多行:
/*注释信息*/
- 文档:
/**注释信息*/
二、关键字
被Java语言赋予了特殊含义的单词
特点:
- 关键字的字母小写
- 常用的代码编辑器对关键字有高亮显示
例如:public、class、static、void等
public class day_001 {
public static void main(String[] args){
int a = 10;
int b = 20;
System.out.println( ( a++ > 10 )||( ++b > 20 ) );
}
}
三、常量
在程序运行的过程中,值不变的量。
分类
常量类型 | 说明 | 举例 |
---|---|---|
整数常量 | 不带小数的数字 | 12,34 |
小数常量 | 带小数的数字 | 12.23 |
字符常量 | 用单引号括起来的内容 | ‘A’,‘国’ |
字符串常量 | 用双引号括起来的内容 | “women”,“中国” |
布尔常量 | 布尔值:用来表示真假 | 只有两个值:true/false |
空常量 | 特殊值:空值 | null |
public class day_001 {
public static void main(String[] args){
//整数常量
System.out.println(12);
System.out.println(-34);
System.out.println("------");
//小数常量
System.out.println(12.2);
System.out.println(-34.56);
System.out.println("------");
//字符常量
System.out.println('w');
System.out.println('我');
System.out.println("------");
//字符串常量
System.out.println("right");
System.out.println("我们");
System.out.println("------");
//布尔常量
System.out.println(true);
System.out.println(false);
}
}
注意:空常量不能直接输出
四、数据类型
- 计算机存储单元
计算机存储设备的最小信息单元是“位(bit)”,用小写的b表示;最小的存储单元是“字节(byte)”,用大写的B表示。
1B = 8bit
1KB = 1024B
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB - 数据类型
分类:Java中的数据类型分为基本数据类型和引用数据类型。其中,基本数据类型又分为数值型和非数值型。
数值型有整数、浮点数和字符;非数值型有布尔。 - 数据类型内存占用和取值范围
数据类型 | 关键字 | 内存占用 | 取值范围 |
---|---|---|---|
整数 | byte | 1 | -128~127 |
short | 2 | -32768~32767 | |
int(默认) | 4 | -2³¹~2³¹-1 | |
long | 8 | -2⁶³~2⁶³-1 | |
浮点数 | float | 4 | 负数:-3.402823×10³⁸到-1.401298×10﹣⁴⁵;正数:1.401298×10﹣⁴⁵到3.402823×10³⁸ |
double(默认) | 8 | 负数:-1.797693×10³⁰⁸到-4.9000000×10﹣³²⁴;正数:4.9000000×10﹣³²⁴到1.797693×10³⁰⁸ | |
字符 | char | 2 | 0~65535 |
布尔 | boolean | 1 | true、false |
五、变量
在程序运行过程中,其值可以发生变化的量
变量暂时保存在内存中。
- 格式:数据类型 变量名 = 变量值
int a = 2;
- 变量的使用:变量名
a
- 修改变量值:变量名 = 变量值
a = 3;
public class day_001 {
public static void main(String[] args) {
//定义变量
int a = 12;
//输出变量
System.out.println(a);
System.out.println("------");
//修改变量
a = 23;
//输出
System.out.println("修改后a的值为:" + a);
}
}
- 变量使用注意事项
变量名不能重复
变量未赋值,不能使用
long类型变量定义时后加L,方式整数过大
float类型变量定义时后加F,防止类型不兼容
//这是错误的示范
public class day_001 {
public static void main(String[] args) {
//int类型
int a = 12;
//输出变量
System.out.println(a);
System.out.println("------");
//Boolean类型
//相同变量名错误
Boolean a = true;
//输出
System.out.println(a);
System.out.println("------");
//char类型
//未赋值错误
char b;
//输出
System.out.println(b);
System.out.println("------");
//long类型
//数值过大错误,需要在后面加上L
long c = 10000000000;
System.out.println(c);
System.out.println("------");
//float类型
//类型不兼容错误,需要在其后方加上F
float d = 12.23;
System.out.println(d);
}
}
六、标识符
给类、方法、变量等起名字的符号
-
定义规则
由数字、字母、下划线、美元符组成;不能以数字开头;不能时关键字;区分大小写。 -
常见命名约定
(1)小驼峰命名法(方法、变量):标识符是一个单词时,首字母小写;例如:student,school
标识符是多个单词时,除首单词之外的每一个单词首字母大写。例如:oneSchool,firstStudent(2)大驼峰命名法(类):标识符是一个单词时,首字母大写;例如:Teacher,Book
标识符是多个单词时,每个单词首字母都要大写。例如:FirstBook,GoodTeacher
未完待续~