Java常用类-Java基础宋红康学习笔记

1、字符串相关的类

1.1、String的特性

package com.Java常用类;

import org.junit.Test;

public class StringTest {
/*
  * String字符串
  * 1、String声明为final的,不可被继承
  * 2、String实现了Serializable接口:表示字符串是支持序列化的
  *          实现了Comparable接口:表示String可以比较大小
  * 3、String内部定义了final char[] value用于存储字符串数据
  * 4、String:代表不可变的字符序列。简称:不可变性
  *   体现:1、当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值。
  *        2、当对现有的字符串进行操作时,也需要重新制定内存区域赋值,不能使用原有的value进行赋值
  *        3、当调用String的replace()方法修改指定字符串或字符时,也必须重新指定内存区域进行复制
  * 5、通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中
  * 6、字符串常量池中是不会存储相同内容的字符串的
  * */
  @Test
  public void test(){
      String s1 = "abc";
      String s2 = "abc";
      s1 = "hello";

      System.out.println(s1 == s2);//比较s1和s2的地址值

      System.out.println(s1);//hello
      System.out.println(s2);//abc

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

      String s3 = "abc";
      s3 += "def";
      System.out.println(s3);//abcdef
      System.out.println();

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

      String s4 = "abc";
      String s5 = s4.replace('a', 'm');
      System.out.println(s4);
      System.out.println(s5);

  }
  @Test
  /*
  * String的实例化方式
  *   方式一:通过字面量定义的方式
  *   方式二:通过new + 构造器的方式
  *
  *   面试题:String s = new String("asd");方式创建对象,在内存中创建了几个对象
  *           两个:一个是在堆空间中new结构,另一个是char[]对应的常量池中的数据:"abc"
  * */
  public void test2(){
      //通过字面量定义的方式:此时的s1和s2的数据javaEE声明在方法区中的字符串常量池中
      String s1 = "javaEE";
      String s2 = "javaEE";
      //通过new+构造器的方式:此时的s3和s4保存的地址,是数据在对空间中开辟空间以后对应的地址
      String s3 = new String("javaEE");
      String s4 = new String ("javaEE");

      System.out.println(s1==s2);//true
      System.out.println(s1==s3);//false
      System.out.println(s1==s4);//false
      System.out.println(s3==s4);//false

  }
  /*
  * 1、常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量
  * 2、只要其中有一个是变量,结构就在堆中
  * 3、如果拼接的结果调用intern()方法,返回值就在常量池中
  * */
  @Test
  public void test3(){
      String s1 = "javaEE";
      String s2 = "hadoop";

      String s3 = "javaEEhadoop";
      String s4 = "javaEE"+"hadoop";
      String s5 = s1 + "hadoop";
      String s6 = "javaEE"+s2;
      String s7 = s1 +s2;

      System.out.println(s3 == s4);//true
      System.out.println(s3 == s5);//false
      System.out.println(s3 == s6);//false
      System.out.println(s3 == s7);//false
      System.out.println(s5 == s6);//false
      System.out.println(s5 == s7);//false
      System.out.println(s6 == s7);//false

      String s8 = s5.intern();//返回值得到的s8使用的是常量池中已存在的"javaEEhadoop"
      System.out.println(s3 == s8);//true
  }
}

1.2、字符串的常用方法

  • int length():返回字符串的长度: return value.length

  • char charAt(int index): 返回某索引处的字符return value[index]

  • boolean isEmpty():判断是否是空字符串:return value.length == 0

  • String toLowerCase():使用默认语言环境,将 String 中的所有字符转换为小写

  • String toUpperCase():使用默认语言环境,将 String 中的所有字符转换为大写

  • String trim():返回字符串的副本,忽略前导空白和尾部空白

  • boolean equals(Object obj):比较字符串的内容是否相同

  • boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大 小写

  • String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+”

  • int compareTo(String anotherString):比较两个字符串的大小

  • String substring(int beginIndex):返回一个新的字符串,它是此字符串的从 beginIndex开始截取到最后的一个子字符串。

  • String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字 符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。

  • boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束

  • boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始

  • boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的 子字符串是否以指定前缀开始

  • boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列 时,返回 true

  • int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引

  • int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出 现处的索引,从指定的索引开始

  • int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引

  • int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后 一次出现处的索引,从指定的索引开始反向搜索 注:indexOf和lastIndexOf方法如果未找到都是返回-1

  • String replace(char oldChar, char newChar):返回一个新的字符串,它是 通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

  • String replace(CharSequence target, CharSequence replacement):使 用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。

  • String replaceAll(String regex, String replacement) : 使 用 给 定 的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。

  • String replaceFirst(String regex, String replacement) : 使 用 给 定 的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。

  • boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。
  • String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。

  • String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此 字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。

