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; }
    1. 基本类型,在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)
    2. char 16bit/2byte \u0000~\uFFFF,unicode编码
    3. 在计算机中,正数以原码形式存在,负数以补码形式存在。以byte为例: 
      0000 0001代表数字1,1000 0000 代表数字-1,因此byte的最大值为 
      0111 1111即数字127,最小值为1111 1111也就是数字-128

借鉴:https://www.cnblogs.com/Wilange/p/7732236.html

Java中的8种基本数据类型的更多相关文章

  1. JAVA中的四种JSON解析方式详解

    JAVA中的四种JSON解析方式详解 我们在日常开发中少不了和JSON数据打交道,那么我们来看看JAVA中常用的JSON解析方式. 1.JSON官方 脱离框架使用 2.GSON 3.FastJSON ...

  2. main方法中声明8种基本数据类型的变量并赋值

    main方法中声明8种基本数据类型的变量并赋值  char→  int→ long→ float→ double byte→ short→ 

  3. JAVA基础学习之throws和throw的区别、Java中的四种权限、多线程的使用等(2)

    1.throws和throw的区别 throws使用在函数外,是编译时的异常,throw使用在函数内,是运行时的异常 使用方法 public int method(int[] arr) throws ...

  4. 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 ...

  5. JAVA中的四种引用以及ReferenceQueue和WeakHashMap的使用示例

    简介: 本文主要介绍JAVA中的四种引用: StrongReference(强引用).SoftReferenc(软引用).WeakReferenc(弱引用).PhantomReference(虚引用) ...

  6. java中有哪4种整形数据类型?它们有什么不同之处?

    java中有哪4种整形数据类型?它们有什么不同之处? (byte.short.int和long型)最大值和最小值都不一样.

  7. Java中的四种引用

    引用定义 实际上,Java中存在四种引用,它们由强到弱依次是:强引用.软引用.弱引用.虚引用.下面我们简单介绍下这四种引用: 强引用(Strong Reference):通常我们通过new来创建一个新 ...

  8. java中的几种架构对象(PO,VO,DAO,BO,POJO)

    java中的几种对象(PO,VO,DAO,BO,POJO)   一.PO :(persistant object ),持久对象 可以看成是与数据库中的表相映射的java对象.使用Hibernate来生 ...

  9. java中的几种内部类

    Java中的几种内部类 内部类,听名字就可以知道是什么意思,就是类里面的类.有成员内部类,静态内部类,局部内部类和匿名内部类. 下面说一个每种内部类的的使用. 一.  成员内部类

随机推荐

  1. 某个应用的CPU使用率居然达到100%,我该怎么办?

    > 本文是通过学习极客时间专栏<Linux性能优化实战>05 | 基础篇:某个应用的CPU使用率居然达到100%,我该怎么办? ## CPU 使用率 *** 为了维护 CPU 时间, ...

  2. 突破CRUD | 简单优雅的代码生成工具诞生记(万字长文慎入)

    0.学习本文你或许可以收获 1.一个需求从产生.分析到解决的全过程思考2.简单的面向对象分析实践3.UML类图实践4.设计模式的实践应用5.最后收获一款还算不错的代码生成工具实现思路和源代码 本文将从 ...

  3. linux防火墙之iptables

    linux防火墙之iptables 1.1.1 关于iptables简介 IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 ...

  4. Win7旗舰版仅供测试支持正版

    系统效果展示 安装后唯一标准的桌面截图:(如发现安装后与本图不一致,均为第三方安装工具捆绑所为,请注意使用工具!慎用XX桃.XX菜.uXX之类的工具,建议使用推荐的方法安装) 如此清新简洁的安装界面, ...

  5. python之字符串的简单应用

    1.实现5+7加法运算 value = input(">>>") v1, v2 = value.split('+') c1 = int(v1) c2 = int( ...

  6. Markdown 复杂公式&常用符号

    公式格式 行内公式 行内公式(不会换行)使用 $ 作为起止符,例如:$a + b = c$, 效果为:\(a + b = c\) 块级公式 块级公式(单独一行)使用 $$ 作为起止符,例如:$$a + ...

  7. TensorFlow——dropout和正则化的相关方法

    1.dropout dropout是一种常用的手段,用来防止过拟合的,dropout的意思是在训练过程中每次都随机选择一部分节点不要去学习,减少神经元的数量来降低模型的复杂度,同时增加模型的泛化能力. ...

  8. spring-boot内嵌三大容器https设置

    spring-boot内嵌三大容器https设置 spring-boot默认的内嵌容器为tomcat,除了tomcat之前还可以设置jetty和undertow. 1.设置https spring-b ...

  9. .net Core 使用IHttpClientFactory请求

            导读:本文已添加在晨曦微服务之旅,现在自己在尝试微服务架构,一边学边做项目快速的进入状态.当然在学习的过程中会将自己学到的知识进行分享. 一.为什么不用HttpClient       ...

  10. 2次方的期望dp

    某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(    我们来简化一下这个游戏的规则    有n次点击要做,成功了就是o,失败了就是x,分数是按comb计算的,连续a个com ...