javaSE总结笔记-6

简单概述

此文章是自己进行java学习的一个总结内容, 可能会有多篇内容.

API

定义

API是Application Program Interface的缩写, 其表示为应用程序的编程接口, 实际是一套固定的编程规则方法, 也就是别人将相关的方法与类封装好, 我们直接进行调用, 不需要知道其实现过程.

常用API

Objects类

定义

Objects类是java所有类的根类

方法
方法名称方法描述(原文)-JDK1.8
static String toString(Object o)Returns the result of calling toString for a non-null argument and “null” for a null argument.
static boolean equals(Object a, Object b)Returns true if the arguments are equal to each other and false otherwise.Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

String类

定义

String类是一个不可变的字符串, 其底层实现为字符串数组.

常用的构造方法
构造方法
String(byte[] bytes)Constructs a new String by decoding the specified array of bytes using the platform’s default charset.
String(byte[] bytes, int offset, int length)Constructs a new String by decoding the specified subarray of bytes using the platform’s default charset.
String(char[] value)Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
String(char[] value, int offset, int count)Allocates a new String that contains characters from a subarray of the character array argument.
String(String original)Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
常用的方法
方法名称方法描述(原文)-JDK1.8
boolean isEmpty()Returns true if, and only if, length()is 0.
boolean contains(CharSequence s)Returns true if and only if this string contains the specified sequence of char values.
boolean startsWith(String prefix)Tests if this string starts with the specified prefix.
boolean endsWith(String suffix)Tests if this string ends with the specified suffix.
boolean equals(Object anObject)Compares this string to the specified object.
boolean equalsIgnoreCase(String anotherString)Compares this String to another String, ignoring case considerations.
int length()Returns the length of this string.
String concat(String str)Concatenates the specified string to the end of this string.
char charAt(int index)Returns the char value at the specified index.
int indexOf(String str)Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
String substring(int beginIndex)Returns a string that is a substring of this string.
String substring(int beginIndex, int endIndex)Returns a string that is a substring of this string.
char[] toCharArray()Converts this string to a new character array.
byte[] getBytes()Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.
String replace(char oldChar, char newChar)Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
String toLowerCase()Converts all of the characters in this String to lower case using the rules of the default locale.
String toUpperCase()Converts all of the characters in this String to upper case using the rules of the default locale.
String[] split(String regex)Splits this string around matches of the given regular expression.

StringBuilder类

定义

String类是一个可变的字符串, 其底层实现为字符串数组.

常用构造方法
构造方法构造方法描述(原文)-JDK1.8
StringBuilder()Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(int capacity)Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
StringBuilder(String str)Constructs a string builder initialized to the contents of the specified string.
常用方法
增加

append()

insert()

删除

delete()

deleteCharAt()

修改

replace()

setCharAt()

查询

charAt()

length()

capacity()

Date类

常用的构造方法
构造方法构造方法描述(原文)-JDK1.8
Date()Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
Date(long date)Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as “the epoch”, namely January 1, 1970, 00:00:00 GMT.
常用的方法
方法名称方法描述(原文)-JDK1.8
long getTime()Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
void setTime(long time)Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

SimpleDateFormat类

定义

专门对日期进行格式转换的

常用的构造方法
构造方法构造方法描述(原文)-JDK1.8
SimpleDateFormat()Constructs a SimpleDateFormat using the default pattern and date format symbols for the default FORMAT locale.
SimpleDateFormat(String pattern)Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default FORMAT locale.
常用的方法
方法名称方法描述(原文)-JDK1.8
StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos)Formats the given Date into a date/time string and appends the result to the given StringBuffer.
Date parse(String text, ParsePosition pos)Parses text from a string to produce a Date.

Calendar类

常用的构造方法
构造方法构造方法描述(原文)-JDK1.8
protected Calendar()Constructs a Calendar with the default time zone and the default FORMAT locale.
常用的方法
方法名称方法描述(原文)-JDK1.8
static Calendar getInstance()Gets a calendar using the default time zone and locale.
void set(int field, int value)Sets the given calendar field to the given value.
int get(int field)Returns the value of the given calendar field.
abstract void add(int field, int amount)Adds or subtracts the specified amount of time to the given calendar field, based on the calendar’s rules.

System类

常用的方法
方法名称方法描述(原文)-JDK1.8
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
static void gc()Runs the garbage collector.
static long currentTimeMillis()Returns the current time in milliseconds.
static void exit(int status)Terminates the currently running Java Virtual Machine.

Array类

常用的方法
方法名称方法描述(原文)-JDK1.8
static String toString(int[] a)Returns a string representation of the contents of the specified array.
static int binarySearch(int[] a, int key)Searches the specified array of ints for the specified value using the binary search algorithm.
static void sort(int[] a)Sorts the specified array into ascending numerical order.

Math类

常用的方法
方法的名称方法描述(原文)-JDK1.8
static int abs(int a)Returns the absolute value of an int value.
static double ceil(double a)Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
static double floor(double a)Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
static int max(int a, int b)Returns the greater of two int values.
static int min(int a, int b)Returns the smaller of two int values.
static double pow(double a, double b)Returns the value of the first argument raised to the power of the second argument.
static double random()Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
static int round(float a)Returns the closest int to the argument, with ties rounding to positive infinity.
static double sqrt(double a)Returns the correctly rounded positive square root of a double value.

基本包装类

基本包装类部分是对基本类型的包装: Byte, Short, Integer, Long, Float, Double, Boolean, Character.

Integer类
常用构造方法
构造方法构造方法描述(原文)-JDK1.8
Integer(int value)Constructs a newly allocated Integer object that represents the specified int value.
Integer(String s)Constructs a newly allocated Integer object that represents the int value indicated by the String parameter.
常用的方法
方法名称方法描述(原文)-JDK1.8
static String toBinaryString(int i)Returns a string representation of the integer argument as an unsigned integer in base 2.
static String toHexString(int i)Returns a string representation of the integer argument as an unsigned integer in base 16.
static String toOctalString(int i)Returns a string representation of the integer argument as an unsigned integer in base 8.
static String toString(int i, int radix)Returns a string representation of the first argument in the radix specified by the second argument.

异常处理

定义

在运行过程中, 代码出现了可控范围外的操作, 这种操作如果可以进行过处理, 那么我们称之为异常处理.

如何处理异常

try…catch操作

try{
	可能会出现的异常代码;
}catch(异常的类名 变量名){
	处理方法;
}finally{
	执行代码;
}

正则表达式

此部分参见教程: 正则表达式30分钟入门教程

其他

== 与 equals方法的区别

==对于基本类型进行值比较, 对于引用类型进行地址比较.

equals()则只能对引用类型使用, 对String其进行了重写比较两个字符串序列是否相等, 默认情况下也是比较对象的地址值.

intern()方法

String s = new String("a") + new String("b");
s.inter();
String str = "aa";
System.out.println(s == str);

请问上述代码的结果是true还是false?

问题解答

此问题与Java的版本有关, 如果是1.7以后版本会返回true, 否则会返回false, 产生这个问题的原因是因为java内部对常量池的位置进行了更改, 1.6版本常量池放在perm区, 而perm区和堆区是分开的, 而在1.7中已经从perm当中移动到了堆区, 1.6中是直接将字符串复制到常量池并返回引用, 而1.7则是将相同的字符串实例引用添加到常量池当中, 不进行复制, 直接返回引用.

关于我

我的Github: PorterZhang2021
我的博客地址:PorterZhang

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值