编程语言
首页 > 编程语言> > Java语言学习day33--8月8日

Java语言学习day33--8月8日

作者:互联网

今日内容介绍
1、基本类型包装类
2、System类
3、Math类
4、Arrays类
5、大数据运算


 

 

 

 

###01基本数据类型对象包装类概述
*A:基本数据类型对象包装类概述
*a.基本类型包装类的产生
在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的。而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类型,考试成绩需要转换成double类型等
*b.八种基本类型对应的包装类
char Character
int Integer
byte Byte
short Short
long Long
float Float
double Double
boolean Boolean

###02Integer类parseInt方法
*A:Integer类parseInt方法:
*a:parseInt()
int i = Integer.parseInt("12");
System.out.println(i/2);//6

*b:parseInt(String s, int radix)
/*
* Integer类静态方法parseInt(String s, int radix)
* radix基数,进制
* "110",2 含义 前面的数字是二进制的,但是方法parseInt运行结果都是十进制
* 指定进制的字符串转换为十进制的整数
*/
public static void function_1(){
int i = Integer.parseInt("110", 2);
System.out.println(i);
}
###03Integer类int转成字符串
*A:Integer类int转成字符串:
*a:使用+与字符串拼接
int i = 3;
String s = i+"";
System.out.println(s+1);//"31"

*b:toString(int ,int 进制),任意进制整数转成任意进制的字符串 (了解)
String s1 = Integer.toString(5,2);
System.out.println(s1);

###04Integer类构造方法
*A:Integer类构造方法
/*
* Integer类构造方法
* Integer (String s)
* 将数字格式的字符串,传递到Integer类的构造方法中
* 创建Integer对象,包装的是一个字符串
* 将构造方法中的字符串,转成基本数据类型,调用方法,非静态的, intValue()
*/
public static void function_3(){
Integer in = new Integer("100");
int i = in.intValue();
System.out.println(--i);//99
}


###05Integer类其他方法
*A:Integer类其他方法
/*
* Integer类的3个静态方法
* 做进制的转换
* 十进制转成二进制 toBinarString(int)
* 十进制转成八进制 toOctalString(int)
* 十进制转成十六进制 toHexString(int)
* 三个方法,返回值都是以String形式出现
*/
a:十进制转二,八,十六进制
public static void function_1(){
System.out.println(Integer.toBinaryString(99));
System.out.println(Integer.toOctalString(99));
System.out.println(Integer.toHexString(999));
}
b:获取int的最大值和最小值
/*
* Integer类的静态成员变量
* MAX_VALUE
* MIN_VALUE
*/
public static void function(){
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}

###06自动装箱和自动拆箱
*A:自动装箱与自动拆箱:
//JDK1.5新特性
//自动装箱,拆箱的 好处: 基本类型和引用类直接运算
//自动装箱:使用Integer.valueOf(整数值)返回一个封装了该整数值的Integer对象
//自动拆箱:使用Integer对象.intValue()返回Integer对象中封装的整数值
public static void function(){
//引用类型 , 引用变量一定指向对象
//自动装箱, 基本数据类型1, 直接变成了对象

Integer in = 1; // Integer in = new Integer(1)
//in 是引用类型,不能和基本类型运算, 自动拆箱,引用类型in,转换基本类型

//in+1 ==> in.inValue()+1 = 2
//in = 2 自动装箱
in = in + 1;

System.out.println(in);

}

###07自动装箱和自动拆箱练习题
*A:自动装箱与自动拆箱:
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i==j);// false 对象地址
System.out.println(i.equals(j));// true 继承Object重写equals,比较的对象数据

System.out.println("===================");

Integer a = 500;//Integer integer=Integer.valueOf(500)
//integer=new Integer(500);
Integer b = 500;
System.out.println(a==b);//false
System.out.println(a.equals(b));//true

System.out.println("===================");


//数据在byte(-128~127)范围内,JVM不会从新new对象
Integer aa = 127; // Integer aa = new Integer(127)
Integer bb = 127; // Integer bb = aa;
System.out.println(aa==bb); //true
System.out.println(aa.equals(bb));//true

=========================第二节课开始====================================
###08System类方法currentTimeMillis
*A:System类方法currentTimeMillis():用于计算程序的执行时间
/*
* 获取系统当前毫秒值
* static long currentTimeMillis()
* 对程序执行时间测试
*/
public static void function(){
long start = System.currentTimeMillis();//当前时间x-1970年1月1日零时零分零秒
for(int i = 0 ; i < 10000; i++){
System.out.println(i);
}
long end = System.currentTimeMillis();//当前时间y-1970年1月1日零时零分零秒
System.out.println(end - start);//当前时间y-当前时间x
}

###09System类方法exit
*A:System类方法exit()方法
/*
* 退出虚拟机,所有程序全停止
* static void exit(0)
*/
public static void function_1(){
while(true){
System.out.println("hello");
System.exit(0);//该方法会在以后的finally代码块中使用(讲到再说)
}
}
###10System类方法gc
A:System类方法gc
public class Person {
public void finalize(){
System.out.println("垃圾收取了");
}
}

/*
* JVM在内存中,收取对象的垃圾
* 当没有更多引用指向该对象时,会自动调用垃圾回收机制回收堆中的对象
* 同时调用回收对象所属类的finalize方法()
* static void gc()
*/
public static void function_2(){
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
new Person();
System.gc();
}

###11System类方法getProperties
A:System类方法getProperties(了解)
/*
* 获取当前操作系统的属性:例如操作系统名称,
* static Properties getProperties()
*/
public static void function_3(){
System.out.println( System.getProperties() );
}

###12System类方法arraycopy
A:System类方法arraycopy:
/*
* System类方法,复制数组
* arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* Object src, 要复制的源数组
* int srcPos, 数组源的起始索引
* Object dest,复制后的目标数组
* int destPos,目标数组起始索引
* int length, 复制几个
*/
public static void function_4(){
int[] src = {11,22,33,44,55,66};
int[] desc = {77,88,99,0};

System.arraycopy(src, 1, desc, 1, 2);//将src数组的1位置开始(包含1位置)的两个元素,拷贝到desc的1,2位置上
for(int i = 0 ; i < desc.length ; i++){
System.out.println(desc[i]);
}
}

标签:Java,--,day33,System,int,new,println,Integer,out
来源: https://www.cnblogs.com/open52000/p/15115944.html