1.3、String类型和基本数据类型、包装类型的转换

package com.Java常用类;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class StringTest1 {
/*
  * 复习:
  * String 与基本数据类型、包装类之间的转换
  * String --> 基本数据类型、包装类:调用包装类的静态方法:parseXxx(str)
  * 基本数据类型、包装类-->String:调用String重载的valueOf(xxx)方法
  * */
  @Test
  public void test1(){
      String s1 = "123";
      //int num = (int)s1;//错误的
      int num = Integer.parseInt(s1);

      String s = String.valueOf(num);//"123"
      String s3 = num + "";
  }

  /*
  * String 与 char[]之间的转换
  * 
  * String --> char[] :调用String 的 toCharArray()
  * char[] --> String :调用String的构造器
  * */
  @Test
  public void test2(){
      String str1 = "abc123";

      char[] charArray = str1.toCharArray();
      for (int i = 0; i < charArray.length; i++) {
          System.out.println(charArray[i]);

      }

      char [] arr = new char[]{'a','b','c','d'};
      String s = new String(arr);
      System.out.println(s);
  }

  /*
  * String 和 byte[]之间的转换
  * 编码:String --> byte[]:调用String的getBytes()
  * 解码:byte[] --> String:调用String的构造器
  *
  * 编码:字符串 -->字节(看得懂--->看不懂的二进制数据)
  * 解码:编码的逆过程,字节-->字符串(看不懂的二进制数据-->看得懂)
  *
  * 说明:解码时,要求解码使用的字符集必须与编码时使用的字符集一致,否则就会出现乱码
  * */
  @Test
  public void test3() throws UnsupportedEncodingException {
      String s1 = "abc123中国";
      byte[] bytes = s1.getBytes();//使用默认的字符集,进行转换
      System.out.println(Arrays.toString(bytes));//遍历数组

      byte[] gbks = s1.getBytes("gbk");//使用gbk编码
      System.out.println(Arrays.toString(gbks));

      System.out.println("*****************");
      String s = new String(bytes);//使用默认的字符集进行解码
      System.out.println(s);

      String s2 = new String(gbks);
      System.out.println(s2);//乱码

      String gbk = new String(gbks, "gbk");//使用gbk字符集解码
      System.out.println(gbk);
  }
}

1.4、StringBuffer 和 StringBuilder的使用

package com.Java常用类;

import org.junit.Test;

public class StringBufferBuilderTest {
/*
  * String、StringBuffer和StringBuilder三者的异同
  * String:不可变的字符序列;底层结构使用char[]存储
  * StringBuffer:可变的字符序列:线程安全的,效率偏低;底层结构使用char[]存储
  * StringBuilder:可变的字符序列:线程不安全的,效率较高(jdk5.0新增);底层结构使用char[]存储
  *
  * (不涉及线程安全问题时使用 StringBuilder)
  *
  * 源码分析:
  *  String str = new String();//char []value = new char[0];
  * String str1 = new String("abc");//char []value = new char[]{'a','b','c'}
  *
  * StringBuffer sb1 = new StringBuffer();//char []value = new char[16]; 底层创建了一个长度为16的数组
  * sb1.append('a');//value[0] = 'a';
  * sb1.append('b');//value[1] = 'b';
  *
  * StringBuffer sb2 = new StringBuffer("abc");//char []value = new char["abc".length() + 16]
  *
  * //问题1.System.out.println(sb2.length());//输出的长度是多少
  * //问题2.扩容问题,如果添加的数据底层数组存不下了,那就需要扩容底层的数组
  *           默认情况下,扩容为原来容量的2倍,同时将原有数组的元素复制到新的数组中
  *
  *           指导意义:开发中建议大家使用:StringBuffer(int capacity);//建议使用带参数的构造器
   * */
  @Test
  public void test1(){
      StringBuffer s1 = new StringBuffer("abc");
      s1.setCharAt(0,'m');//无返回值,改变了s1的值
      System.out.println(s1);

      StringBuffer s2 = new StringBuffer();
      System.out.println(s2.length());//0  不是16
  }
}

