基本数据类型的包装类

基本数据类型的包装类

Math,Random,Date,Calendar,BigDecimal,Runtime

  • Date的使用

1)java.util.Date该类,是处理时间的,可以拿到年,月,日,小时,…;

2)格式化和解析日期字符串

     SimpleDateFromate对象

String str="[2018][04][03][13][15][36]";

                  

                   SimpleDateFormat sdf=new SimpleDateFormat("[yyyy][MM][dd][HH][mm][ss]");

                  

                   //通过SimpleDateFormat对象进行解析字符串为Date

                   Date date=sdf.parse(str);

                  

                   //获取到年,月,日,小时,分,秒

                   int year=date.getYear();//过时方法

                   int m=date.getMonth();

                   int d=date.getDate();//一个月中的哪一天

                   int h=date.getHours();

                   int m1=date.getMinutes();

                   int s=date.getSeconds();

                  

                   //通过SimpleDateFormat对象进将时间转为指定的格式字符串

                   SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd HH小时mm分钟ss秒");

                   String timeStr=sdf1.format(date);

                   System.out.println(timeStr);

 

常量:常量是不能改变的一些量,比如pi(3.14159);

final  double  pi=3.14159;

 

 

pi=5;

 

  • Calendar

是Date的升级,更加注重的是时间的运算,如果是简单的将时间转为指定字符串或者将字符串转为时间用Date

复杂时可以结合使用。

 

//今天对应的下个月是周几

 

                   String str1="[2018][04][03][13][15][36]";

                  

                   SimpleDateFormat sdf=new SimpleDateFormat("[yyyy][MM][dd][HH][mm][ss]");

                  

                   Date date1=sdf.parse(str1);

                 

                  

                  

                   Calendar cal1=Calendar.getInstance();//得到当前系统的时间

                   cal1.setTime(date1);//将Date放置在Calendar对象中

                  

                   System.out.println(cal1);

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

                   //后10天

                   cal1.add(Calendar.DATE, -10);

                  

                   System.out.println(cal1);

 

  • Random

获取随机数的

//创建对象Random

                   Random r=new Random();

                  

//               for(int i=0;i<10;i++) {

//                         boolean f=r.nextBoolean();

//                        

//                         System.out.println(f);

//               }

                  

                  

                  

                   for(int i=0;i<10;i++) {

                            int j=r.nextInt(36);

                           

                            System.out.println(j);

                  }

                  

                  

 

  • Math

数学运算的处理

public static void main(String[] args) {

                   //返回比他小的或等的整数

                   double d=Math.floor(-5.4);

                   System.out.println(d);//-5.9==-6.0

                  

                  

                   double d1=Math.floor(5.4);

                   System.out.println(d1);//5.0

                  

                   //四舍五入,负数比较特殊的

                   //返回参数中最接近的 long ,其中 long四舍五入为正无穷大

                   long d2=Math.round(-5.51) ;//-5.6==>-6   -5.50==>-5   -5.4==>-5   负数时以 0.5轴,大了  整数-1

                   System.out.println(d2);

                  

                   long d3=Math.round(5.5) ;// 5.5==>6     5.5==>6     5.4==>5

                   System.out.println(d3);

                  

                  

                  

         }

 

 

  • 基本数据类型的包装类

基本数据类型

包装类

最大最小值

byte

Byte

Byte.MAX_VAVLUE; Byte.MIN_VAVLUE

short

Short

 

int

Integer

 

long

Long

 

char

Character

 

boolean

Boolean

 

float

Float

 

double

Double

 

 

将基本数据类型转为包装类时,装包,包装以后可以调用更多的方法

byte b=123;

Byte c=b ,自动装包

 

byte  b1=c;//自动拆包

 

//将字符串,转为基本数据类型或者包装类

String str="123";

                  

                   int i=Integer.parseInt(str);

                  

                   Integer j=Integer.valueOf(str);

 

                   System.out.println(i+j);

                  

                  

 

 

 

 

 

//转为字符串

1)包装类都toString方法可以直接转为字符串;

2)基本数据类型可以通过  String.valueOf(基本数据类型);

3)+””

         String str="false";

                  

                   boolean f1=Boolean.parseBoolean(str);

                  

                   Boolean f2=Boolean.valueOf(str);

                  

                   //包装类的toString

                   String str2=f2.toString();

                   String str22=f2+"";

                  

                   //基本数据类型

                   String str3=String.valueOf(f1);

                   String str33=f1+"";

                  

 

 

==  比较Integer的数值,如果在 -128~127之间,包括边界,此时结果为true,超出范围为false

public static  void main(String[] args) {

                  

                   int  a=123;

                   int  b=123;

                   boolean f= a==b;//true

                  

                   Integer a1=123;

                   Integer b1=123;

                   boolean f1= a1==b1;//true

                  

 

                   Integer a2=127;

                   Integer b2=127;

                   boolean f2= a2==b2;//true

                  

 

                   Integer a3=128;

                   Integer b3=128;

                   boolean f3= a3==b3;//false

                  

                   Integer a4=-128;

                   Integer b4=-128;

                   boolean f4= a4==b4;//true

 

                   Integer a5=-129;

                   Integer b5=-129;

                   boolean f5= a5==b5;//false

                  

                   System.out.println();

         }

 

 

 

 

  • BigDecimal

提供加减乘除运行,主要基本数据类型运算时因为二进制,没办法先想要的结果,可以通过他进行处理

BigDecimal a=new BigDecimal("8.0");//需要写数据对应的字符串

                   BigDecimal b=new BigDecimal("4.0");

                  

                   BigDecimal c=a.subtract(b);//a-b  div

                  

                   //BigDecimal c=a.divide(b);

                  

                   double result=c.doubleValue();//将BigDecimal转为double

                  

                   System.out.println(result);

                  

                   System.out.println(1.0/7);

 

 

 

  • 运行时

Runtime开启和关闭应用

Runtime runTime=Runtime.getRuntime();

                  

                   //通过该程序打卡一个应用

                   String fileName="C:/Windows/system32/calc.exe";

                  

                   Process process=runTime.exec(fileName);

                  

                   Thread.sleep(1000*10);//程序

                  

                   //关闭Process

                   process.destroy();

 查看jvm中现在的内存消耗情况

 

                   Runtime runTime=Runtime.getRuntime();

                   //空闲的内存

                   long freeM=runTime.freeMemory();

                   //最大内存

                   long max=runTime.maxMemory();

                   //总内存

                   long count =runTime.totalMemory();

                   System.out.println("期望的最大:"+max+",总内存:"+count+",可用内存:"+freeM+",已用内存"+(count-freeM));

                  

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

                   Scanner sc=new Scanner(System.in);

                  

                   int[] arr= {1234,3455,4566,456,678,567,567,456,4567,4567};

                  

                   runTime.gc();//垃圾回收机进行运行

                  

                   //空闲的内存

                   freeM=runTime.freeMemory();

                   //总内存

                    count =runTime.totalMemory();

                    System.out.println("期望的最大:"+max+",总内存:"+count+",可用内存:"+freeM+",已用内存"+(count-freeM));

                  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值