Java核心类库
创始人
2024-05-31 22:57:41
0

Java核心类库

    • Math(☆☆☆)
    • System(☆☆☆)
    • Object(☆☆☆☆)
    • Objects (☆)
    • BigDecimal(☆☆☆☆)
    • 基本类型的包装类(☆☆☆☆☆)
    • 算法(☆☆☆☆☆)
      • 二分查找
      • 冒泡排序
      • 递归
    • Arrays(☆☆☆☆)
    • Date (☆☆☆☆☆)
    • SimpleDateFormat(☆☆☆☆☆)
    • LocalDateTime (☆)
    • Throwable 类(☆☆☆☆)
    • String(☆☆☆☆☆)
    • StringBuilder(☆☆☆)

Math(☆☆☆)

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

1.public static int abs(int a)		返回参数的绝对值  absolute 绝对的 例:int abs = Math.abs(-10);    //102.public static double ceil(double a)		向上取整  例:double ceil = Math.ceil(10.1);    //11.03.public static double floor(double a)		向下取整例:double floor = Math.floor(10.9);	 //10.04.public static int round(float a)		四舍五入例:long round = Math.round(10.1);    //10long round1 = Math.round(1.9);    //25.public static int max(int a,int b)	返回两个int值中的较大值例:int max = Math.max(10, 20);   //206.public static int min(int a,int b)	返回两个int值中的较小值例:int max = Math.max(10, 20);   //107.public static double pow(double a,double b)   返回a的b次幂的值例:double pow = Math.pow(2, 3);   //88.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;}@Overridepublic 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.7999999999999999BigDecimal 可以精准运算小数的加减乘除

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.32.public BigDecimal subtract (另一个BigDecimal对象)   //减法例:BigDecimal subtract = bd1.subtract(bd2);    //-0.13.public BigDecimal multiply (另一个BigDecimal对象)   //乘法例:BigDecimal multiply = bd1.multiply(bd2);    //0.024.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			Byteshort			Shortint			Integer(特殊)long			Longfloat			Floatdouble		Doublechar			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 --> StringString s = 100+"";String s1 = Integer.toString(10);String str = String.valueOf(10);
String --> intint a = Integer.parseInt("100"); // 这个地方传入的字符串 必须是 数字形式的字符串,否则就要运行出错了
float 和String的转换:float --> StringString 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 <= max3,计算出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.287public 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();      //2020public int getMonthValue()     获取月份(1-12)例:int month = localDateTime.getMonthValue();    //11Month month1 = localDateTime.getMonth();     //NOVEMBERpublic int getDayOfMonth()     获取月份中的第几天(1-31)例:int day = localDateTime.getDayOfMonth();   //12public int getDayOfYear()     获取一年中的第几天(1-366)int dayOfYear = localDateTime.getDayOfYear();    //317public DayOfWeek getDayOfWeek()      获取星期DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();     //THURSDAYpublic int getMinute()       获取分钟int minute = localDateTime.getMinute();        //14public int getHour()       获取小时int hour = localDateTime.getHour();        //13

转换方法:

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

格式化和解析:

LocalDateTime ---> Stringpublic String format (指定格式) 	把一个LocalDateTime格式化成为一个字符串 指定格式传DateTimeFormatter格式参数例:DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");String s = localDateTime.format(pattern);    //2021年11月12日 13:14:20String ---> LocalDateTimepublic 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:15public static DateTimeFormatter ofPattern(String s) 格式对象,以便给DateTimeFormatter传参 ()里是格式

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

1,public LocalDateTime plusYears (long years)   添加或者减去年例:LocalDateTime newLocalDateTime = localDateTime.plusYears(1);   //2022-11-12T13:14:20LocalDateTime newLocalDateTime = localDateTime.plusYears(-1);  //2020-11-12T13:14:202,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:20LocalDateTime newLocalDateTime = localDateTime.minusYears(-1);    //2022-11-12T13:14:202,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);   //P28Y11M11Dpublic int getYears()         获得这段时间的年数System.out.println(period.getYears());  //28public int getMonths()        获得此期间的月数System.out.println(period.getMonths());  //11public int getDays()          获得此期间的天数System.out.println(period.getDays());  //11public long toTotalMonths()   获取此期间的总月数System.out.println(period.toTotalMonths());  //347Period 不能获取总天数, 因为在这两时间之内  每个月份是有31天和30天 29天28天之分的。// 要想获取 时间间隔天数 请用下面的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);  //PT21H57M58Spublic long toSeconds()	       获得此时间间隔的秒System.out.println(duration.toSeconds());  //79078public long toMillis()	           获得此时间间隔的毫秒System.out.println(duration.toMillis());  //79078000public long toNanos()             获得此时间间隔的纳秒System.out.println(duration.toNanos());   //79078000000000// 获取此事件间隔的天数System.out.println(duration.toDays());	

