-
强类型语言
-
要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用
-
安全性高
-
速度慢
-
-
弱类型语言
-
可以不符合规定VB,JS
-
-
Java的数据类型分为两大类
-
基本类型(primitive type)
-
数值类型
-
整数类型
-
byte占一个字节范围:-18-127
-
short占2个字节范围:-32768-32767
-
int占4个字节范围:-2147483648-2147483647
-
long占8个字节范围:-9223372036854775808-9223372036854775807
-
-
浮点类型
-
float占4个字节
-
double占8个字节
-
-
字符类型
-
char占2个字节
-
-
-
Boolean类型
-
-
引用类型(reference type)
-
类
-
接口
-
数组
-
-
public class Demo02 {
public static void main(String[] args) {
//八大基本数据类型
// 整数
int num1 = 10; // 最常用
byte num2 = 20;
short num3 = 30;
long num4 = 30L; // Long类型要在数字后面加个L
// 小数:浮点数
float num5 = 50.1F; // float类型要在数字后面加个F
double num6 = 3.14159265357964533;
// 字符
char name = '中';
// 字符串,String不是关键字,类
// String namea = "解迪";
//布尔值
boolean flag = true;
// boolean flag1 = false;
}
}
变量
-
变量是什么:可以变化的量
-
Java是一种强类型语言,每个变量都必须声明其类型
-
Java变量事程序中最基本的储存单元,其要素包含变量名,变量类型和作用域
-
数据类型 变量名 = 值;
-
注意事项
-
每个变量都有类型,类型可以是基本类型,也可以是引用类型
-
变量名必须是合法的标识符
-
变量声明是一条完整的语句,因此每一个声明都必须以分号结束
-
public class Demo02 {
public static void main(String[] args) {
// int a=1, b=2, c=3; 不提倡多行,程序可读性
String name = "xd";
char x = 'x';
double pi = 3.14;
}
}
-
变量作用域
-
类变量:写在类里面
-
加static
static int a = 0;
-
-
实例变量:写在类中间
-
局部变量:写在方法中
-
public class Demo02 {
//属性:变量
//实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0
//布尔值:默认是false
//除了基本类型,其余默认值都是null
String name;
int age;
//类变量 static
static double salary = 2500;
//main方法
public static void main(String[] args) {
//局部变量;必须声明和初始化值,只在该方法有效
int i = 10;
System.out.println(i);
//变量类型 变量名字 = new Demo02()
Demo02 demo02 = new Demo02();
System.out.println(demo02.age);
System.out.println(demo02.name);
System.out.println(salary);
}
//其它方法
public void add(){
}
}
-
变量的命名规范
-
所有变量,方法,类名:见名知意
-
类成员变量:首字母小写和驼峰原则:monthSalary
-
局部变量:首字母小写和驼峰原则
-
常量:大写字母和下划线:MAX_VALUE
-
类名:首字母大写和驼峰原则:Man,GoodMan
-
方法名:首字母小写和驼峰原则:run(),runRun()
-
常量
-
常量(constant):初始化后不能再改变值,不会动的值
-
所谓常量可以理解为一种特殊变量,它的值被设定后,再程序运行过程中不允许被改变
-
final 常量名=常量值;
-
public class Demo02 {
//修饰符,不存在先后顺序
static final double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
字节
-
位(bit):是计算机 内部数据 存储的最小单位,11001100是一个八位二进制数
-
字节(byte):是计算机中 数据处理 的基本单位,习惯上用大写 B 来表示
-
1B = 8bit
-
字符:是指计算机中使用的字母,数字,字和符号
-
1bit = 1位
-
1byte = 1B = 8b
-
1024B = 1KB
-
1024KB = 1M
-
1024M = 1G
-
1024G = 1TB
数据类型扩展
-
整数拓展
public class Demo02 {
public static void main(String[] args) {
//整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010; //八进制
int i3 = 0x10; //十六进制0x 0-9 A-F
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("====================================");
//=======================================================
//浮点数拓展 银行业务表示
//=======================================================
//float 有限 离散 舍入误差 大约 接近但不等于
//double
//最好完全不用浮点数进行比较
//BigDecimal 数学工具类
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);
System.out.println(f);
System.out.println(d);
System.out.println("====================================");
float d1 = 32323232f;
float d2 = d1 + 1;
System.out.println(d1 == d2);
System.out.println(d1);
System.out.println(d2);
//=======================================================
//字符拓展
//=======================================================
System.out.println("====================================");
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1); //强制转换
System.out.println(c2);
System.out.println((int)c2); //强制转换
//所有的字符本质还是数字
//编码 Unicode 2字节 65536 excel 2 16 65536
char c3 = '\u0061';
System.out.println(c3); //a
System.out.println("====================================");
//转义字符
// \t 制表符
// \n 换行
System.out.println("hello\tworld");
System.out.println("hello\nworld");
//=======================================================
//布尔值拓展
//=======================================================
boolean flag = true;
if (flag==true){}
if (flag){}