1.5、StringBuffer类的常用方法

  • StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接

  • StringBuffer delete(int start,int end):删除指定位置的内容

  • StringBuffer replace(int start, int end, String str):把[start,end)位置替换为str

  • StringBuffer insert(int offset, xxx):在指定位置插入xxx

  • StringBuffer reverse() :把当前字符序列逆转

  • public int indexOf(String str)

  • public String substring(int start,int end)

  • public int length() public char charAt(int n )

  • public void setCharAt(int n ,char ch)

2、JDK8之前日期时间API

2.1、JDK8之前日期时间API

package com.Java常用类;

import org.junit.Test;

import java.util.Date;

public class DateTimeTest {
/*
* 1、System类中的currentTimeMillis()
* */
  @Test
  public void test1(){
      long time = System.currentTimeMillis();
      //返回房前时间与1970年1月1日0时0分0秒之间以毫秒为单位的事件差
      //称为时间戳
      System.out.println(time);

  }
  /*
  * 2、java.util.Date类
  *           java.sql.Date类
  *
  *   1)两个构造器的使用
  *       //构造器一:Date():创建了一个对应当前时间的Date对象
  *       //构造器二: 创建指定毫秒数的Date对象
  *   2)两个方法的使用
  *           toString();显示当前的年、月、日、时、分、秒
  *           getTiem():获取当前Date对象对应的时间戳
  *   3) java.sql.Date对应着数据库中日期类型的对象
  *           >如何实例化
  *           >如何将java.util.Date对象转换为java.sql.Date对象
  * */
  @Test
  public void test2(){
      //构造器一:Date():创建了一个对应当前时间的Date对象
      Date date1 = new Date();
      System.out.println(date1.toString());

      System.out.println(date1.getTime());//1632310401687

      //构造器二: 创建指定毫秒数的Date对象
      Date date = new Date(1632310401687L);
      System.out.println(date);

      //创建java.sql.Date对象
      java.sql.Date date2 = new java.sql.Date(123123123131L);
      System.out.println(date2);

      //如何将java.util.Date对象转换为java.sql.Date对象
      //情况一:
      Date date3 = new java.sql.Date(123456465L);
      java.sql.Date date4 = (java.sql.Date) date3;
      //情况二:
      Date date5 = new Date();
//        java.sql.Date date6 = (java.sql.Date) date5;//报错
      java.sql.Date date6 = new java.sql.Date(date5.getTime());


  }
}

2.2、SimpleDateFormat的使用

package com.Java常用类;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
* jdk 8 之前的日期时间的API测试
* 1、System类中currentTimeMillis();
* 2、java.util.Date 和子类 java.sql.Date
* 3、SimpleDateFormat
* 4、Calendar
* */
public class DateeTimeTest {
/*
* SimpleDateFromat的使用:SimpleDateFormat对日期Date类的格式化和解析
*
* 1、两个操作
*   1.1、格式化:日期--->字符串
*   2.1解析:格式化的逆过程,字符串--->日期
*
* 2、SimpleDateFormat的实例化
*
* */
  @Test
  public void test1() throws ParseException {
      //实例化SimpleDateFormat:使用默认的构造器
      SimpleDateFormat sdf = new SimpleDateFormat();

      //格式化:日期--->字符串
      Date date1 = new Date();
      System.out.println(date1);//Wed Sep 22 19:53:13 CST 2021

      String format = sdf.format(date1);
      System.out.println(format);//2021/9/22 下午7:54

      //解析:格式化的逆过程,字符串--->日期
      String str ="2021/9/22 下午7:54";
      Date parse = sdf.parse(str);
      System.out.println(parse);

      //************按指定的方式格式化和解析******************************
      SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      //格式化
      String format2 = format1.format(new Date());
      System.out.println(format2+"--------");//2021-09-22 08:01:46--------
      //解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现)
      //否则,抛异常
      Date parse1 = format1.parse(format2);
      System.out.println(parse1);

  }
}

