JAVA 常用API

11. Java API

###11.1 Class Object:

Object是java中所有类的超类(父类),因此所有的类都能调用Object类中的非私有的方法.

  • String toString(); 返回某个对象的字符串表示。默认的toString是直接输出地址值。如果想要打印类的内容,那么就必须在每个子类中重写toString方法( Idea快捷键:alt+insert)
import java.util.Objects;

public class Student {
    private String ID;
    private String name;
    private int age;
    private String city;

    @Override
    public String toString() {
        return ID+','+name+","+age+","+city;
    }
}
  • **boolean equals(Object obj)?*比较两个对象是否相等,如果没有重写equals 方法,默认是比较两个对象的地址值。 在对象的内容比较过程中,这个equals方法必须由每个子类重写,否则默认使用==来比较地址值。

11.2Class String

  • The String class represents character strings. All string literals in Java programs, such as “abc”, are implemented as instances of this class.
  • Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";

​ is equivalent to:

 char data[] = {'a', 'b', 'c'};
     String str = new String(data);
  • Strings class has overriden the toStrings() method so that we can print the text out in stead of the address of the Strings.
constructor

String a = “abc”;

String a = 123+"";//将数值型转为String 类,只需进行字符串拼接。

new String(char[] char, int offset , int len)//将字符数组转为字符串。

new String(byte[] byte, int offset , int len)//将字节数组转为字符串。

new String(int[] int, int offset , int len)//将int数组转为字符串。

常用方法,

boolean equals(Object obj);

Tips: equals/ equalsIgnoreCase 的用法:为避免空指针异常(voidPointerException.),将常量写在前面。

eg: var.equals(“abc”) —> “abc”.equals(var)

boolean equalsIgnoreCase (String anotherString);

boolean isEmpty();

boolean contains(CharSequence s)

int length () :return the length of a string。

char charAr(int index);

int indexOf (String str) :return the index of the given string firstly occurs in the string。

int indexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。

byte[] getbytes();//transfer it to a byte array.

char[] toCharArray();// transfer it to a char array.

String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。

String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。

String replace (Char older, Char replacement) :将与target匹配的字符串使用replacement字符串替换。

String replaceAll (String regex,String replacement)

String replaceFirst(String regex, String replacement)

String[] split(String regex);///regex means regular expression.

String[] split(String regex,int limit);//

String toLowerCase()

String toUpperCase()

11.3Class StringBuilder

  • A mutable sequence of characters;它能避免在字符串拼接过程中,生成一些多余的字符串,占用过多的内存,扩容机制与Arraylist 一致。
constructor:

StringBuilder() 构造一个没有字符的字符串构建器,初始容量为16个字符。
StringBuilder(String str) 构造一个初始化为指定字符串内容的字符串构建器

common method

public StringBuilder append(任意类型):添加数据,并返回对象本身(支持链式调用)。
public StringBuilder reverse():字符序列进行反转
public String toString():返回此序列中数据的字符串表示形式。 转为String

pubulic int capacity();//Returns the current capacity.

11.4 Class Arrays

工具类Arrays的常用方法
public static String toString(int[] a):把数组转成字符串
public static void sort(int[] a):对数组进行升序排序

11.5 包装类

在java语言中,存在8种基本数据类型,而对于真实存在的东西,java语言都会进行描述并封装为类。所以java基于8种基本类型进行了封装:基本数据类型包装类(包装类)

基本类型: byte 、short 、int 、long 、boolean 、float 、double 、char

包装类: Byte 、Short 、Integer 、Long 、Boolean 、Float 、Double 、Character

为什么要使用包装类呢?

基本类型数据通常只有两个功能:赋值、取值。 当把基本类型封装为类后,就可以使用更多的功能来操作基本类型数据,基本数据类型包装类最常见的用法就是用于和字符串之间进行相互转换

04_int类型和String类型的相互转换
int->String
1+""
String.valueOf(数据);

String ->int(重点)
包装类.ParseXXX
int i= Integer.parseInt(“100”)

11.6 Class Date

Date类的构造方法:
Date() 无参构造方法:以当前的系统时间来创建出一个Date对象
Date(long date):根据指定的毫秒值,创建出一个Date对象。 指定的毫秒值,**从1970年1月1日(**计算机的基准时间)起经过的毫秒值

常用方法:
public long getTime() 把日期对象转换成对应的时间毫秒值。
void setTime(long time) 将此 Date对象设置为1970年1月1日00:00:00 起经过的毫秒值

08_SimpleDateFormat类的概述和使用

构造方法
SimpleDateFormat(String pattern) 使用给定模式构建一个 SimpleDateFormat ,默认日期格式符号为默认的 FORMAT区域设置。
参数pattern就是模式
字母模式:y表示面 M表示月 d表示日 H表示时 m表示分 s表示秒 S表示毫秒 中国式时间: 2019年3月11日 11点 09分 33秒 333毫秒
代码的字母模式: yyyy年MM月dd日 HH点mm分ss秒 SSS毫秒
成员方法
格式化(日期 -> 文本): Date – String
public final String format(Date date)解析(文本 -> 日期): String – Date

​ 解析字符串(文本->日期)

​ public Date parse(String source)

####11.7 Class Math

package com.itheima.Math_05;

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.PI);
        System.out.println(Math.abs(100));//取绝对值
        System.out.println(Math.abs(-100));
        System.out.println(Math.ceil(3.4));//向上取整
        System.out.println(Math.ceil(3.6));
        System.out.println(Math.floor(3.6));//向下取整
        System.out.println(Math.floor(3.4));
        System.out.println(Math.max(1, 2));
        System.out.println(Math.min(1, 2));
        System.out.println(Math.round(3.4));//四舍五入
        System.out.println(Math.round(3.5));
        System.out.println(Math.pow(3,3));
    }
}

####11.8 Class Random

constructor

​ Random() 创建一个新的随机数生成器。

Methods
int nextInt() 从这个随机数生成器的序列返回下一个伪随机数,均匀分布的 int值。
int nextInt(int bound) ,均匀分布 返回值介于0(含)和指定值bound(不包括)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值