API
API(Application Program Interface)就是应用程序编程接口,我们可以理解为:类的说明书。用于说明 类/接口/枚举 类型来自哪里(包名),叫什么(名称),做什么(解释),有什么(属性、方法、构造函数)以及他们具体做什么(方法的解释说明)。
常用类
Object
java.lang包下的所有类,在java程序中使用时,都不需要导包。
- 在java.lang包下
- Object类是所有类的超类
- 所有对象(包括数组)都实现这个类的方法
常用方法:
hashCode():返回对象的哈希值——可以理解为对象在内存中的地址
equals(Object o):判断调用对象与o的地址是否相同(建议每个子类都重写)
toString():以字符串形式返回对象信息(建议每个子类都重写)
equals()与 == 的区别:
equals()是方法; == 是比较运算符
在Object类中,二者都用于比较对象地址是否相同
在其他子类中,一般重写equals(),用于比较对象内容是否相同
public class TestObject {public static void main(String[] args) {Object obj1 = new Object();Object obj2 = new Object();boolean r1 = obj1.equals(obj2);System.out.println(r1);Student s1 = new Student(18,"张三","103");Student s2 = new Student(18,"张三","104");boolean r2 = s1.equals(s2);System.out.println(r2);}}
学生类中重写equals方法
@Overridepublic boolean equals(Object o){Student other = (Student)o;if(this.name.equals(other.name)&&this.age == other.age&&this.className.equals(other.className)){return true;}return false;}
String类
String是类,但不能被继承
- 在java.lang包下
- Java程序中所有字符串值都是String类的对象
- 字符串是常量,创建后值不可更改
构造函数:
private static void testConstroct() {// 无参构造String s1 = new String();// 有参构造byte[] bytes = {65,66,67,97,98,99};char[] chars = {'a','b','c'};String s2 = new String(bytes);String s3 = new String(bytes,2,2);String s4 = new String("Java");System.out.println(s4);}
常用方法:
charAt(int index) : 返回指定索引处的 char 值。返回char类型
concat(String str) :将指定字符串连接到调用字符串的结尾。返回String类型(没有改变字符串的值,而是开辟了新的内存地址存入新的字符串,调用对象指向新的地址)
startsWith(String prefix) :测试此字符串是否以指定的前缀开始。返回boolean类型
endsWith(String suffix) :测试此字符串是否以指定的后缀结束。返回boolean类型
getBytes() :将调用字符串转换成新的byte数组,将结果存储并返回该byte数组。(可以使用Array.toString()再转换成String)返回byte[]类型
indexOf(char ch / String str [, int fromIndex]) :返回指定字符 / 子字符串在此字符串中第一次出现处的索引[,从指定的索引开始]。返回int类型
lastIndexOf(char ch / String str [, int fromIndex]) :返回指定字符 / 子字符串在此字符串中最后一次出现处的索引[,从指定的索引开始]。返回int类型
length():返回调用字符串的长度——即字符串内字符个数,返回int类型
isEmpty():判断字符串是否为空,返回boolean类型。
replace(char oldChar, char newChar) :返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
replaceAll(String regex, String replacment) :使用给定的 replacement 替换此字符串匹配给定的正则表达式的子字符串。
replaceFirst(String regex, String replacment) : 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
split(String regex[, int limit]) : 根据给定正则表达式拆分字符串;limit为拆分次数上限(可有可无,没有就算全部),返回String[]类型
substring(int begin[, int end]) : 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
toCharArray() : 将此字符串转换为一个新的字符数组,返回值类型char[]
trim() : 返回字符串的副本,忽略前导空白和尾部空白。
java正则表达式汇总
StringBuffer类
线程安全的可变字符序列. 效率低
StringBuffer 转 String:
StringBuffer的toString()
new String(StringBuffer)
String.valueOf(Object );
构造函数:
StringBuffer sb1 = new StringBuffer("java");
常用方法:
setCharAt(char ch) : 将给定索引处的字符设置为ch
deleteCharAt(int index) : 将指定索引处的字符删除
insert(int index, a ) : 将a的字符串表达形式从指定索引从插入;a的数据类型可以是int、long、float、double、char、boolean、Object、String、char[](char[]可以指定char[]的起始位置和长度);返回值类型:StringBuffer
append(a) : 将a的字符串表达形式追加;a的数据类型可以是int、long、float、double、char、boolean、Object、String、StringBuffer、char[](char[]可以指定char[]的起始位置和长度);返回值类型:StringBuffer
StringBuilder
不保证线程安全的可变序列,与StringBuffer的API完全一致,但效率更高。
包装类
包装类主要指 将8种基本数据类型包装成对应的类
.大多数情况,包装类的主要作用是用于 基本数据类型与字符串的相互装换.
基本数据类型 |
包装类 |
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
boolean |
Boolean |
char |
Character |
- 8种基本数据类型中除了Character以外都有parseXxx(String s) 将字符串转为对应的基本数据类型
- 自动装箱:可以直接将基本数据类型赋值给对应的包装类对象
- 自动拆箱:可以直接将包装类对象转为基本数据类型
int 转 Integer:
int a = 1;
Integer i1 = new Integer(a);
Integer i2 = a;
Integer i3 = Integer.valueOf(a);
Integer 转 int :
Integer i = new Integer(2);
int n1 = i.intValue();
int n2 = i;
int 转 String :
int i = 1;
String s1 = “”+i;
String s2 = String.valueOf(i);
String 转 int :
String s = “1”;
int i = Integer.parseInt(s);
String 转 Integer :
String s = “1”;
Integer i1 = new Integer(s);
Integer i2 = Integer.valueOf(s);
Integer 转 String :
Integer i = 1;
String s = String.valueOf(i);
Math
public static void main(String[] args) {// 圆周率常量值System.out.println(Math.PI);// 绝对值System.out.println(Math.abs(-1));System.out.println(Math.abs(1));// 开立方根System.out.println(Math.cbrt(27));// 开平方根System.out.println(Math.sqrt(2));// 第一个参数的第二个参数次幂System.out.println(Math.pow(2, 0.25));// 上限最小整数double值System.out.println(Math.ceil(12.3));System.out.println(Math.ceil(12.8));System.out.println(Math.ceil(-12.3));System.out.println(Math.ceil(-12.8));System.out.println("-------");// 下限最大整数double值System.out.println(Math.floor(12.3));System.out.println(Math.floor(12.8));System.out.println(Math.floor(-12.3));System.out.println(Math.floor(-12.8));// 四舍五入System.out.println(Math.round(1.4));// 返回 >=0.0 <`1.0 的double随机数System.out.println(Math.random());// 1-10的随机数 ?int i = 1;while(i <= 100) {// System.out.println(Math.round(Math.random()*10)); // 有0// System.out.println((int)(Math.random()*10));System.out.print((int)(Math.random()*10+1)+" ");i++;}}
System
long l = System.currentTimeMillis();//获取当前毫秒值,从1990-1-1开始
Random
package com.zhiyou100.homework;import java.util.Random;public class TestRandom {public static void main(String[] args) {Random r1 = new Random();System.out.println(r1.nextInt());// Random r2 = new Random(1);// System.out.println(r2.nextInt());// 摇骰子 色子System.out.println(r1.nextInt(6)+1);/** 生成6位数随机数验证码*//** 点名程序*/}public static void dianming() {System.out.println("点名开始 ...");String[] nameArr = {"A","B","C","D","邢士祥"};for (int i = 10; i > 0; i--) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(i);}System.out.println("就是你 --> "+nameArr[new Random().nextInt(5)]);}}