.基本类型
Java支持数据类型分为两类: 基本数据类型和引用数据类型。
数据类型转换有两种:自动隐式和强制转换
基本数据类型共有8种,可以分为三类:
- 数值型:整数类型(byte、short、int、long)和浮点类型(float、double)
- 字符型:char
- 尔型:boolean
8种基本数据类型的默认值、位数、取值范围,如下表所示:
float和double的、最小值和最大值都是以科学记数法的形式输出的,结尾的“E+数字”表示E之前的数字要 乘以10的多少倍。比如3.14E3就是3.14×1000=3140,3.14E-3就是3.14/1000=0.00314。
/*
* Copyright (c) 2020, 2023, MingZi.cn All rights reserved.
*
*/
package org.abc;
/**
* <p>Project: jse2303 - E1</p>
* <p>Powered by MingZi On 2023-07-06 11:16:18</p>
* <p>描述:<p>
*
* @author MingZi [MingZi@182.com]
* @version 1.0
* @since 17
*/
public class E1 {
public static void main(String[] args) {
byte b1 = 5;
System.out.println(b1);
short s1 = 5;
System.out.println(s1);
int i1 = 5;
System.out.println(i1);
long n1 =5;
System.out.println(n1);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
float f1 = .5f;
double d2 = .5;
System.out.println(f1);
System.out.println(d2);
char c1 ='a';
int num ='a';
char c2 ='中';
System.out.println(c1);
System.out.println(num);
System.out.println(c2);
System.out.println((int) c2);
boolean f = true;
System.out.println(f);
System.out.println(!!!f);
int aaa2 = (int)789456123066l;
System.out.println(aaa2);
double n =10;
}
}
注意一下几点:
- java八种基本数据类型的字节数:1字节(byte、boolean)、 2字节(short、char)、4字节(int、 float)、8字节(long、double)
- 浮点数的默认类型为double(如果需要声明一个常量为float型,则必须要在末尾加上f或F)
- 整数的默认类型为int(声明Long型在末尾加上l或者L)
- 八种基本数据类型的包装类:除了char的是Character、int类型的是Integer,其他都是首字母大写 char类型是无符号的,不能为负,所以是0开始的
.类型转换
自动类型转换隐式转换
- 转换规则:从存储范围小的类型到存储范围大的类型。简言之,由低到高。
- 具体规则为:byte→short(char)→int→long→float→double
- 强制类型转换(float)3.5;
/*
* Copyright (c) 2020, 2023, MingZi.cn All rights reserved.
*
*/
package org.abc;
/**
* <p>Project: jse2303 - E3</p>
* <p>Powered by MingZi On 2023-07-06 21:01:32</p>
* <p>描述:<p>
*
* @author MingZi [MingZi@182.com]
* @version 1.0
* @since 17
*/
public class E3 {
public static void main(String[] args) {
long i = 2; //自动隐式类型转换
int y =(int)i;//强制类型转换
//类型数据 f F float d D double l L long
float f1 = .5f;
double d1 = .5D;
long g1 = 1;
short s = 6;
/* s = (short)(s + 1);
System.out.println(s);*/
s+=1;
System.out.println(s);
}
}
快捷键:
- 复制行 Ctrl + D
- 移动行 Alt + Shift + up (down)
- 单行注释 Ctrl + /
- 多行注释 Ctrl + Shift + /
- 重构 Shift + F6