2.3、Calendar日历类的使用

package com.Java常用类;

import org.junit.Test;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class CalendarTest {
/*
*   Calendar日历类(抽象类)的使用
* */
  @Test
  public void testCalendar(){
      //1、实例化
      //方式一:创建其子类(GregorianCalendar) 的对象
      //方式二:调用其静态方法getInstance()
      Calendar calendar = new GregorianCalendar();
//        System.out.println(calendar.getClass());
      //2、常用方法
      //get()
      int days = calendar.get(Calendar.DAY_OF_MONTH);//当前对象是这个月的第几天
      System.out.println(days);
      System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//今天是这一年的第几天

      //set()
      calendar.set(Calendar.DAY_OF_MONTH,2);//修改当前对象的值,将其修改为本月的第2天
      days = calendar.get(Calendar.DAY_OF_MONTH);
      System.out.println(days);

      //add()
      calendar.add(Calendar.DAY_OF_MONTH,2);//在本月的基础上增加或减少 x天
      days = calendar.get(Calendar.DAY_OF_MONTH);
      System.out.println(days);

      //getTime():将日历类 -->Date类
      Date time = calendar.getTime();

      //setTime():Date---> 日历类
      Date date = new Date();
      calendar.setTime(date);
      days = calendar.get(Calendar.DAY_OF_MONTH);//当前对象是这个月的第几天
      System.out.println(days);
  }
}

注意:

  • 获取月份时:一月是0,二月是1.以此类推,十二月是11
  • 获取星期时:周日是1,周二是2.......周六是7

3、JDK8中新日期时间API

package com.Java常用类;

import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

public class JDK8DateTest {


   @Test
   public void test1(){
       //偏移量
       Date date1 = new Date(2020-1900,9-1,8);
       System.out.println(date1);//Tue Sep 08 00:00:00 CST 2020

   }

   /*
   * LocalDate、LocalTime、LocalDateTime
   *
   * 说明:
   *   1、LocalDateTime相较于LocalDate、LocalTime,使用的频率要高
   *   2、类似于Calender
   * */
   @Test
   public void test2(){
       //now():获取当前的日期、时间、日期+时间
       LocalDate localDate = LocalDate.now();
       LocalTime localTime = LocalTime.now();
       LocalDateTime localDateTime = LocalDateTime.now();

       System.out.println(localDate);
       System.out.println(localTime);
       System.out.println(localDateTime);

       //of():设置指定的年、月、日、时、分、秒。没有偏移量
       LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 1,13,26,46);
       System.out.println(localDateTime1);

       //getXxx()
       System.out.println(localDateTime1.getDayOfMonth());
       System.out.println(localDateTime1.getDayOfWeek());
       System.out.println(localDateTime1.getMonth());
       System.out.println(localDateTime1.getMonthValue());
       System.out.println(localDateTime1.getMinute());

       //体现不可变性
       LocalDateTime localDateTime2 = localDateTime1.withDayOfMonth(12);
       System.out.println(localDateTime1);//2020-10-01T13:26:46
       System.out.println(localDateTime2);//2020-10-12T13:26:46
   }
}


3.2、瞬时:Instant

   @Test
   public void test3(){
       //now():获取本初子午线上的时间
       Instant instant = Instant.now();//2021-09-23T08:12:46.327701200Z
       System.out.println(instant);//本初子午线上的时间,需要加上8个小时

       //添加时间的偏移量
       OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
       System.out.println(offsetDateTime);

       //toEpochMilli():获取对应的毫秒数  (距离1970年1月1日0分0秒)
       long l = instant.toEpochMilli();
       System.out.println(l);

       //ofEpochMilli():通过给定的毫秒数获取Instant实例--->Date(long millis)
       Instant instant1 = Instant.ofEpochMilli(1632385079910L);
       System.out.println(instant1);
   }

