Java中的8种基本数据类型
JAVA中的8种基本数据类型:byte short int long float double char boolean
特别说明:
1)char类型占2个字节,可以表示汉字。汉字和英文字符都占2个字节。
2)boolean类型理论上占1个bit,但实际中按1个byte(字节)处理。
3)基本数据类型之间的转换:低级向高级自动转换,高级向低级需要显示(强制)转换。
public class ByteAndBit { public static void main(String[] args) { System.out.println("type\t"+"bit\t"+"byte");
System.out.println("Byte\t"+Byte.SIZE+"\t"+Byte.BYTES);
System.out.println("Short\t"+Short.SIZE+"\t"+Short.BYTES);
System.out.println("Integer\t"+Integer.SIZE+"\t"+Integer.BYTES);
System.out.println("Long\t"+Long.SIZE+"\t"+Long.BYTES);
System.out.println("Float\t"+Float.SIZE+"\t"+Float.BYTES);
System.out.println("Double\t"+Double.SIZE+"\t"+Double.BYTES);
System.out.println("Character"+Character.SIZE+"\t"+Character.BYTES); }
}
输出:
包装类:
为满足Java语言面相对对象的特性,上述8种基本数据类型在java.lang包中都有对应的包装类。
包装类可以分为3大类,Character,Boolean和Number(Short,Integer,Long,Float,Double)
包装类的一些特性:
1)所有包装类都可以将与之对应的基本数据类型作为参数来创建它们的实例对象
2)除了Character类之外,其他包装类都可以将一个字符串作为参数来构造它们的实例
3)Boolean类的构造方法参数为String类型时,若该字符串为true(不论大小写),则该对象表示true,否则表示false
4)当包装类Number构造方法的参数为String类型时,字符串不能为null,并且该字符串必须能够解析为基本类型的数据
public static void main(String[] args) {
// 1)所有包装类都可以将与之对应的基本数据类型作为参数来创建它们的实例对象
Byte b=new Byte((byte) 2);
Short s=new Short((short) 3);
Integer a=new Integer(4);
Double d = new Double(5.0d);
Character c=new Character('A');
Boolean bb=new Boolean(false);
Long l=new Long(4L);
Float f=new Float(4.7f);
System.out.println(b+" "+s+" "+a+" "+d+" "+c+" " +bb);
// output: 2 3 4 5.0 A false // 2)除了Character类之外,其他包装类都可以将一个字符串作为参数来构造它们的实例
Byte b1=new Byte("2");
Short s1=new Short("3");
Integer a1=new Integer("4");
Double d1 = new Double("5.0d");
Character c1=new Character('A');
Boolean bb1=new Boolean("false");
System.out.println(b1+" "+s1+" "+a1+" "+d1+" "+c1+" " +bb1);
// output: 2 3 4 5.0 A false // 3)Boolean类的构造方法参数为String类型时,若该字符串为true(不论大小写),则该对象表示true,否则表示false
Boolean n1 = new Boolean("True");
Boolean n2 = new Boolean("TrUE");
Boolean n3 = new Boolean("hello");
System.out.println(n1+" "+n2+" "+n3);
// output: true true false // 4)当包装类Number构造方法的参数为String类型时,字符串不能为null,并且该字符串必须能够解析为基本类型的数据
Integer a2 = new Integer(" "); //Exception in thread "main" java.lang.NumberFormatException: For input string: " "
Integer a3 = new Integer(""); // Exception in thread "main" java.lang.NumberFormatException: For input string: ""
Integer a4 = new Integer(null); //Exception in thread "main" java.lang.NumberFormatException: null
Integer a5 = new Integer("abs"); //Exception in thread "main" java.lang.NumberFormatException: For input string: "abs"
System.out.println(a2+" "+a3+" "+a4+" "+a5); //
byte bbb=2;
short ss=3;
int ii=4;
long ll=5;
float ff=6.67f; // 必须加f
double dd=7.7d; //可以不加d
dd=bbb;
dd=ss;
dd=ii;
dd=ll; ff=bbb;
ff=ss;
ff=ii;
ff=ll;
ff=(float)dd; ll=bbb;
ll=ss;
ll=ii;
ll=(long)ff;
ll=(long)dd; ii=bbb;
ii=ss;
ii=(int)ll;
ii=(int)ff;
ii=(int)dd; ss=bbb;
ss=(short)ii;
ss=(short)ll;
ss=(short)ff;
ss=(short)dd; bbb=(byte)ss;
bbb=(byte)ii;
bbb=(byte)ll;
bbb=(byte)ff;
bbb=(byte)dd; }
包装类的作用:
1)集合中不允许存放基本数据类型,故常用包装类
2)包含了每种基本数据类型的相关属性,如最大值、最小值、所占位数等
3)作为基本数据类型对应的类类型,提供了一系列实用的对象操作,如类型转换、进制转换等等
public static void main(String[] args) {
//Byte
System.out.println(Byte.MAX_VALUE); //
System.out.println(Byte.MIN_VALUE); //-128
System.out.println(Byte.hashCode((byte)2)); System.out.println(Byte.valueOf((byte)2));//返回Byte类型
System.out.println(Byte.valueOf("2")); //返回Byte类型 System.out.println(Byte.toString((byte) 4)); System.out.println(Byte.parseByte("2")); //返回byte类型
System.out.println(); //Integer
System.out.println(Integer.MAX_VALUE); //(2^31)-1 2147483647
System.out.println(Integer.MIN_VALUE); //-(2^31) -2147483648
System.out.println(Integer.hashCode(2)); System.out.println(Integer.valueOf(2)); //返回Integer类型
System.out.println(Integer.valueOf("2")); //返回Integer类型 System.out.println(Integer.max(1, 2));
System.out.println(Integer.min(1, 2)); System.out.println(Integer.parseInt("2")); //
System.out.println(Integer.parseInt("3",8)); // System.out.println(Integer.toBinaryString(2));//
System.out.println(Integer.toHexString(2));//
System.out.println(Integer.toString(3)); // Integer i=new Integer(10);
int ii=i.intValue(); //Character
System.out.println(Character.isDigit('s'));
System.out.println(Character.isLetter('e'));
System.out.println(Character.isLetterOrDigit('E')); }
从JDK1.5就开始引入了自动拆装箱的语法功能,也就是系统将自动进行基本数据类型和与之相对应的包装类型之间的转换,这使得程序员书写代码更加方便。
public static void main(String[] args) {
//自动装箱
int m = 10;
Integer in = m;
System.out.println(in);//10 //自动拆箱
Integer out = new Integer(10);
int n = out;
System.out.println(n);//
}
包装类中==和equals的用法比较:
值得注意的是,包装类中的equals方法和String类一样,都是重写了Object类中的equals方法,因此比较的是内容而不是地址,
而“==”比较的依然是引用变量的地址,只是当包装类型和与之相对应的基本类型进行“==”比较时会先做自动拆箱处理。
public static void main(String[] args) {
Integer a=new Integer(-100);
Integer b=new Integer(-100);
int c=-100;
System.out.println(a==b); //false;
System.out.println(a==c); //true; System.out.println(a.equals(b));//true;
System.out.println(a.equals(c));//true; }
- 基本类型,在Java中所有数字都是带符号的。
类型 长度 范围
一、基本数据类型的特点,位数,最大值和最小值。
1、
基本类型:short 二进制位数:16
包装类:java.lang.Short
最小值:Short.MIN_VALUE=-32768 (-2的15此方)
最大值:Short.MAX_VALUE=32767 (2的15次方-1)
2、
基本类型:int 二进制位数:32
包装类:java.lang.Integer
最小值:Integer.MIN_VALUE= -2147483648 (-2的31次方)
最大值:Integer.MAX_VALUE= 2147483647 (2的31次方-1)
3、
基本类型:long 二进制位数:64
包装类:java.lang.Long
最小值:Long.MIN_VALUE=-9223372036854775808 (-2的63次方)
最大值:Long.MAX_VALUE=9223372036854775807 (2的63次方-1)
4、
基本类型:float 二进制位数:32
包装类:java.lang.Float
最小值:Float.MIN_VALUE=1.4E-45 (2的-149次方)
最大值:Float.MAX_VALUE=3.4028235E38 (2的128次方-1)
5、
基本类型:double 二进制位数:64
包装类:java.lang.Double
最小值:Double.MIN_VALUE=4.9E-324 (2的-1074次方)
最大值:Double.MAX_VALUE=1.7976931348623157E308 (2的1024次方-1) - char 16bit/2byte \u0000~\uFFFF,unicode编码
- 在计算机中,正数以原码形式存在,负数以补码形式存在。以byte为例:
0000 0001代表数字1,1000 0000 代表数字-1,因此byte的最大值为
0111 1111即数字127,最小值为1111 1111也就是数字-128
借鉴:https://www.cnblogs.com/Wilange/p/7732236.html
Java中的8种基本数据类型的更多相关文章
- JAVA中的四种JSON解析方式详解
JAVA中的四种JSON解析方式详解 我们在日常开发中少不了和JSON数据打交道,那么我们来看看JAVA中常用的JSON解析方式. 1.JSON官方 脱离框架使用 2.GSON 3.FastJSON ...
- main方法中声明8种基本数据类型的变量并赋值
main方法中声明8种基本数据类型的变量并赋值 char→ int→ long→ float→ double byte→ short→
- JAVA基础学习之throws和throw的区别、Java中的四种权限、多线程的使用等(2)
1.throws和throw的区别 throws使用在函数外,是编译时的异常,throw使用在函数内,是运行时的异常 使用方法 public int method(int[] arr) throws ...
- Java中的五种单例模式实现方法
[代码] Java中的五种单例模式实现方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2 ...
- JAVA中的四种引用以及ReferenceQueue和WeakHashMap的使用示例
简介: 本文主要介绍JAVA中的四种引用: StrongReference(强引用).SoftReferenc(软引用).WeakReferenc(弱引用).PhantomReference(虚引用) ...
- java中有哪4种整形数据类型?它们有什么不同之处?
java中有哪4种整形数据类型?它们有什么不同之处? (byte.short.int和long型)最大值和最小值都不一样.
- Java中的四种引用
引用定义 实际上,Java中存在四种引用,它们由强到弱依次是:强引用.软引用.弱引用.虚引用.下面我们简单介绍下这四种引用: 强引用(Strong Reference):通常我们通过new来创建一个新 ...
- java中的几种架构对象(PO,VO,DAO,BO,POJO)
java中的几种对象(PO,VO,DAO,BO,POJO) 一.PO :(persistant object ),持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生 ...
- java中的几种内部类
Java中的几种内部类 内部类,听名字就可以知道是什么意思,就是类里面的类.有成员内部类,静态内部类,局部内部类和匿名内部类. 下面说一个每种内部类的的使用. 一. 成员内部类
随机推荐
- Linux下扫描服务器IP地址是否冲突(arp-scan)
部署服务突然发现,连接的服务器断开了,因为服务器用户名密码是一样的,所以重新连接后,发现文件变了,跟之前不一样. 猜想是不是ip地址冲突了,两次连接的服务器不同. 网上查找资料说可以用工具扫描.工具: ...
- Linux下Docker的安装与使用
yum安装Docker最新版和docker-compose(超级简单的安装方法) Install Docker 首先安装依赖 yum install -y yum-utils device-mappe ...
- C# 根据链接提取div内容
安装NuGet包 HtmlAgilityPack var wc = new WebClient(); wc.Encoding = Encoding.GetEncoding("UTF-8 ...
- Arrays.sort() VS Arrays.parallelSort()
英文原文地址:Arrays.sort vs Arrays.parallelSort 作者:baeldung 翻译:高行行 1. 概述 我们都使用过 Arrays.sort() 对对象或原始数据类型数组 ...
- 在浏览器窗口内移动的div
------------今天研究了一个最简单的屏保效果----------- 效果图如下:效果很神奇,就是这个div在浏览器窗口不断的灵活移动 代码却很简单 <!DOCTYPE html> ...
- C++数值计算
1.序 (1)程序设计分两种: 1.结构化设计(面向过程)——分解算法为模块,将算法的步骤分解为模块. 2.面向对象程序设计——主要是“类”与“对象”. (2)进制的转换 1.二进制转十进制 整数部分 ...
- Vue 组件复用性和slot
1.组件可复用 2.slot元素作为组件模板之中的内容分发插槽,元素自身可以被替换 <!DOCTYPE html> <html lang="en"> < ...
- java8 stream自定义分组求和并排序
public static void main(String[] args) { List<GroupDetailDTO> list = new ArrayList<>(); ...
- Elasticsearch如何修改Mapping结构并实现业务零停机
Elasticsearch 版本:6.4.0 一.疑问 在项目中后期,如果想调整索引的 Mapping 结构,比如将 ik_smart 修改为 ik_max_word 或者 增加分片数量 等,但 El ...
- webpack构建工具初始化并运行简单的demo
webpack官网:https://webpack.js.org/ webpack是构建工具 安装webpack的前提:node,npm要安装 初始化项目 首先是初始化项目,创建一个文件夹,并且进入文 ...