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中的几种内部类 内部类,听名字就可以知道是什么意思,就是类里面的类.有成员内部类,静态内部类,局部内部类和匿名内部类. 下面说一个每种内部类的的使用. 一. 成员内部类
随机推荐
- 1092 最好吃的月饼 (20分)C语言
月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种"最好吃"的月饼,那势必在吃货界引发一场腥风血雨-- 在这里我们用数字说话,给出全国各地各种月饼的 ...
- 前端加密MD5
今天接触了MD5加密方式,记录一下使用方法,又去搜了搜关于MD5的详细内容 MD5在vue中使用方法 1.下载MD5模块 cnpm install md5 -S 2.引入模块 const md5 = ...
- spark(1.1) mllib 源码分析(三)-决策树
本文主要以mllib 1.1版本为基础,分析决策树的基本原理与源码 一.基本原理 二.源码分析 1.决策树构造 指定决策树训练数据集与策略(Strategy)通过train函数就能得到决策树模型Dec ...
- 傅立叶变换—FFT
FFT(快速傅立叶变换)使用“分而治之”的策略来计算一个n阶多项式的n阶DFT系数的值.定义n为2的整数幂数,为了计算一个n阶多项式f(x),算法定义了连个新的n/2阶多项式,函数f[0](x)包含了 ...
- VMware上安装Kali Linux 超详细教程
一.下载镜像文件 下载好系统对应镜像文件 https://www.kali.org/downloads/ 二.创建新的虚拟机 1.创建新的虚拟机 我们使用自定义的配置方法. 2.添加镜像文件的路径 ...
- 小白学 Python 爬虫(36):爬虫框架 Scrapy 入门基础(四) Downloader Middleware
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- npm安装报错npm ERR! Refusing to install package with name "xxxx" under a packagexxxx
npm ERR! code ENOSELF npm ERR! Refusing to install package with name "webpack" under a pac ...
- 830. String Sort
830. String Sort 题解 int alpha[256] = {0};//记录字符的次数 bool cmp(char a,char b) { if(alpha[a]==alpha[b])/ ...
- 通过例子进阶学习C++(五)计算2的1次方至2的64次方之和
本文是通过例子学习C++的第五篇,通过这个例子可以快速入门c++相关的语法. 1.上篇回顾 在上一篇中,我们通过字符数组计算264次方: 通过例子进阶学习C++(四)计算2的64次方 带着这个问题:为 ...
- python+opencv中最近出现的一些变化( OpenCV 官方的 Python tutorial目前好像还没有改过来?) 记一次全景图像的拼接
最近在学习过程中发现opencv有了很多变动, OpenCV 官方的 Python tutorial目前好像还没有改过来,导致大家在学习上面都出现了一些问题,现在做一个小小的罗列,希望对大家有用 做的 ...