- 文件名需要和类名保持一致
- public : 访问修饰限定符 private protected
- class : 定义类的关键字
- 一个Java文件当中只能有一个public类
- 方法、函数。 main方法 : 是程序的入口 , 入口只有一个
方法 : [public static] 返回值 方法名 (形式参数)
编译 javac 文件名.java
一个类 会生成 一个 字节码文件
好处 : 随用随取
JVM : java虚拟机 (c/c++实现的一个软件)
Java命令 运行程序
类名要满足大驼峰 不能用拼音 数字
System.out.print("hello"); //打印不换行
System.out.println("hello"); //打印且换行
System.out.printf("%dn",10); //格式化输出 (不常用)
因为加了中文
说明自己编辑器默认是utf-8编码
javac 默认GBK编码
方法: 加参数 指定编码格式
javac -encoding utf-8 HelloWorld.java
java 运行时的命令行参数
数组 : 代表一组数据
public class HelloWorld{
public static void main(String[] args) {
for(int i = 0; i < args.length;i++){
System.out.println(args[i]);
}
数据类型
箱子 : 变量
不同类型的箱子 : 数据类型
不同的数据类型,定义的变量 一定是大小不一的
==冯诺伊曼体系 ==
整形变量int 4字节
main 回车
pl 回车
整形 int
a —变量名 : 标识符
变量命名 : 小驼峰 : maxNum max 由数字 字母 下划线 美元符号 组成 (数字不能在开头)
- 不能以数字开头
- 建议不要以下划线和美元符号开头 (合法但不建议)
- 小驼峰
计算机当中衡量大小的单位
bit byte kb mb gb tb pb
位 字节
1字节 = 8 位
1kb = 1024 byte
…
快捷键注释 : 选中 CTRL+shift +/
System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE);
Integer : 包装类
Integer是一种类型
Integer和int的关系 : 把Integer当作int的plus版本
得到结果 正数最大值 2147483647
负数最小值 -2147483648
double 8字节
public class HelloWorld{
public static void main(String[] args) {
double d = 12.5;
System.out.println(d);
System.out.println(Double.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
}
注 : java看不了各种类型所占多少字节
public class HelloWorld{
public static void main(String[] args) {
double num = 1.1;
System.out.println(num*num);
}
这说明 在电脑中 小数是没有一个最精确的
他只能最精确到小数点后几位
float 6位
public class HelloWorld{
public static void main(String[] args) {
double num = 1.1;
System.out.println(num*num); //在电脑中 小数是没有一个最精确的
int a = 1;
int b = 2;
System.out.println(a/b);
}
只能保存整数位 不能保存小数位
改为double
public class HelloWorld{
public static void main(String[] args) {
double num = 1.1;
System.out.println(num*num); //在电脑中 小数是没有一个最精确的
double a = 1;
double b = 2;
System.out.println(a/b);
}
单精度浮点类型
float 4字节
对应的包装类Float
public class HelloWorld{
public static void main(String[] args) {
float f = 12.5;
System.out.println(f);
}
报错
改为
12.5f 指明现在的12.5是一个单精度的数
public class HelloWorld{
public static void main(String[] args) {
float f = 12.5f;
System.out.println(f);
}
字符类型
** char 类型 2个字节**
public class HelloWorld{
public static void main(String[] args) {
char ch = 'd'; //单引号引起的叫做字符 双引号引起的叫做字符串 char 类型占2个字节
char ch2 = '高'; //一个汉字是两个字节
char ch3 = 97; //只知道char类型是字符类型,虽然你给了我一个正数,但我会去找这个数对应的ASCII码
System.out.println(ch);
System.out.println(ch2);
System.out.println(ch3);
}
Java 采用的表叫Unicode
字节类型(byte)byte 1字节
取值范围 : -128~127
public static void main(String[] args) {
byte b = 10; //byte 1字节
System.out.println(b);
}
public static void main(String[] args) {
byte b = 128; //byte 1字节
System.out.println(b);
}
取值范围 : -128~127
如果是直接赋值的时候,要注意: 直接赋值的不能超过表示范围
public static void main(String[] args) {
byte b = 127; //byte 1字节
System.out.println(b+1);
}
System.out.println打印的时候 默认是以整形打印的 4个字节
+1后没有超过4个字节 所以没有报错
所以 不要直接赋值一个超过范围的数据
两个字节 16位 -2^16 ~ 2^16 - 1
包装类Short
只有两个值 true false
JVM 没有明确布尔类型的大小
有的书写的1bit 有的写的1字节



