1.Math

此类中封装了一些数学运算的API 。
public static double abs(double a) //求绝对值
public static double ceil(double a) //向上取整
public static double floor(double a) //向下取整
public static double max(double a,double b) //二者取大
public static double min(double a,double b) //二者取小
public static double random() //获取 [0,1) 随机数
public static long round(double a) //四舍五入
public static double sqrt(double a) // 开平方根
public static double pow(double a, double b) // 求次方
生成一个随机数 [1—-100]
public class TestMath {
public static void main(String[] args) {

double random = Math.random();
System.out.println(random);
double d = random * 100;

System.out.println(Math.ceil(d));
System.out.println(Math.floor(d));
System.out.println(Math.round(d));
// 1—100
int num = (int)(d + 1);
System.out.println(num);

double pow = Math.pow(2, 3);
System.out.println(pow);

System.out.println(Math.sqrt(16));

System.out.println(“==============”);

//求出1—100 所有的质数
System.out.println(“2 是质数”);
for(int i=3 ;i<=97;i++){

boolean f = true ;
for(int j=2;j<= Math.sqrt(i);j++){
if(i % j == 0){
f = false ;
break ;
}
}

if(f) {
System.out.println(i +”是质数”);
}

}
}
}

2 .System

System类提供的System包括标准输入,标准输出和错误输出流; 访问外部定义的属性和环境变量; 一种加载文件和库的方法; 以及用于快速复制阵列的一部分的实用方法
属性:
API - 图1
方法 :
/*
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目的地数据中的起始位置。
length - 要复制的数组元素的数量。
/
public static void arraycopy(Object src,int srcPos,int destPos,int length)
public static long currentTimeMillis() //获取从1970年1月1日 开始 到现在的 毫秒数
public static void exit(int status) //终止虚拟机执行
public class TestSystem {
public static void main(String[] args) {
System.out.println(“AAAAAAAAAA”);
System.err.println(“CCCCCCCCCCCCCCCCC”);
/Scanner sc = new Scanner(System.in);
String s = sc.next();
System.out.println(s);
/

System.out.println(System.getProperty(“user.country”));

Properties pros = System.getProperties();
Set keySet = pros.keySet();
for(Object o :keySet){
System.out.println(o);
}
}
}

3.UUID

  • 一个表示不可变的通用唯一标识符(UUID)的类。 UUID表示128位值。

public static UUID randomUUID() //获取一个唯一的字符串 32位
public class TestUUID {
public static void main(String[] args) {

UUID uuid = UUID.randomUUID();
String s = uuid.toString();
System.out.println(s);

System.out.println(s.replace(“-“, “”));
}
}

4.Calender

日历 操作日期的对象 ,此类是一个抽象类 ,不能直接创建对象

  1. 构造方法 :

API - 图2

  1. 属性省略。。。
  2. 方法 public static Calendar getInstance() //返回一个Calendar对象
    public int get(int field) //根据属性 获取属性值public class TestCalendar {
    public static void main(String[] args) {
    Calendar c = Calendar.getInstance();
    int i = c.get(Calendar.WEEK_OF_YEAR); //获取日期中的属性值
    System.out.println(i);
    }
    }

    5.包装类

    Java中有八大基本数据类型,Java提供了针对八大基本数据类型的包装类(引用类型)
    byte ———- Byte
    short ———- Short
    int ———- Integer
    long ———- Long
    float ———- Float
    double ———- Double
    char ———- Character
    boolean ———- Boolean
    下面以Integer 和 Double 为例 :

  3. Integer 属性 :public static final int MAX_VALUE
    public static final int MIN_VALUE构造方法 :API - 图3public class TestInteger {
    public static void main(String[] args) {

    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.MIN_VALUE);

    Integer i1 = new Integer(100);
    Integer i2 = new Integer(“123456”);

    //Integer i3 = new Integer(“abc”); java.lang.NumberFormatException
    }
    }API方法 :public static int compare(int x,int y)
    public int compareTo(Integer anotherInteger) //放在比较器对象中讲
    public double doubleValue()
    public static Integer getInteger(String nm)
    public int intValue()
    public long longValue()
    public static int max(int a,int b)
    public static int min(int a,int b)
    public static int parseInt(String s) //把字符串类型 转成 int类型
    public static String toBinaryString(int i)
    public static String toHexString(int i)
    public static String toOctalString(int i)
    public static Integer valueOf(int i)
    public static Integer valueOf(String s)public class TestInteger {
    public static void main(String[] args) {

    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.MIN_VALUE);

    Integer i1 = new Integer(100);
    Integer i2 = new Integer(“123456”);

    //Integer i3 = new Integer(“abc”); java.lang.NumberFormatException

    String s1 = “123”;
    int i3 = 123;

    int i4 = Integer.parseInt(s1); //*
    int num = i3 + i4 ;
    System.out.println(num);


    int x = 16 ;
    String s2 = Integer.toBinaryString(x);
    String s3 = Integer.toOctalString(x);
    String s4 = Integer.toHexString(x);

    System.out.println(s2 + “==”+s3 +”===”+s4);
    }
    }Double :方法 :public boolean isNaN() //判断对象 是不是非数字类型
    public static boolean isNaN(double v)

JDK1.5之后 ,基本类型和 包装类型 ,可以自动转换的 。
练习 :操作一下 long 类型中的方法

6. BigDecaimal 、BigInteger

public class TestDoubleCal {
public static void main(String[] args) {
double d = 0.0 ;
for(int i=0;i<10;i++){
d += 0.1;
}
System.out.println(d);
}
}
// 0.9999999999999999 从执行 可以看出 ,double 在参与计算的时候 不是一个精确值
如果是做金融项目 ,涉及到钱的问题,不能使用double进行计算
在精确运算的时候 使用 BigDecimal进行计算
一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故用到java.math.BigDecimal类,BigDecimal类支持不可变的、任意精度的有符号十进制定点数

  1. 构造方法public BigDecimal(double val) // 不推荐使用 计算还是不精确的
    public BigDecimal(String val) // 推荐使用 精确的
  2. 方法API注意:当如除法计算时,除不尽,咱们需要设定小数点的位数,以及舍去模式ROUND_DOWN 去尾模式(直接去掉后面的数据)ROUND_UP 丢失后面的数据时,如果下一位不是0,就向前进1ROUND_HALF_UP 四舍五入public class TestDoubleCal {
    public static void main(String[] args) {
    / double d = 0.0 ;
    for(int i=0;i<10;i++){
    d += 0.1;
    }
    System.out.println(d);
    /

    BigDecimal d1 = new BigDecimal(“0.0”);
    BigDecimal d = new BigDecimal(“0.1”);

    for(int i=0;i<10;i++){
    d1 = d1.add(d);
    }

    System.out.println(d1.toString());

    BigDecimal d2 = new BigDecimal(“10”);
    BigDecimal d3 = new BigDecimal(“3.0”);

    BigDecimal d4 = d2.divide(d3, 3,BigDecimal.ROUND_DOWN);
    BigDecimal d5 = d2.divide(d3, 3,BigDecimal.ROUND_UP);
    BigDecimal d6 = d2.divide(d3, 3,BigDecimal.ROUND_HALF_UP);

    System.out.println(d4 + “—“ +d5 + “===”+d6);

    BigDecimal dd = new BigDecimal(“1.34236727688”);
    dd = dd.setScale(4, BigDecimal.ROUND_HALF_UP);
    System.out.println(dd);
    }
    } | 方法名 | 意义 | | —- | —- | | public BigDecimal add(BigDecimal augend) | 加 | | public BigDecimal subtract(BigDecimal subtrahend) | 减 | | public BigDecimal multiply(BigDecimal multiplicand) | 乘 | | public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) | 除 | | public BigDecimal setScale(int newScale, int roundingMode) | 设置精度 |

  3. BigIntegerInteger类作为int的包装类,能存储的最大整型值为231-1,Long类也是有限的,最大为263-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类都无能为力,更不用说进行运算了java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger 提供所有Java 的基本整数操作符的对应物,并提供java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作BigInteger(String val):根据字符串构建BigInteger对象

    1. 构造器
    2. API 方法 | 方法名 | 意义 | | —- | —- | | 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 |

    public class TestBigIntetger {
    public static void main(String[] args) {

    BigInteger x = new BigInteger(“100”);
    BigInteger y = new BigInteger(“30”);

    BigInteger[] arr = x.divideAndRemainder(y);

    for(BigInteger i :arr){
    System.out.println(i);
    }
    }
    }

    7.String

  4. String类:代表字符串。Java 程序中的所有字符串字面值(如”abc” )都作为此类的实例实现

  5. String是一个final类,代表不可变的字符序列
  6. 字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改
  7. 字符串在虚拟机中存储是一个char类型的数组 API - 图4

字符串是一个常量 ,因为String是使用 final修饰的,在JDK1.8的虚拟机中,存储在堆区的

  1. 构造方法#### public class TestString {
    public static void main(String[] args) {
    String s1 = “abc”;
    String s2 = “abc”;
    String s3 = “abc”;
    /
    s1和s2 这两个引用指向的是内存的同一个地址
    字符串直接复制的时候 ,他会在内存中查找 有没有想到的字符串
    如果有 就把地址直接赋给 引用 可以多个引用指向同一个地址
    /
    System.out.println(s1 == s2); //true

    /

    如果只要使用 new 关键词 就会在内存开辟新的空间
    /
    String s4 = new String(“abc”);
    String s5 = new String(“abc”);
    System.out.println(s5 == s4); // false

    String s = “蜗牛学院”;
    byte[] bytes = s.getBytes();

    String s6 = new String(bytes,0,9);
    System.out.println(s6);
    char[] cs = {‘a’,’r’,’y’};
    String s7 = new String(cs);
    System.out.println(s7);
    }
    } | 构造方法 | 意义 | | —- | —- | | public String() | 创建一个空字符串序列 | | public String(byte[] bytes) | 利用一个字节数组创建一个字符串 | | public String(byte[] bytes, int offset, int length) | 利用字节数组,截取一段字节,创建字符串 | | public String(char[] value) | 利用字符数组创建字符串 | | public String(char[] value, int offset, int count) | 利用字符数组,截取一部分创建字符串 | | public String(String original) | 利用字符串创建字符串 | | public String(StringBuffer buffer) | 利用StringBuffer创建字符串 | | public String(StringBuilder builder) | 利用StringBuilder创建字符 |

  2. API方法 | 方法 | 意义 | | —- | —- | | int length() | 返回字符串的长度 | | public char charAt(int index) | 获取下标对应的字符 | | public String toUpperCase() | 把小写换成大写 | | public String toLowerCase() | 把大写换成小写 | | public int compareTo(String anotherString) | 比较两个字符串的大小 返回值:1.返回正数: 前面大于后面 2. 返回 0 ,两个字符串相等 3,返回负数 前面的小于后面的,按照asc码比较 | | public int compareToIgnoreCase(String str) | 比较两个字符串的大小 (不区分大小写) 返回值:1.返回正数: 前面大于后面 2. 返回 0 ,两个字符串相等 3,返回负数 前面的小于后面的,按照asc码比较 | | public String concat(String str) | 字符串的拼接 | | public boolean contains(CharSequence s) | 判断字符串中是否包含某一个字符后者字符串 | | public boolean endsWith(String suffix) | 判断字符串是否以 传的参数 为结尾 | | public boolean startsWith(String prefix) | 判断是否以 参数 开始的字符串 | | public static String copyValueOf(char[] data) | 通过一个字符数组创建一个字符串 | | public boolean equals(Object anObject) | 将此字符串与指定对象进行比较值 | | public boolean equalsIgnoreCase(String anotherString) | 将此字符串与指定对象进行比较值,忽略大小写 | | public byte[] getBytes() | 使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中 | | public int indexOf(int ch) | 返回指定字符第一次出现的字符串内的索引 | | public int indexOf(int ch, int fromIndex) | 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。 | | public int lastIndexOf(int ch) | 返回指定字符的最后一次出现的字符串中的索引 | | public int indexOf(String str) | | | public int indexOf(String str, int fromIndex) | | | public String intern() | 返回字符串对象的规范表示 ,把对象从堆区整到方法区 | | public boolean isEmpty() | length()是 0 返回值就是 true 否则就是false “ ” | | public String trim() | 去电字符串两端的空格 | | public boolean matches(String regex) | 字符串和正则进行比较 | | public String replace(char oldChar, char newChar) | 替换 | | public String replace(CharSequence target, CharSequence replacement) | 替换 | | public String replaceAll(String regex, String replacement) | 替换 | | public String[] split(String regex) | 把字符串分割成 字符串数组 | | public String substring(int beginIndex) | 从beginIndex开始截取字符串 (包含beginIndex下标) | | public String substring(int beginIndex, int endIndex) | 返回一个字符串,该字符串是此字符串的子字符串。 子串开始于指定beginIndex并延伸到字符索引endIndex - 1 ,包前不包后 [ ) | | public char[] toCharArray() | 把字符串转成字符数组 |

8.StringBuider

一个可变的字符序列 ,可以改变的字符串。
在个类中提供了一个方法 append(String str)提供对字符串的改变

  1. 构造方法 API - 图5
  2. API方法public StringBuilder append(boolean b) //有很多重载方法 添加的参数类型不同
    public StringBuilder delete(int start,int end) //删除字符串中的一部分字符串
    public StringBuilder deleteCharAt(int index) //删除指定的字符
    public StringBuilder reverse() //翻转字符串

    9.StringBuffer

    用法和StringBuilder是完全一样的,没有任何区别,方法也一样
    区别 :
    StringBuider :是线程不安全的 ,执行效率高 (推荐使用)
    @Override
    public StringBuilder append(String str) {
    super.append(str);
    return this;
    }
    StringBuffer : 线程安全的,执行效率低
    @Override
    public synchronized StringBuffer append(String str) {
    toStringCache = null;
    super.append(str);
    return this;
    }
    面试题 :String、StringBuider、StringBuffer三个类之间的区别?

10.Arrays

数组的工具类 :包含数组的一些工具方法 (排序和查找)
API 方法 :
public static List asList(T… a) //把参数的值 封装在List集合中
public static int binarySearch(byte[] a, byte key) // 二分查找 数组必须是 已经排序的 *
public static int[] copyOf(int[] original,int newLength) //数组的拷贝
public static int[] copyOfRange(int[] original,int from,int to) //根据下标 赋值数组的一部分 *
public static void fill(int[] a, int val) //使用 val 的值 给 a 数组赋值
public static void sort(int[] a) //数组的排序 *
public class TestArrays {
public static void main(String[] args) {


int[] nums = {2,3,4,5,6,64,2,21,23,445,5,667,67} ;
Arrays.sort(nums);
for(int i:nums){
System.out.println(i);
}

int index = Arrays.binarySearch(nums, 64);
System.out.println(index);

System.out.println(“==========”);

int[] arr = Arrays.copyOfRange(nums, 5,10);
for(int i :arr){
System.out.println(i);
}
}
}

11.多参数使用

public class TestMultiParams {
public static void main(String[] args) {
printNames(100,”jim”,”tom”,”jack”,”lucy”);
}
/*
1,多参数的使用 实际上 参数是一个数组
2,多参数的使用 ,就是因为不知道将来要传多少个参数 所以使用多参数组
3,多参数的使用 必须是 在方法的最后一个参数上
@param name
/
static void printNames(int num ,String …names){
System.out.println(100);
for(String m :names){
System.out.println(m);
}
}
}