装箱、拆箱以及Math类
2016-08-22 17:49
134 查看
import java.util.Random;
import java.util.Scanner;
public class IntegerTest {
public static void main(String[] args) {
/*装箱:把基本数据类型转换为对应对象类型
*作用:1、在需要用到对象类型的时候,可以转换成对应的对象类型(集合里面)
* 2、转换成对象以后,拥有相应的属性和方法,方便进行主句操作
*/
//方法1
/*int i = 5;
System.out.println(i);*/
/* Integer integer = new Integer(12);//装箱,数据产生对应的属性和方法
System.out.println("装箱"+integer);
int i = integer;
System.out.println("拆箱"+i);
//方法二
Integer integer2 = new Integer("15");//引号里面只能是纯数字
System.out.println("装箱"+integer2);
int j = integer2;
System.out.println("拆箱"+j);
//方法三
Integer integer3 = Integer.valueOf(21);//装箱
System.out.println(integer3);
int k = integer3.intValue();//拆箱
System.out.println(k);
//方法四
Integer integer4 = 66;//装箱
int l = integer4;//拆箱
System.out.println(l);
//字符串和基本数据类型的转换
//字符串转换成基本数据类型
int i1 = Integer.parseInt("123");
System.out.println(i1);
float f = Float.parseFloat("123");
System.out.println(f);
double d = Double.parseDouble("123");
System.out.println(d);
boolean b = Boolean.parseBoolean("true");
System.out.println(b);
//基本数据类型转换成字符串类型
int i2 = 123;
String s = Integer.toString(i2);
System.out.println(s);
String s2 = 123+"";
System.out.println(s2);
String s3 = i2+"";
System.out.println(s3);
String s4 = '男'+"";
System.out.println(s4);
//Math类
//输出最大值
int num1 = Math.max(20,25);
System.out.println(num1);
//输出最小值
int num2 = Math.min(20,25);
System.out.println(num2);
//输出3的平方(前面数的后面数次方)
double num3 = Math.pow(3, 2);
System.out.println(num3);
//开根号
double num4 = Math.sqrt(16);
System.out.println(num4);
//abs(求绝对值)
int i6 = -6;
int abs = Math.abs(i6);
System.out.println(abs);
//返回三角正弦值
double angle = (Math.PI)/2;
double sin = Math.sin(angle);
System.out.println(sin);
//返回三角余弦值
double angle1 = (Math.PI)/6;
double cos = Math.cos(angle1);
System.out.println(cos);
//正切
double angle2 = (Math.PI)/4;
double tan = Math.tan(angle2);
System.out.println(tan);
//正切
double angle3 = (Math.PI)/4;
double asin = Math.asin(angle3);
System.out.println(asin);*/
/*
//random 返回一个从0到1的随机数 包括0 ,不包括1。
//输出一个从1到10 的随机数
int random = (int)(Math.random()*10+1);
System.out.println(random);
//随机输出0或1
int money = Math.random()>=0.5?1:0;
System.out.println(money);
//随机输出5-10之间的数
int check = (int)(Math.random()*6+5);
System.out.println(check);
*/
/*
//计算半径3.5的圆面积
System.out.println("计算半径3.5的圆面积");
double r = 3.5;
System.out.println( Math.pow(r,2)*Math.PI);
//随机输出5个1-6的整数
System.out.println("随机输出5个1-6的整数");
for(int a = 0;a<5;a++){
System.out.print((int)(Math.random()*6+1)+"\t");
}
System.out.println();
//找出1-10之间的两个随机数,并输出最小的
System.out.println("找出1-10之间的两个随机数,并输出最小的");
int random1 = (int)(Math.random()*10+1);
int random2 = (int)(Math.random()*10+1);
System.out.println(random1+" "+random2);
System.out.println(Math.min(random1, random2) );
*/
/*
String result = String.valueOf(1);
System.out.println(result);
String nums = "abc";
byte[] bytes = nums.getBytes();
System.out.println(Arrays.toString(bytes));
*/
/*
//将O替换为0,将i替换为1
String str = "272O50i66";
str = str.replace('O', '0');
str = str.replace('i', '1');
System.out.println(str);
String str1 = "My_English_NaMe";
String[] str2 = str1.split("_");
str2[0] = str2[0].toLowerCase();
str2[1] = str2[1].toUpperCase();
str2[2] = str2[2].toUpperCase();
str1 = str2[0]+str2[1]+str2[2];
System.out.println(str1);
*/
/*
Scanner input = new Scanner(System.in);
StringBuffe
b8ab
r sb = new StringBuffer();
String word;
do{
System.out.println("请输入单词:");
word = input.next();
if(word.equals("end")){
System.out.println("结束输入!");
break;
}else{
StringBuffer sb1 =sb.append(word+",");
sb = sb1;
}
}while(!word.equals("end"));
System.out.println(sb);
String result = sb.toString();
String[] result1 = result.split(",");
for(int i = 0;i<result1.length;i++){
System.out.println(result1[i]);
}
input.close();
*/
//Random
Random random = new Random();
int i = random.nextInt(26);
System.out.println(i);
long i2 = random.nextLong();
System.out.println(i2);
System.out.println("输出10个随机boolean类型数,求各占比率");
double num = 0;
for(int j = 0;j < 10; j++){
boolean boo = random.nextBoolean();
if(boo){
num ++;
}
System.out.print(boo+"\t");
}
System.out.println();
System.out.println(num/10);
System.out.println("输出10个1-10之间随机double类型数");
System.out.println();
for(int j = 0;j < 10; j++){
System.out.println(random.nextDouble()*9+1);
}
System.out.println("两位随机整数");
System.out.println((int)(random.nextDouble()*90+10));
}
}
import java.util.Scanner;
public class IntegerTest {
public static void main(String[] args) {
/*装箱:把基本数据类型转换为对应对象类型
*作用:1、在需要用到对象类型的时候,可以转换成对应的对象类型(集合里面)
* 2、转换成对象以后,拥有相应的属性和方法,方便进行主句操作
*/
//方法1
/*int i = 5;
System.out.println(i);*/
/* Integer integer = new Integer(12);//装箱,数据产生对应的属性和方法
System.out.println("装箱"+integer);
int i = integer;
System.out.println("拆箱"+i);
//方法二
Integer integer2 = new Integer("15");//引号里面只能是纯数字
System.out.println("装箱"+integer2);
int j = integer2;
System.out.println("拆箱"+j);
//方法三
Integer integer3 = Integer.valueOf(21);//装箱
System.out.println(integer3);
int k = integer3.intValue();//拆箱
System.out.println(k);
//方法四
Integer integer4 = 66;//装箱
int l = integer4;//拆箱
System.out.println(l);
//字符串和基本数据类型的转换
//字符串转换成基本数据类型
int i1 = Integer.parseInt("123");
System.out.println(i1);
float f = Float.parseFloat("123");
System.out.println(f);
double d = Double.parseDouble("123");
System.out.println(d);
boolean b = Boolean.parseBoolean("true");
System.out.println(b);
//基本数据类型转换成字符串类型
int i2 = 123;
String s = Integer.toString(i2);
System.out.println(s);
String s2 = 123+"";
System.out.println(s2);
String s3 = i2+"";
System.out.println(s3);
String s4 = '男'+"";
System.out.println(s4);
//Math类
//输出最大值
int num1 = Math.max(20,25);
System.out.println(num1);
//输出最小值
int num2 = Math.min(20,25);
System.out.println(num2);
//输出3的平方(前面数的后面数次方)
double num3 = Math.pow(3, 2);
System.out.println(num3);
//开根号
double num4 = Math.sqrt(16);
System.out.println(num4);
//abs(求绝对值)
int i6 = -6;
int abs = Math.abs(i6);
System.out.println(abs);
//返回三角正弦值
double angle = (Math.PI)/2;
double sin = Math.sin(angle);
System.out.println(sin);
//返回三角余弦值
double angle1 = (Math.PI)/6;
double cos = Math.cos(angle1);
System.out.println(cos);
//正切
double angle2 = (Math.PI)/4;
double tan = Math.tan(angle2);
System.out.println(tan);
//正切
double angle3 = (Math.PI)/4;
double asin = Math.asin(angle3);
System.out.println(asin);*/
/*
//random 返回一个从0到1的随机数 包括0 ,不包括1。
//输出一个从1到10 的随机数
int random = (int)(Math.random()*10+1);
System.out.println(random);
//随机输出0或1
int money = Math.random()>=0.5?1:0;
System.out.println(money);
//随机输出5-10之间的数
int check = (int)(Math.random()*6+5);
System.out.println(check);
*/
/*
//计算半径3.5的圆面积
System.out.println("计算半径3.5的圆面积");
double r = 3.5;
System.out.println( Math.pow(r,2)*Math.PI);
//随机输出5个1-6的整数
System.out.println("随机输出5个1-6的整数");
for(int a = 0;a<5;a++){
System.out.print((int)(Math.random()*6+1)+"\t");
}
System.out.println();
//找出1-10之间的两个随机数,并输出最小的
System.out.println("找出1-10之间的两个随机数,并输出最小的");
int random1 = (int)(Math.random()*10+1);
int random2 = (int)(Math.random()*10+1);
System.out.println(random1+" "+random2);
System.out.println(Math.min(random1, random2) );
*/
/*
String result = String.valueOf(1);
System.out.println(result);
String nums = "abc";
byte[] bytes = nums.getBytes();
System.out.println(Arrays.toString(bytes));
*/
/*
//将O替换为0,将i替换为1
String str = "272O50i66";
str = str.replace('O', '0');
str = str.replace('i', '1');
System.out.println(str);
String str1 = "My_English_NaMe";
String[] str2 = str1.split("_");
str2[0] = str2[0].toLowerCase();
str2[1] = str2[1].toUpperCase();
str2[2] = str2[2].toUpperCase();
str1 = str2[0]+str2[1]+str2[2];
System.out.println(str1);
*/
/*
Scanner input = new Scanner(System.in);
StringBuffe
b8ab
r sb = new StringBuffer();
String word;
do{
System.out.println("请输入单词:");
word = input.next();
if(word.equals("end")){
System.out.println("结束输入!");
break;
}else{
StringBuffer sb1 =sb.append(word+",");
sb = sb1;
}
}while(!word.equals("end"));
System.out.println(sb);
String result = sb.toString();
String[] result1 = result.split(",");
for(int i = 0;i<result1.length;i++){
System.out.println(result1[i]);
}
input.close();
*/
//Random
Random random = new Random();
int i = random.nextInt(26);
System.out.println(i);
long i2 = random.nextLong();
System.out.println(i2);
System.out.println("输出10个随机boolean类型数,求各占比率");
double num = 0;
for(int j = 0;j < 10; j++){
boolean boo = random.nextBoolean();
if(boo){
num ++;
}
System.out.print(boo+"\t");
}
System.out.println();
System.out.println(num/10);
System.out.println("输出10个1-10之间随机double类型数");
System.out.println();
for(int j = 0;j < 10; j++){
System.out.println(random.nextDouble()*9+1);
}
System.out.println("两位随机整数");
System.out.println((int)(random.nextDouble()*90+10));
}
}
相关文章推荐
- JAVA自动装箱拆箱以及装箱时的缓存问题
- 【Java】Java的==和equals()以及自动装箱拆箱
- csharp004值类型引用类型区别,以及装箱拆箱以及转换相关
- java String的比较,BOX装箱拆箱,以及面向对象的小代码
- Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?
- c#运算符使用,以及装箱拆箱
- Java中的基本类型与封装类型以及自动装箱、拆箱
- 装箱与拆箱以及性能损失
- 装箱与拆箱以及性能损失
- 装箱与拆箱以及性能损失
- 堆栈和托管堆以及装箱和拆箱的理解
- int与Integer,long与Long的区别以及装箱与拆箱的理解
- 自动装箱/拆箱,以及高速缓存问题
- 黑马程序员--张孝祥Java高新技术-JDK1.5新特性(二)【基本数据类型的自动拆箱与装箱,枚举】以及享元设计模式
- 关于静态导入,装箱与拆箱,枚举,以及可变参数的学习
- 基本数据类型运算以及自动装箱与拆箱
- 再谈CLR:值类型按照引用传递(以及与装箱拆箱的区别)
- Java中的基本类型与封装类型以及自动装箱、拆箱、String类型的解释
- java Integer类以及拆箱和装箱
- 装箱和拆箱和ValueOf以及XXXValue -- Java语法糖(Syntax sugar)