-
Java是一种强类型语言要求变量的使用要严格符合规定,所有变量都必须先定义在使用
-
弱类型语言:有自己的编辑器,也有自己的一定的规定
基本类型8个
-
byte:占1个字节
-
short:占2个字节
-
int:占4个字节
-
long:占8个字节后面加L
-
float:占4个字节
-
double:占8个字节
-
char:占2个字节
-
boolean:占1为只有true和false两个
public class Test {
public static void main(String[] args) {
int a=10; //最常用
byte b=20;
short c=30;
long e=40l; //要在后面加l
float f=1.1f;//要在后面加f
double g=3.13243;
char h='国';
boolean i=true;
}
}
注:String不是关键字他是一种引用类型定义字符串不能用整型
如果超出范围则会内存溢出,字节等于4位
引用类型
-
类
-
接口
-
数组
数据类型转换
-
由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换,小数的优先级大于整数
public class Test {
public static void main(String[] args) {
int i = 128;
byte b = (byte) i;//强制转换将int转换为byte
System.out.println(i);
System.out.println(b);//128大于byte的范围会发生内存溢出
}
}
128
-128
Process finished with exit code 0
强制转换格式: (类型)变量名
-
运算中,不同类型的数据先转化为同一类型,然后进行计算
-
强制类型转换
由高到低要进行强制类型转换
-
自动类型转换
由低到高可进行自动类型转换
public class Test {
public static void main(String[] args) {
int i = 128;
double b = i;//强制转换将int转换为byte
System.out.println(i);
System.out.println(b);//128大于byte的范围会发生内存溢出
}
}
128
128.0
Process finished with exit code 0
-
注意点:
boolean不能进行转换,不能把对象类型转换为不相干的类型,再把高容量转换为低容量时强制转换,转换的时候可能存在内存溢出,或者精度问题,操作比较大的数的时候,注意溢出问题,JDK7新特性,数字之间可以用下划线分割(int monet = 10_0000_0000;输出money)时下划线并不会输出
public class Test {
public static void main(String[] args) {
int money = 10_0000_0000;
int year = 20;
int total1 = money*year;
long total2 = money*year;
//计算是溢出
System.out.println(total1);
//在转换前就出现问题
System.out.println(total2);
long total12 = money*((long)year);
//在转换时将其中的一个数转换为long类型得到的结果就是long类型
System.out.println(total12);
}
}
-1474836480
-1474836480
20000000000
Process finished with exit code 0
-
拓展:什么是字节
位(bit):是计算机内部数据存储的最小单位,字节(byte)是计算机中数据处理的基本单位,习惯上用B表示。1B=8bit。字符是指计算机中使用的字母,数字,字和字符。1B=8b,1024B=1KB,1024KB=1M,1024M=1G。
面试题拓展
整数:进制
public class Test {
public static void main(String[] args) {
//整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i=10;
int i1=010; //八进制
int i2=0x10; //十六进制0x 0~9与A~F
System.out.println(i);
System.out.println(i1);
System.out.println(i2);
}
}
10
8
16
Process finished with exit code 0
浮点数:银行钱数目怎么展示
public class Test {
public static void main(String[] args) {
//浮点数拓展 银行钱数目怎么展示
float f=0.1f;
double d=1.0/10; //1.0除以10
System.out.println(f==d);
float f1=12324141414144f;
float f2=f1+1; //将f1的结果加1
System.out.println(f1==f2);
}
}
false
true
Process finished with exit code 0
第一个计算结果相同但计算机打印的是false第二个结果不同但计算机打印的是true。浮点数表示的字长是有限的但有些东西除出来是无限的。浮点数是有限且离散的有舍入误差表示的是一个大约数,接近但不等于。所以最好完全避免使用浮点数进行比较。可以用类BigDecimel一个数学工具类
字符类:
public class Test {
public static void main(String[] args) {
//字符类拓展
char c1='a';
char c2='中';
char c3='u0061'; //转义
System.out.println(c1);
System.out.println((int)c1); //将字符类强制转换为数字
System.out.println(c2);
System.out.println((int)c2); //将字符类强制转换为数字
System.out.println(c3);
}
}
a
97
中
20013
a
Process finished with exit code 0
所有的字符本质还是数字所以可以将它们转换为数字。Unicode编码可以处理多个语言的编码一个占2个字节。(97=a;65=A)U0000~UFFFF
//转义字符 t空格 n换行
String拓展:
public class Test {
public static void main(String[] args) {
String sa=new String("DPC");
String sb=new String("DPC");
System.out.println(sa==sb);
String sc="DPC";
String sd="DPC";
System.out.println(sc==sd); //将字符类强制转换为数字
}
}
false
true
Process finished with exit code 0
对象,从内存分析。表面看上去一样但在内存里是不一样的。
booleatn拓展:
public class Test {
public static void main(String[] args) {
//boolean拓展
boolean flag=true;
if (flag =true){
}
}
}


