Java核心类库

Math(☆☆☆)

Math的这些方法 都是静态的。 Math的构造方法私有的。 Math是不能创建对象的。

1.public static int abs(int a)		返回参数的绝对值  absolute 绝对的 
    例:int abs = Math.abs(-10);    //10

2.public static double ceil(double a)		向上取整  
    例:double ceil = Math.ceil(10.1);    //11.0

3.public static double floor(double a)		向下取整
	例:double floor = Math.floor(10.9);	 //10.0

4.public static int round(float a)		四舍五入
    例:long round = Math.round(10.1);    //10
	   long round1 = Math.round(1.9);    //2

5.public static int max(int a,int b)	返回两个int值中的较大值
    例:int max = Math.max(10, 20);   //20

6.public static int min(int a,int b)	返回两个int值中的较小值
    例:int max = Math.max(10, 20);   //10

7.public static double pow(double a,double b)   返回a的b次幂的值
	例:double pow = Math.pow(2, 3);   //8

8.public static double random()		返回值为double的正值,[0.0,1.0):double a = Math.Random();   //随机一个0.0到1.0之间的小数 
      int b  = (int)(Math.random()*13)     //[0,13)                                           

System(☆☆☆)

System的这些方法 都是静态的。 System的构造方法私有的。 System是不能创建对象的。

1.public static void exit(int status)	终止当前运行的 Java 虚拟机,非零表示异常终止 
    
2.public static long currentTimeMillis()   返回的是 当前时间 减去1970-1-1 8:00:00的时间差,所换算成的毫秒值。
    
3.arraycopy(Object src , int formIndex , Object dest ,int formIndex1 ,int len );   //arraycopy(数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数)

Object(☆☆☆☆)

toString : 类重写了toString, 打印语句就打印toString的内容
	public class Student /*extends Object*/
    	private String name;
        private int age;
        public Student(String name, int age) {
        	this.name = name;
            this.age = age;
        }
        @Override           //重写toString方法
        public String toString() {
        	return "Student{" +
            	"name='" + name + '\'' +
                ", age=" + age +
                 '}';
        }
	}
	public class Demo {
    	public static void main(String[] args) {
        	Student s = new Student("张三",23);
            System.out.println(s); //内容
            System.out.println(s.toString()); //内容
        }
	}
equals(); 
	Object 的equals方法是比较的地址。
	要想让自己定义的类的对象比较内容,就必须重写equals方法 来比较内容:
	public class Student {
		private int age;
		private int score;
		public Student(int age , int score){
		this.age = age;
		this.score = score;
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || this.getClass() != o.getClass()) return false;
			
			Student student = (Student) o;

			if (age != student.age) return false;
			return score == student.score;
	}

Objects (☆)

Object 类的 工具类

工具类 :private 私有构造, 里面全部都是 静态方法。

1.public static String toString(对象)  		 返回参数中对象的字符串表示形式。	

2.public static String toString(对象, 默认字符串)  返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.

3.public static Boolean isNull(对象)		 判断对象是否为空

4.public static Boolean nonNull(对象)		 判断对象是否不为空

BigDecimal(☆☆☆☆)

 System.out.println(0.7+0.1); 	 //0.7999999999999999
 BigDecimal 可以精准运算小数的加减乘除

BigDecimal的两种构造方法:

1.BigDecimal bd = new BigDecimal(double d);       //   不可以精准运算 :  BigDecimal bd3 = new BigDecimal(1.3);
        BigDecimal bd4 = new BigDecimal(0.5);

2.BigDecimal bd = new BigDecimal(String s);       //   可以精准运算: BigDecimal bd1 = new BigDecimal("0.1");
       BigDecimal bd2 = new BigDecimal("0.2");

成员方法:

1.public BigDecimal add(另一个BigDecimal对象)       //加法: BigDecimal add = bd1.add(bd2);      //0.3

2.public BigDecimal subtract (另一个BigDecimal对象)   //减法:BigDecimal subtract = bd1.subtract(bd2);    //-0.1

3.public BigDecimal multiply (另一个BigDecimal对象)   //乘法:BigDecimal multiply = bd1.multiply(bd2);    //0.02

