Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42378
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 726
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-24 08:29
文章分类

全部博文(71)

文章存档

2015年(71)

我的朋友

分类: Java

2015-02-11 16:09:18

int、float、double、boolean、char 等。基本数据类型是不具备对象的特性的,比如基本类型不能调用方法、功能简单。。。,为了让基本数据类型也具备对象的特性, Java 为每个基本数据类型都提供了一个包装类,这样我们就可以像操作对象那样来操作基本数据类型。 

基本类型和包装类之间的对应关系:

包装类主要提供了两大类方法:

1. 将本类型和其他基本类型进行转换的方法

2. 将字符串和本类型及包装类互相转换的方法


点击(此处)折叠或打开

  1. public class HelloWorld {
  2.     public static void main(String[] args) {
  3.         
  4.         // 定义int类型变量,值为86
  5.         int score1 = 86;
  6.         
  7.         // 创建Integer包装类对象,表示变量score1的值
  8.         Integer score2=new Integer(score1);
  9.         
  10.         // 将Integer包装类转换为double类型
  11.         double score3=score2.doubleValue();
  12.         
  13.         // 将Integer包装类转换为float类型
  14.         float score4=score2.floatValue();
  15.         
  16.         // 将Integer包装类转换为int类型
  17.         int score5 =score2.intValue();

  18.         System.out.println("Integer包装类:" + score2);
  19.         System.out.println("double类型:" + score3);
  20.         System.out.println("float类型:" + score4);
  21.         System.out.println("int类型:" + score5);
  22.     }
  23. }

阅读(130) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~