3.3、格式化与解析日期或时间

   /*
   * DateTimeFormatter:格式化或解析日期、时间
   *   类似于SimpleDateFormat
   *
   * */
   @Test
   public void test4(){
//        方式一:预定义的标准格式。如ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
       DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
       //格式化:日期-->字符串
       LocalDateTime localDateTime = LocalDateTime.now();
       String format = formatter.format(localDateTime);
       System.out.println(localDateTime);
       System.out.println(format);

       //解析:字符串--> 日期
       TemporalAccessor parse = formatter.parse(format);
       System.out.println(parse);

//      方式二:本地化相关格式。
//        本地化相关的格式。如:ofLocallizedDateTime()
//        FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
       DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
       //格式化
       String format1 = formatter1.format(localDateTime);
       System.out.println(format1);

//        本地化相关格式。如:ofLocalizedDate()
//          FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT
       DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
       //格式化
       String format2 = formatter2.format(LocalDate.now());
       System.out.println(format2);

//    重点:方式三:自定义的格式.如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
       DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
       //格式化
       String format3 = formatter3.format(LocalDateTime.now());
       System.out.println(format3);//2021-09-23 04:40:50

       //解析
       TemporalAccessor parse1 = formatter3.parse(format3);
       System.out.println(parse1);
   }

4、比较器

4.1、方式一 自然排序

/*
*一、说明:Java中的对象,正常情况下,只能进行比较:== 或 != .不能使用> 或 <
*         但是在开发场景中,我们需要对多个对象进行排序,言外之意,就需要比较对象的大小
*         如何实现?使用两个接口中的任何一个:Comparable 或 Comparator
*
* 二、Comparable接口的使用
*
* */
public class CompareTest {

   /*
   * Comparable接口的使用举例:自然排序
   * 1、像String、包装类等实现了Compara接口,重写了compareTo(obj)方法,给出了比较两个对象大小的方式
   * 2、像String、包装类重写compareTo()方法以后,进行了从小到大的排列
   * 3、重写compareTo(obj)的规则
   *如果当前对象this大于形参obj,则返回正整数
   *如果当前对象this小于形参obj,则返回负整数
   *如果当前对象this等于形参obj,则返回 0
   *4、对于自定义类,如果需要排序,我们可以让自定义类实现Comparable接口,重写compateTo()方法
   * 在compareTo(obj)方法中指明如何排序
   * */

   @Test
   public void test1(){
       String []arr = new String[]{"aa","ff","cc","bb","gg","zz"};
       System.out.println(Arrays.toString(arr));
       Arrays.sort(arr);
       System.out.println(Arrays.toString(arr));
   }

   @Test
   public void test2(){
       Good [] arr = new Good[4];
       arr[0] = new Good("123",1.2f);
       arr[1] = new Good("123",2.2f);
       arr[2] = new Good("123",0.2f);
       arr[3] = new Good("123",11.2f);

       Arrays.sort(arr);

       System.out.println(Arrays.toString(arr));
   }
}
   /*Good类继承了Comparable接口,重写compareTo()方法指明商品比较大小的方式*/
   @Override
   public int compareTo(Object o) {
       if(o instanceof Good){
           Good good = (Good) o;
//            方式一:
           if(this.price > good.price){
               return 1;
           }else if(this.price < good.price){
               return -1;
           }else{
               return 0;
           }
           //        方式二:
//            return Double.compare(this.price,good.price);
       }
       throw new RuntimeException("传入的参数异常");
   }

4.2、定制排序