4.public BigDecimal divide (另一个BigDecimal对象)    //除法:BigDecimal divide = bd1.divide(bd2);      //0.5
5.public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)     //除法:BigDecimal divide = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);   //bd1除以bd2,保留2位小数,四舍五入
舍入模式:    
     //进一法  BigDecimal.ROUND_UP    已过时
     //去尾法  BigDecimal.ROUND_FLOOR     已过时
     //四舍五入 BigDecimal.ROUND_HALF_UP     已过时

基本类型的包装类(☆☆☆☆☆)

8种基本类型的包装类:(int和char特殊记,剩余首字母大写)
	  byte			Byte
      short			Short
      int			Integer(特殊)
      long			Long
      float			Float
      double		Double
      char			Character(特殊)
      Boolean		Boolean

构造方法

Integer in = new Integer(int num);   // (已过时)in 就是100
Integer in1 = new Integer(String num);  // (已过时)in1 就是100
Integer in2 =Integer.valueOf(int num);
Integer in3 =Integer.valueOf(String num); 

自动装箱

Integer inte = 10;    //自动装箱

int i = 20;
Integer inte2 = i;     //自动装箱

自动拆箱

Integer in = new Integer("10"); 
int a = in;       //自动拆箱
Integer in = new Integer(10);
int a = in + 10;      //只涉及到了拆箱
Integer in1 = in +10;     // 自动装箱和自动拆箱

基本类型和String之间的转换

int --> String
	String s = 100+"";
	String s1 = Integer.toString(10);
	String str = String.valueOf(10);
String --> int
	int a = Integer.parseInt("100"); // 这个地方传入的字符串 必须是 数字形式的字符串,否则就要运行出错了
floatString的转换:
	float --> String
		String s = 100.5f +"";
		String s1 = Float.toString(100.5f);
		String str = String.valueOf(100.5f);
		String --> float 
		float f = Float.parseFloat("100.6");
		其他的也相同.... //byte b = Byte.parseByte("126"); 
注意:Character 里面没有 parseChar的。。。。

算法(☆☆☆☆☆)

二分查找

前提条件:
	数组内的元素一定要按照大小顺序排列,如果没有大小顺序,是不能使用二分查找法的

二分查找的实现步骤:
    1,定义两个变量,表示要查找的范围。默认min = 0 , max = 最大索引
    2,循环查找,但是min <= max
    3,计算出mid的值
    4,判断mid位置的元素是否为要查找的元素,如果是直接返回对应索引
    5,如果要查找的值在mid的左半边,那么min值不变,max = mid -1.继续下次循环查找
    6,如果要查找的值在mid的右半边,那么max值不变,min = mid + 1.继续下次循环查找
    7,当 min > max 时,表示要查找的元素在数组中不存在,返回-1.

冒泡排序

概述:
	1,可以实现数组内容按顺序排序
    2,一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,依次对所有的数据进行操作,直至所有数据按要求完成排序
    
如果有n个数据进行排序,总共需要比较n-1次

每一次比较完毕,下一次的比较就会少一个数据参与

递归

什么是递归
	递归指的是方法本身自己调用自己
	
递归解决问题的思路
    把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解
    递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算
    
递归的注意事项
     1,递归必须要有出口
     2,递归每次的参数传递 要去靠近出口
     3,递归的过程就是 把问题逐渐缩小化的过程,最终涌向出口
     4,递归的次数不宜过多,否则都会造成内存溢出,大概最大到 18000次左右

Arrays(☆☆☆☆)

Arrays 类的 工具类

工具类 :private 私有构造, 里面全部都是 静态方法。

1,public static String toString(int[] a)     返回指定数组的内容的字符串表示形式
	例:int [] arr = {3,2,4,6,7};
	   Arrays.toString(arr)          //[3, 2, 4, 6, 7]
           
2,public static void sort(int[] a)         按照数字顺序排列指定的数组
	   Arrays.sort(arr);             //{2,3,4,6,7}

3,public static int binarySearch(int[] a, int key)       利用二分查找返回指定元素的索引
		//1,数组必须有序
		//2.如果要查找的元素存在,那么返回的是这个元素实际的索引
		//3.如果要查找的元素不存在,那么返回的是 (-插入点-1)
		//插入点:如果这个元素在数组中,他应该在哪个索引上