Throwable 类(☆☆☆☆)

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

异常的体系结构:

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

RuntimeException运行期异常处理:

运行期异常的trycatch处理方式:当出现了问题之后 给这个用户一个温馨的提示,并且做到让后续的代码继续执行(程序不崩溃)try…catch处理方式标准格式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: 100System.out.println("------------");System.out.println(e.getMessage());  //100System.out.println("------------");e.printStackTrace();/*java.lang.ArrayIndexOutOfBoundsException: 100at 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");     //等同于14, 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;

相关内容

热门资讯

Python|位运算|数组|动... 目录 1、只出现一次的数字(位运算,数组) 示例 选项代...
张岱的人物生平 张岱的人物生平张岱(414年-484年),字景山,吴郡吴县(今江苏苏州)人。南朝齐大臣。祖父张敞,东...
西游西后传演员女人物 西游西后传演员女人物西游西后传演员女人物 孙悟空 六小龄童 唐僧 徐少华 ...
名人故事中贾岛作诗内容简介 名人故事中贾岛作诗内容简介有一次,贾岛骑驴闯了官道.他正琢磨着一句诗,名叫《题李凝幽居》全诗如下:闲...
和男朋友一起优秀的文案? 和男朋友一起优秀的文案?1.希望是惟一所有的人都共同享有的好处;一无所有的人,仍拥有希望。2.生活,...
戴玉手镯的好处 戴玉手镯好还是... 戴玉手镯的好处 戴玉手镯好还是碧玺好 女人戴玉?戴玉好还是碧玺好点佩戴手镯,以和田玉手镯为佳!相嫌滑...
依然什么意思? 依然什么意思?依然(汉语词语)依然,汉语词汇。拼音:yī    rán基本解释:副词,指照往常、依旧...
高尔基的散文诗 高尔基的散文诗《海燕》、《大学》、《母亲》、《童年》这些都是比较出名的一些代表作。
心在飞扬作者简介 心在飞扬作者简介心在飞扬作者简介如下。根据相关公开资料查询,心在飞扬是一位优秀的小说作者,他的小说作...
卡什坦卡的故事赏析? 卡什坦卡的故事赏析?讲了一只小狗的故事, 我也是近来才读到这篇小说. 作家对动物的拟人描写真是惟妙...
林绍涛为简艾拿绿豆糕是哪一集 林绍涛为简艾拿绿豆糕是哪一集第三十二集。 贾宽认为是阎帅间接导致刘映霞住了院,第二天上班,他按捺不...
小爱同学是女生吗小安同学什么意... 小爱同学是女生吗小安同学什么意思 小爱同学,小安同学说你是女生。小安是男的。
内分泌失调导致脸上长斑,怎么调... 内分泌失调导致脸上长斑,怎么调理内分泌失调导致脸上长斑,怎么调理先调理内分泌,去看中医吧,另外用好的...
《魔幻仙境》刺客,骑士人物属性... 《魔幻仙境》刺客,骑士人物属性加点魔幻仙境骑士2功1体质
很喜欢她,该怎么办? 很喜欢她,该怎么办?太冷静了!! 太理智了!爱情是需要冲劲的~不要考虑着考虑那~否则缘...
言情小说作家 言情小说作家我比较喜欢匪我思存的,很虐,很悲,还有梅子黄时雨,笙离,叶萱,还有安宁的《温暖的玄》 小...
两个以名人的名字命名的风景名胜... 两个以名人的名字命名的风景名胜?快太白楼,李白。尚志公园,赵尚志。
幼儿教育的代表人物及其著作 幼儿教育的代表人物及其著作卡尔威特的《卡尔威特的教育》,小卡尔威特,他儿子成了天才后写的《小卡尔威特...
海贼王中为什么说路飞打凯多靠霸... 海贼王中为什么说路飞打凯多靠霸气升级?凯多是靠霸气升级吗?因为之前刚到时确实打不过人家因为路飞的实力...
运气不好拜财神有用吗运气不好拜... 运气不好拜财神有用吗运气不好拜财神有没有用1、运气不好拜财神有用。2、拜财神上香前先点蜡烛,照亮人神...