Java常用类__装箱/拆箱
2024-09-05 08:41:59
以下是常用的各两种方法(各类提供了构造方法,静态方法)
一、基本数据类型 转化为 包装类(装箱)
例:int i=10;
Integer num=i;//num=10
二、包装类 转化为 基本数据类型 (拆箱)
例:Integer num=10;
int i=num;//i=10
三、基本数据类型 转化为 String类
例:int i=10;
String str=i+'' '';// "10"
四、String类 转化为 基本数据类型
例:String str="123";
int i=Integer.parseInt(str);//123
五、包装类 转化为 String类
例:Integer num=123;
String str=num+"";//"123"
六、String类 转化为 包装类
例:String str="123";
Integer num=Integer.parseInt(str);//123
随机数
一、Math
公式为 (int)(Math.Random()*(Max-Min+1)+Min)
二、Random
公式为 (int)(Random.nextInt(Max-Min+1)+Min)
例: 使用Math类随机产生100个2~99的的整数(包括2,包括99)
// 使用Math.random()产生随机数
for (int i = 0; i < 100; i++) {
System.out.print((int) (Math.random() * (99 - 2 + 1) + 2) + ","); if (i != 0 && i % 10 == 0) {
System.out.println();
}
} // 使用Random类产生随机数
Random r = new Random();
System.out.println(r.nextInt()); Random r1 = new Random(1000);// 提供种子
System.out.println(r1.nextInt());
Random r2 = new Random(1000);// 结果相同
System.out.println(r2.nextInt()); System.out.println(); for (int i = 0; i < 100; i++) {
System.out.print((int) (r2.nextInt(99 - 2 + 1)+ 2)+",");
if (i != 0 && i % 10 == 0) {
System.out.println();
}
}
最新文章
- 《与小卡特一起学Python》Code3 抓取网页中的某个数据
- Windows Server 2012 没有远程桌面授权服务器可以提供许可证,远程会话被中断
- 不同场景下 MySQL 的迁移方案
- HoloLens开发手记 - 应用程序模型 App model
- 【转】【C#】【Thread】Mutex 互斥锁
- JVM的GC理论详解
- VS插件开发
- c++ 实现百度自动搜索
- 8.css边框
- 01-08-02【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider
- eventlet的学习
- hdu_5794_A Simple Chess(lucas+dp)
- Web Worker 初探
- Python从入门到精通之First!
- SQL调优日记--并行等待的原理和问题排查
- python之全局安装包管理工具pip
- day 26
- Rsync 软件的工作方式
- SSH框架整合的其它方式
- python之ftp作业【还未完成】
热门文章
- 使用node打造自己的命令行
- Windows安装python包出现PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问的问题解决方案
- uWSGI, send_file and Python 3.5
- 深度学习与人类语言处理-语音识别(part1)
- C语言程序设计(十二) 结构体和共用体
- JsonFormat 日期少了8个小时?还我
- python3使用js2py
- 解决QQ“抱歉,无法发起临时会话,您可以 添加对方为好友以发送消息”
- 利用JDBC工具类添加和查询数据-Java(新手)
- 手把手教大家如何用scrapy爬虫框架爬取王者荣耀官网英雄资料