基本数据类型

整型【byte,short,int,long】

byte 1 字节

short 2 字节

int 4 字节 【默认】

long 8 字节 (声明long型常量时,需在常量后加l或L)

浮点型【float,double】

float 4 字节 (保留8位有效数字)

(声明float型常量时,需在常量后加f或F)

double 8 字节 (保留17位有效数字)【默认】

说明:

1)浮点数在机器中的存放形式:浮点数=符号位+指数位+尾数位

2)尾数部分可能丢失,造成精度损失(小数都是近似值)

例如: 2.7 并不等同于 8.1/3

(因为在浮点数运算中8.1/3的结果无限趋近于2.7但不等于2.7)

字符型【char】

char 2 字节 (存放单个字符,用‘’)

说明:

1)在java中,char本质是一个整数,在输出时,是unicode码中对应的字符

2)char类型是可以进行运算的,相当与一个整数,因为他有对应的Unicode码

public class Hello{
	public static void main(String[] args){
		char ch_1 = 65;
		System.out.println(ch_1 + 10);//75
		char ch_2 = 'A';
		System.out.println(ch_2 + 10);//75
	}
}

布尔型【boolean】

boolean 1 字节 (存放true、false)

说明:

1)boolean类型数据只允许取值true和false,无null

2)boolean一般用于逻辑运算,程序流程控制语句

3)不可以用0或非0整数替代false和true,这点与C语言不同

基本数据类型转换

1)自动类型转换:当java程序在进行赋值或运算时,精度小的类型自动转换为精度大的数据类型,其过程为自动类型转换

数据类型、基本数据类型转换_类型转换

自动类型转换注意和细节:

1)有多种类型的数据混合运算时,系统先自动将所有数据转换成容量最大的那种数据类型,然后再进行计算。

public class Test {
	public static void main(String[] args) {
		int a = 1;
        char b = 'A';
		double c = 10.0;
    	// double > int   
        //1.将a变为1.0
        //2.将b变为'A' -- 65 -- 65.0
        //3.将a、b、c相加,得到结果
		System.out.println(a + b + c);
        // 76.0  --- 结果为 11.0 double类型
	}
}

2)当我们把精度(容量)大的数据类型赋值给精度(容量)小的数据类型时,就会报错,反之就会进行自动类型转换。

public class Test {
	public static void main(String[] args) {
		//  情况一:
    	int a = 1;
		byte b = a;//报错    
    	//  情况二:
        byte c = 100;  
        //系统一般默认的整数常量的类型为int型 
		//但是这里却能将100赋值给c
        //因为100没有超出byte的范围-128 - 127
        //所以可以对其进行赋值,不会报错
	}
}

3)byte、short、char之间不会相互自动转换。

package Test;

public class Test {
	public static void main(String[] args) {
		char a = 'A';
		byte b = a; // 报错    
    //char byte short之间进行赋值 不会进行自动 转换
	}
}

4) byte、short、char三者可以计算,在计算时先转换为int类型。

public class Test {
	public static void main(String[] args) {
		char a = 'A';
		byte b = 10;
		byte c = a + b;// 报错 因为char byte short在计算时,会转换为int型
		int c = a + b; // √ 
	}
}

5)自动提升原则:表达式结果的类型提升为操作数中最大的类型

2)强制类型转换:自动类型转换的逆过程,将容量大的数据类型转换为客量小的数据类型。使用时要加上强制转换符(),但可能造成精度降低或溢出,格外要注意。

public class Test {
	public static void main(String[] args) {
		int i = (int)1.9;
		System.out.println(i);
		
		int j = 100;
		byte b = (byte)j;
		System.out.println(b1);
    
    	int y = (int)(3 * 0.5 + 6 * 0.5);
        System.out.println(y);
        
        char c1 = 100;
        int m = 100;
        char c2 = m;//报错
        char c3 =(char)m;
		System.out.println(c3);
    }
}

强制类型转换细节说明:

1)当进行数据的大小从大——>小,就需要使用到强制转换

2)强转符号只针对于最近的操作数有效,往往用小括号提升优先级

3) char类型可以保存int的常量值,不能保存int的变量值,需要强转

4.)byte和short类型在进行运算时,当做int类型处理。

基本数据类型和String类型的转换

在程序开发中,我们经常需要将基本数据类型转成String类型或者将String类型转成基本数据类型。

基本类型转String类型 ---- 将基本类型的值+""即可

public class Test {
	public static void main(String[] args) {
		int n1 = 1;
    	float n2 = 10.0f;
        double n3 = 3.14;
		boolean n4 = true;
        
        String str1 = n1 + "";
        String str2 = n2 + "";
        String str3 = n3 + "";
        String str4 = n4 + "";
        System.out. println(str1 + " " + str2 +" "+ str3 + " " +str4);
	}
}

String类型转基本数据类型 ---- 通过基本类型的包装类调用parseXX方法

public class Test {
	public static void main(String[] args) {
				int a1 = Integer. parseInt( "123");
        double a2 = Double.parseDouble("123.1");
        float a3 = Float.parseFloat( "123.23");
        short a4 = Short.parseShort("12");
        long a5 = Long.parseLong( "12345");
        boolean a6 = Boolean.parseBoolean( "true");
        byte a7 = Byte.parseByte("12");
        System.out. println(a1+"\n"+a2+"\n"+a3+"\n"+a4+"\n"+a5+"\n"+a6+"\n"+a7);
  }
}