Java se:常用类

Object类

  • public int hashCode()
    返回对象的哈希值。不是实际地址值,可以理解为地址值。
  • public final Class getClass()
  • public String toString()
    返回对象的字符串表示,默认是由类的全路径+’@’+哈希值的十六进制表示。
  • public boolean equals(Object obj)
    比较两个对象是否相同。默认情况下,比较的是地址值是否相同。
  • protected void finalize()
    当垃圾回收器确定不存在改对象的更多引用时,由对象的垃圾回收期调用此方法,主要准对堆内存
  • clone()
    可以实现对象的克隆,包括成员变量的数据复制,但是它和两个引用指向同一个对象是有区别的。

Scanner类

  • hasNextXxx(),判断是否还有下一个输入项,Xxx可以为Int或者Double等
  • nextXxx()获取下一个输入项

String类

字符串可以看作字符数组

  • 构造方法
    A:public String()
    B:public String(byte[] bytes)
    C:public String(byte[] bytes,int offset,int length)
    D:public String(char[] value)
    E:public String(char[] value,int offset,int count)
    F:public String(String original)
    下面的这一个虽然不是构造方法,但是结果也是一个字符串对象
    String s = “hello”;

  • 特点
    字符串一旦被赋值,就不能改变。
    注意:这里指的是字符串的内容不能改变,而不是引用不能改变。
    在这里插入图片描述

  • 判断功能
    boolean equals (Object obj)
    boolean equalsIgnoreCase(String str)
    boolean contains(String str)
    boolean startsWith(String str)
    boolean endsWith(String str)
    boolean isEmpty()

  • 获取功能
    int length()
    char charAt(int index) 获取某个位置的字符
    int indexOf(int ch) 返回指定字符在此字符串中第一次出现的索引
    int indexOf(String str)
    int indexOf(int ch,int fromIndex) 返回指定字符在该字符串指定位置后第一次出现的索引
    int indexOf(String str,int fromIndex)
    String substring(int start) 从指定位置开始截取字符串,默认到末尾**(左闭右开)**
    String substring(int start,int end)

  • 转换功能
    byte[] getBytes()
    char[] toCharArray()
    static String valueOf(char[] chs)
    static String valueOf(int i)
    String toLowerCase()
    String toUpperCase()
    String concat(String str) 把字符串进行拼接

  • 其他功能

    • 替换
      String replace(char old,char new)
      String replace(String old,String new)
    • 去除字符串两空格
      String trim()
    • 按字典序比较两个字符串
      int compareTo(String str)
      int compareToIgnoreCase(String str)

StringBuffer类

用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了一个字符串缓冲区类。
线程安全的可变字符序列。

  • 构造方法
    StringBuffer()
    StringBuffer(int size) 指定容量
    StringBuffer(String str) 指定字符串内容
  • 添加功能
    public StringBuffer append (String str)
    public StringBuffer insert(int offset,String str)
  • 删除功能
    public StringBuffer deleteCharAt(int index)
    public StringBuffer delete(int start,int end)
  • 替换功能
    public StringBuffer replace(int start,int end,String str)
  • 反转功能
    public StringBuffer reverse() 判断回文串有奇效
  • 截取功能
    public String substring(int start)
    public String substring(int start,int end)
    返回值是String类型,StringBuffer对象本身没有发生改变

String和StringBuffer的转换

String --> StringBuffer
StringBuffer的构造方法
StringBuffer --> String
toString()方法

String,StingBuffer,StringBuilder区别

StringBuffer:同步的,数据安全,效率低。
StringBuilder:不同步的,数据不安全,效率高。

Arrays类

针对数组进行操作的工具类,提供了排序查找等功能

  • 成员方法
    public static String toString(int[] a)
    public static void sort(int[] a)
    public static int binarySearch(int[] a,int key)针对数组有序的情况进行查找

包装类

为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型
	byte 		Byte
	short		Short
	int		Integer
	long		Long
	float		Float
	double		Double
	char		Character
	boolean		Boolean

Integer类

  • 构造方法:
    public Integer(int i)
    public Integer(String str)必须是数字构成的字符串
  • 成员方法:
    public static int parseInt(String s)
    public static String toString(int i)
    public static Integer valueOf(int i)
    public static Integer valueOf(String s)
  • 常用的进制转换方法
    public static String toBinaryString(int i)
    public static String toOctalString(int i)
    public static String toHexString(int i)

String和int的相互转换

//字符串转换为数字
	String s1 = "12345";
	int n1 = Integer.parseInt(s1);
	int n2 = Integer.valueOf(s1);
	Integer n3 = new Integer(s1);