/*
* Comparator接口的使用:定制排序
* 1、背景
* 当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码
* 或者实现了java.lang.Comparable接口的排序规则不适合当前的操作
* 那么可以考虑使用Comparator的对象来排序
* */
public void test3(){
   String []arr = new String[]{"aa","ff","cc","bb","gg","zz"};
   Arrays.sort(arr,new Comparator(){

       //按照字符串从大到小的顺序排列
       @Override
       public int compare(Object o1, Object o2) {
           if(o1 instanceof  String && o2 instanceof String){
               String s1 = (String) o1;
               String s2 = (String) o2;
               return -s1.compareTo(s2);
           }
           return 0;
       }
   });
}



@Test
public void test4(){
   Good [] arr = new Good[4];
   arr[0] = new Good("123",1.2f);
   arr[1] = new Good("123",2.2f);
   arr[2] = new Good("123",0.2f);
   arr[3] = new Good("123",11.2f);

   Arrays.sort(arr,new Comparator(){

       @Override
       public int compare(Object o1, Object o2) {
           if(o1 instanceof Good && o2 instanceof Good){
               Good g1 = (Good) o1;
               Good g2 = (Good) o2;
               if (g1.getName().equals(g2.getName())){
                   return - Double.compare(g1.getPrice(),g2.getPrice());
               }else{
                   return g1.getName().compareTo(g2.getName());
               }
           }
           throw  new RuntimeException("参数异常");
       }

   });

   System.out.println(Arrays.toString(arr));
}

5、System

  • System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。 该类位于java.lang包。

  • 由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实 例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便 的进行调用。

  • 成员变量

  • System类内部包含in、out和err三个成员变量,分别代表标准输入流 (键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。

  • 成员方法

  • native long currentTimeMillis(): 该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时 间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。

  • void exit(int status): 该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表 异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。

  • void gc(): 该方法的作用是请求系统进行垃圾回收。至于系统是否立刻回收,则 取决于系统中垃圾回收算法的实现以及系统执行时的情况。

  • String getProperty(String key): 该方法的作用是获得系统中属性名为key的属性对应的值。

6、Math类

java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回 值类型一般为double型。

  • abs 绝对值
  • acos,asin,atan,cos,sin,tan 三角函数
  • sqrt 平方根
  • pow(double a,doble b) a的b次幂
  • log 自然对数
  • exp e为底指数
  • max(double a,double b)
  • min(double a,double b)
  • random() 返回0.0到1.0的随机数
  • long round(double a) double型数据a转换为long型(四舍五入)
  • toDegrees(double angrad) 弧度—>角度
  • toRadians(double angdeg) 角度—>弧度

7、BigInteger类

  • Integer类作为int的包装类,能存储的最大整型值为2 31-1,Long类也是有限的, 最大为2 63-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类 都无能为力,更不用说进行运算了。

  • java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供 所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、 位操作以及一些其他操作。

  • 构造器

  • BigInteger(String val):根据字符串构建BigInteger对象

  • 常用方法

  •  public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger。

  •  BigInteger add(BigInteger val) :返回其值为 (this + val) 的 BigInteger

  •  BigInteger subtract(BigInteger val) :返回其值为 (this - val) 的 BigInteger

  •  BigInteger multiply(BigInteger val) :返回其值为 (this * val) 的 BigInteger

  •  BigInteger divide(BigInteger val) :返回其值为 (this / val) 的 BigInteger。整数 相除只保留整数部分。

  •  BigInteger remainder(BigInteger val) :返回其值为 (this % val) 的 BigInteger。

  •  BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。

  •  BigInteger pow(int exponent) :返回其值为 (thisexponent) 的 BigInteger。

8、BigDecimal类

  • 一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中, 要求数字精度比较高,故用到java.math.BigDecimal类。

  • BigDecimal类支持不可变的、任意精度的有符号十进制定点数。

  • 构造器

  • public BigDecimal(double val)

  • public BigDecimal(String val)

  • 常用方法

  • public BigDecimal add(BigDecimal augend)

  • public BigDecimal subtract(BigDecimal subtrahend)

  • public BigDecimal multiply(BigDecimal multiplicand)

  • public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)

posted @ 2021-09-23 18:25  黯渊  阅读(161)  评论(0编辑  收藏  举报