Date (☆☆☆☆☆)

Date类概述

Date代表了一个特定的时间类,精确到毫秒值

构造方法

Date d = new Date();           以当前系统时间创建对象
Date d = new Date(long l)      以指定毫秒值时间创建对象,(毫秒值)

成员方法:

Date d = new Date(); 
1,long time = d.getTime();      //获取时间毫秒值  time是 d这个时间的毫秒值

2,d.setTime(long l)       根据传入的毫秒值设置时间

SimpleDateFormat(☆☆☆☆☆)

日期格式化类

构造方法:

SimpleDateFormat sdf = new SimpleDateFormat("日期模板格式");   
	//常用的日期模板格式
	  "yyyy年MM月dd日 HH:mm:ss"
	  "yyyy-MM-dd HH:mm:ss"

格式化 Date —> String

String s = sdf.format(Date d)      将日期对象格式化成字符串
    例:Date d = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String str = sdf.format(d);   //2000-01-01 10:10:10

解析 String—> Date

Date d = sdf.parse(String s);     将字符串日期解析成Date对象
	例:String str = "2020-1-1";
	   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	   Date d = sdf.parse(str);   //Wed Jan 01 00:00:00 CST 2020

LocalDateTime (☆)

jdk8 后加的表示时间的类

构造方法:

public static LocalDateTime now()        获取当前系统时间
    例:LocalDateTime now = LocalDateTime.now();        //2020-01-01T10:40:37.287

public static LocalDateTime of(,,,,,)    使用指定年月日和时分秒初始化一个LocalDateTime对象
    例:LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);   //2020-11-11T11:11:11

获取方法

public int getYear()      获取年
	例:LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 12, 13, 14, 20);
	   int year = localDateTime.getYear();      //2020
	   
public int getMonthValue()     获取月份(1-12)
	例:int month = localDateTime.getMonthValue();    //11
	   Month month1 = localDateTime.getMonth();     //NOVEMBER
	
public int getDayOfMonth()     获取月份中的第几天(1-31)
	例:int day = localDateTime.getDayOfMonth();   //12

