Java基础语法目录
Java自学笔记——基础语法(一)注释、标识符、关键字
Java自学笔记——基础语法(二)数据类型、类型转换
Java自学笔记——基础语法(三)变量、常量
Java自学笔记——基础语法(四)运算符
Java自学笔记——基础语法(五)包机制、JavaDoc
目录
数据类型
强类型语言
——要求变量的使用严格符合规定,所有变量都必须先定义后才能使用
优点:安全性高,速度慢
弱类型语言
VB 、 Javascripe
例子
一、Java的数据类型分为两大类
◆基本类型( primitive type)
◆引用类型( reference type)
1. 八大数据类型
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;
double num6 = 3.141592264792307935704092;
//字符类型
char name = '秦';
//字符串 , String不是关键字,类
//String name = "秦疆";
//布尔值
boolean flag = true;
//boolean flag = false;
}
}
2. 什么是字节
**位(bit):**是计算机内部数据储存的最小单位,1100110是—个八位二进制数
**字节(byte):**是计算机中数据处理的基本单位,习惯上用大写B来表示,
1B(byte,字节) = 8bit(位)
字符:是指计算机中使用的字母、数字、字和符号
- 1bit 表示1位,
- 1Byte 表示—个字节1B=8b
- 1024B = 1KB
- 1024KB =1M
- 1024M = 1G
思考:电脑的32位和64位的区别是什么呢?
二. 面试题
1. 整数拓展:
二进制 0b 八进制 0 十六进制 0x
public class demo03 {
public static void main(String[] args) {
// 整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10; //4
int i2 = 010; //8
int i3 = 0x10; //16
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
}
}
2. 浮点数拓展:
最好完全使用浮点数进行比较
//浮点数扩展? 银行业务怎么表示?钱
// BigDecimal 数学工具类
//===================================================
// float 有限 离散 舍入误差 大约接近 但是不等于
// double
// 最好完全使用浮点数进行比较
// 最好完全使用浮点数进行比较
// 最好完全使用浮点数进行比较
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d); //false
float d1 = 23233232323323f;
float d2 = d1 + 1;
System.out.println(d1==d2); //true
3. 字符拓展
//===================================================
// 字符拓展?
//===================================================
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字节 0-65536 2^16 ascll编码表
//u0000 UFFFF
char c3 = '\u0061';
System.out.println(c3);
4. 转义字符:
这个要从内存中去理解,以后讲
//转义字符
// \t
System.out.println("Hello\tWord"); // \t 空格 \n 换行
//判断他们俩相等吗? 思考 从内存分析
System.out.println("========================");
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa==sb); //false
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd); //true
5. 布尔值拓展:
//布尔值拓展
boolean flag = true;
// 两行代码表示的一样
if (flag==true){} // 新手
if (flag){}
// Less is more! 代码要精简易读
类型转换
-
由于Java是强类型语言所以要进行有些运算的时候的,需要用到类型转换。
低---------------------------------------------------------高
byte, short, char -> int - long-> float ->double 小数优先级大于整数字节 字符串 整数 浮点 小数
-
运算中,不同类型的数据先转化为同一类型,然后进行运算
-
强制类型转换
-
自动类型转换
public class demo05 {
public static void main(String[] args) {
//强制转换 (类型)变量名 高——低
//自动转换 低——高
int i = 128;
byte b = (byte)i; // -128 Byte最多127 溢出
System.out.println(i);
System.out.println(b);
//自动转换 低——高
int q = 128;
double s = q;
System.out.println(q);
System.out.println(s);
/*
注意点:
1. 不能对布尔值强制转换
2. 不能把对象类型转换为不相干的类型
3. 在把高容量转换到低容量的时候,强制转换
4. 转换的时候可能存在内存溢出,或者精度问题!
*/
System.out.println("======================");
System.out.println((int)23.7); //23
System.out.println((int)-45.89f); //-45
System.out.println("======================");
char c = 'a';
int d = c+1;
System.out.println(d); // 98
System.out.println((char) d); //b
}
}
public class demo06 {
public static void main(String[] args) {
//操作比较大的数的时,注意溢出问题
//JDK7新特性 ,数字之间可以用下划线分割
int money =10_0000_0000;
int years =20;
int total = money*years; //-1474836488,计算的时候溢出了
long total2 = money*years; //-1474836488 默认是int,转换之前已经存在问题了
long total3 = money*((long)years); //20000000000 先把一个数转换为Long
System.out.println(total3);
}
}