//数字转换为字符串
	int nn1 = 54321;
	String ss1 = String.valueOf(nn1);
	String ss2 = nn1+" ";
	String ss3 = Integer.toString(nn1);
  • JDK5的新特性
    自动装箱 基本类型–引用类型
    自动拆箱 引用类型–基本类型

Character

  • 构造方法
    Character ch = new Character(‘a’);
  • 成员方法

public static boolean isUpperCase(char ch)
public static boolean isLowerCase(char ch)
public static boolean isDigit(char ch)
public static char toUpperCase(char ch)	
public static char toLowerCase(char ch)

正则表达式

符合一定规则的字符串

  • 常见规则
  •   A:字符
      	x 字符 x。举例:'a'表示字符a
      	\\ 反斜线字符。
      	\n 新行(换行)符 ('\u000A') 
      	\r 回车符 ('\u000D')
      	
      B:字符类
      	[abc] a、b 或 c(简单类) 
      	[^abc] 任何字符,除了 a、b 或 c(否定) 
      	[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 
      	[0-9] 0到9的字符都包括
      	
      C:预定义字符类
      	. 任何字符。我的就是.字符本身,怎么表示呢? \.
      	\d 数字:[0-9]
      	\w 单词字符:[a-zA-Z_0-9]
      		在正则表达式里面组成单词的东西必须有这些东西组成
    
      D:边界匹配器
      	^ 行的开头 
      	$ 行的结尾 
      	\b 单词边界
      		就是不是单词字符的地方。
      		举例:hello world?haha;xixi
      	
      E:Greedy 数量词 
      	X? X,一次或一次也没有
      	X* X,零次或多次
      	X+ X,一次或多次
      	X{n} X,恰好 n 次 
      	X{n,} X,至少 n 次 
      	X{n,m} X,至少 n 次,但是不超过 m 次 
    

判断功能
public boolean matches(String regex)
分割功能
public String[] split(String regex)
替换功能
public String replaceAll(String regex,String replacement)
获取功能
Pattern和Matcher类的使用

Math类

  • 成员方法:
    public static int abs(int a)
    public static double ceil(double a)
    public static double floor(double a)
    public static int max (int a,int b)
    public statci double pow(double a,double b)
    public static double random()
    public static int round(float/double a) 四舍五入
    public static double sqrt(double a)

获取随机数:1-100之间
int number = (int)(Math.random()*100)+1

Random类

  • 用于产生随机数的类
  • 构造方法:
    A:Random() 默认种子,每次产生的随机数不同
    B:Random(long seed) 指定种子,每次种子相同,随机数就相同
  • 成员方法:
    A:int nextInt() 返回int范围内的随机数
    B:int nextInt(int n) 返回[0,n)范围内的随机数

System类

System 类包含一些有用的类字段和方法。它不能被实例化。

  • 成员方法
    public static void gc()
    public static void exit(int status)
    public static long currentTimeMillis()
    public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)

BigInteger类

可以让超过Integer范围内的数据进行运算

  • 构造方法
    BigInteger bi = new BigInteger(“2147483648”);
  • 成员方法
    public BigInteger add(BigInteger val)
    public BigInteger subtract(BigInteger val)
    public BigInteger multiply(BigInteger val)
    public BigInteger divide(BigInteger val)

BigDecimal类

不可变的、任意精度的有符号十进制数。

  • 构造方法
    BigDecimal bd1 = new BigDecimal(“0.09”)
  • 成员方法
    public BigDecimal add(BigDecimal augend)
    public BigDecimal subtract(BigDecimal subtrahend)
    public BigDecimal multiply(BigDecimal multiplicand)
    public BigDecimal divide(BigDecimal divisor)
    public BigDecimal divide(BigDecimal divisor,int scale,
    int roundingMode)自己保留小数几位

Date/DateFormat类

Date是日期类,可以精确到毫秒。
DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat

  • Date构造方法
    Date()
    Date(long time)
  • Date成员方法
    getTime()
    setTime(long time)
  • SimpleDateFormat(String pattern) 给定模式
    yyyy-MM-dd HH:mm:ss
  • 日期和字符串的转换
    Date --> String
    public final String format(Date date)
    String --> Date
    public Date parse(String source)

Calendar类

  • 成员方法
    public static Calendar getInstance() 获得一个日历对象,本质返回的是子类对象

    public int get(int field) 根据日历字段得到对应的值

    public void add(int field,int amount)
    根据日历字段和一个正负数确定是添加还是减去对应日历字段的值

    public final void set(int year,int month,int date) 设置日历对象的年月日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值