public int getDayOfYear()     获取一年中的第几天(1-366int dayOfYear = localDateTime.getDayOfYear();    //317

public DayOfWeek getDayOfWeek()      获取星期
    DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();     //THURSDAY

public int getMinute()       获取分钟
    int minute = localDateTime.getMinute();        //14

public int getHour()       获取小时
    int hour = localDateTime.getHour();        //13

转换方法:

public LocalDate toLocalDate ()       转换成为一个LocalDate对象,只表示 年月日
	例:LocalDate localDate = localDateTime.toLocalDate();      //2020-11-12
	
public LocalTime toLocalTime ()       转换成为一个LocalTime对象,只表示 时分秒
	例:LocalTime localTime = localDateTime.toLocalTime();      //13:14:20

格式化和解析:

LocalDateTime ---> String
	public String format (指定格式) 	把一个LocalDateTime格式化成为一个字符串 指定格式传DateTimeFormatter格式参数
	例:
	DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
	String s = localDateTime.format(pattern);    //2021年11月12日 13:14:20
	
String ---> LocalDateTime
	public LocalDateTime parse (准备解析的字符串, 解析格式)		把一个日期字符串解析成为一个LocalDateTime对象
	例:
	String s = "2020年11月12日 13:14:15";
	DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
	LocalDateTime parse = LocalDateTime.parse(s, pattern);  //2020-11-12T13:14:15
	
	
	public static DateTimeFormatter ofPattern(String s) 格式对象,以便给DateTimeFormatter传参 ()里是格式

增加或者减少时间的方法(plus):

1,public LocalDateTime plusYears (long years)   添加或者减去年
     例:LocalDateTime newLocalDateTime = localDateTime.plusYears(1);   //2022-11-12T13:14:20
		LocalDateTime newLocalDateTime = localDateTime.plusYears(-1);  //2020-11-12T13:14:20

2,public LocalDateTime plusMonths(long months)  添加或者减去月

3,public LocalDateTime plusDays(long days)  添加或者减去日

4,public LocalDateTime plusHours(long hours)  添加或者减去时

5,public LocalDateTime plusMinutes(long minutes)  添加或者减去分

6,public LocalDateTime plusSeconds(long seconds)  添加或者减去秒

7,public LocalDateTime plusWeeks(long weeks)  添加或者减去周

减少或者增加时间的方法(minus):

1,public LocalDateTime minusYears (long years)   减去或者添加年
	例:LocalDateTime newLocalDateTime = localDateTime.minusYears(1);    //2020-11-12T13:14:20
	   LocalDateTime newLocalDateTime = localDateTime.minusYears(-1);    //2022-11-12T13:14:20
	   
2,public LocalDateTime  minusYears (long years)   减去或者添加年

3,public LocalDateTime  minusMonths(long months)   减去或者添加月

4,public LocalDateTime minusDays(long days)   减去或者添加日

5,public LocalDateTime minusHours(long hours)   减去或者添加时

6,public LocalDateTime minusMinutes(long minutes)  减去或者添加分

7,public LocalDateTime minusSeconds(long seconds)  减去或者添加秒

8,public LocalDateTime minusWeeks(long weeks)   减去或者添加周	   

修改方法(with):

1,public LocalDateTime withYear(int year)       直接修改年
    
2,public LocalDateTime withMonth(int month)      直接修改月
    
3,public LocalDateTime withDayOfMonth(int dayofmonth)      直接修改日期(一个月中的第几天)
    
4,public LocalDateTime withDayOfYear(int dayOfYear)     直接修改日期(一年中的第几天)  
    
5,public LocalDateTime withHour(int hour)     直接修改小时
    
6,public LocalDateTime withMinute(int minute)      直接修改分钟
    
7,public LocalDateTime withSecond(int second)      直接修改秒

时间间隔(Period :年月日)

public static Period between(开始时间,结束时间)  计算两个"时间"的间隔,传入LocalDate参数对象
	LocalDate localDate1 = LocalDate.of(2020, 1, 1);
	LocalDate localDate2 = LocalDate.of(2048, 12, 12);
	Period period = Period.between(localDate1, localDate2);
	System.out.println(period);   //P28Y11M11D

public int getYears()         获得这段时间的年数
	System.out.println(period.getYears());  //28

public int getMonths()        获得此期间的月数
	System.out.println(period.getMonths());  //11

public int getDays()          获得此期间的天数
	System.out.println(period.getDays());  //11

public long toTotalMonths()   获取此期间的总月数
	System.out.println(period.toTotalMonths());  //347

 Period 不能获取总天数, 因为在这两时间之内  每个月份是有31天和302928天之分的。
	// 要想获取 时间间隔天数 请用下面的Duration

时间间隔(Duration: 年月日时分秒)

public static Duration between(开始时间,结束时间)  计算两个“时间"的间隔
	LocalDateTime localDateTime1 = LocalDateTime.of(2020, 1, 1, 13, 14, 15);
	LocalDateTime localDateTime2 = LocalDateTime.of(2020, 1, 2, 11, 12, 13);
	Duration duration = Duration.between(localDateTime1, localDateTime2);
	System.out.println(duration);  //PT21H57M58S
	
public long toSeconds()	       获得此时间间隔的秒
	System.out.println(duration.toSeconds());  //79078
	
public long toMillis()	           获得此时间间隔的毫秒
	System.out.println(duration.toMillis());  //79078000
	
public long toNanos()             获得此时间间隔的纳秒
	System.out.println(duration.toNanos());   //79078000000000
		// 获取此事件间隔的天数
	System.out.println(duration.toDays());	

Throwable 类(☆☆☆☆)

异常的概述 :  指的是程序出现了不正常的情况

异常的体系结构:

Throwable 
	Error(问题严重,不需要处理)
	Exception(异常类,程序本身可以处理的问题)
		RuntimeException(运行时异常,编译的时候不报错)
		非RuntimeException(编译期异常,编译的时候就会报出红线,目的是为了让你检查程序,用throws抛出即可)

RuntimeException运行期异常处理:

运行期异常的trycatch处理方式:
	当出现了问题之后 给这个用户一个温馨的提示,并且做到让后续的代码继续执行(程序不崩溃)
	trycatch处理方式标准格式
   	 try{
    	可能会出现异常的代码
     }catch(异常类型 对象名) {
        处理方式
     }
Throwable的三个方法:	
     try {
       /*String s = null;
       boolean aa = s.equals("aa");*/
       int[] arr = {1};
       System.out.println(arr[100]);   // 向外扔出一个异常  new ArrayIndexOutOfBoundsException(100+"");
     } catch (Exception e) {
        System.out.println(e.toString()); //java.lang.ArrayIndexOutOfBoundsException: 100
        System.out.println("------------");
        System.out.println(e.getMessage());  //100
        System.out.println("------------");
        e.printStackTrace();
        /*
        java.lang.ArrayIndexOutOfBoundsException: 100
	    at com.suihao.Demo10.main(Demo10.java:9)
        */
      }

运行期异常的Throws处理方式:
	没有任何用处。  Throws的处理方式 并不是针对运行期异常而产生的。
	因为 运行期异常 不管你throws 还是不throws  都是一摸一样的。 没有任何差别。

String(☆☆☆☆☆)

构造方法

1, String s = "abc";         //直接赋值

2, String s = new String();       //等同于String s1 = "";  

3, String s = new String("abc");     //等同于1

4, String s = new String(char[] chars);     //传入char数组

5 ,String s = new String(char[] chars,int index,int count);	//根据传入的字符数组和指定范围个数来创建一个字符串对象

5, String s = new String(byte[] bytes)      //传入byte数组

6, String s = new String(byte[] bytes,int index,int count);  //根据传入的字节数组和指定范围个数来创建一个字符串对象
    
7,  String s = new String(StringBuilder builder)      //传入StringBuilder对象

成员方法

获取功能:

1, int length();		 获取字符串的长度

2, String concat(String str);		 拼接字符串,并返回新字符串

3, char charAt(int index);		 获取指定索引处的字符

4, int indexOf(String str);		 获取传入的字符串在整个字符串中第一次出现的索引位置

5, int lastIndexOf(String str);		 获取传入的字符串在整个字符串中最后一次出现的索引位置

6, String substring(int index);		 从指定索引处开始截取,默认到结尾

7, String substring(int start,int end);	    	截取指定索引范围的字符串。(包含开始索引、不包含结束索引)
    
8, static String valueOf (参数);  可传: (int i) (long l) (float f) (double d) (char c) (boolean b) (char[]ch)
                                     // 返回 各自 参数的字符串表示形式。
判断功能:

1, boolean equals(String str);				比较两个字符串内容是否相同,区分大小写
    
2, boolean equalsIgnoreCase(String str);	比较两个字符串内容是否相同,不区分大小写
    
3, boolean startsWith(String str);			判断整个字符串是否以传入的字符串为开头
    
4, boolean endsWith(String str);			判断整个字符串是否以传入的字符串为结尾
    
5, boolean contains(String str);			判断整个字符串中是否包含传入的字符串
    
6, boolean isEmpty();						判断字符串是否为空
转换功能:

1, char[] toCharArray();					将字符串转成字符数组

2, byte[] getBytes();						将字符串转成字节数组

3, String replace(String oldS,String newS);用新字符串替换老字符串

4, String toUpperCase();					将字符串转成大写并返回

5, String toLowerCase();					将字符串转成小写并返回
其他功能:

1, String[] split(String regex);			根据指定规则进行切割字符串

2, String trim();							去掉字符串两端的空白

StringBuilder(☆☆☆)

作用: 主要是为了用来拼接字符串的

构造方法:

StringBuilder()  创建一个内容为空的字符串缓冲区对象  

StringBuilder(String str)  根据字符串的内容来创建字符串缓冲区对象

成员方法:

StringBuilder append(任意类型);  向缓冲区中追加数据  

StringBuilder reverse();  将缓冲区的内容反转  

String toString();  将缓冲区的内容转成字符串  

int length();  获取缓冲区的长度

两者转换:

String s = "abc123";
StringBulider sb = new StringBulider(s);

String s1 = sb.toString;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江東-H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值