API
API
Java语言中提供的众多接口和类
API闻到那 就是官方对提供的接口和类的功能进行说明
基本的数据类型包装
基本类型: byte short int long char float double boolean
Java为每个基本类型提供了一个类
int--->Integer char--->Character
public class ApiDemo {
public static void main(String[] args) {
int num = 10;
//构造方法
Integer num1 = new Integer(10);//将int类型的值包装在Integ类的对象中
Integer num2 = new Integer("10");//把字符串转为int类型
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.BYTES);
System.out.println(Integer.SIZE);
//转换为二进制数
System.out.println(Integer.toBinaryString(10));
//转换十六进制字符串
System.out.println(Integer.toHexString(12));
//转换八进制字符串
System.out.println(Integer.toOctalString(15));
//比较大小
System.out.println(num1.compareTo(num2));//返回值-1 0 1
System.out.println(Integer.max(num1, num2));
Integer num3 = new Integer(10);
Integer num4 = new Integer(10);
//包装类型间的相等判断应该用equals,而不是'=='
System.out.println(num3.equals(num4));
System.out.println(num3 == num4);//返回false,== 比较的是对象在内存中的地址
//取出对象中包含的值
int num5 = num4.intValue();
//valueOf 将基本类型转换为引用类型
Integer num6 = Integer.valueOf(10);
//转换为字符串类型
System.out.println(Integer.toString(17));//括号里的转换为字符串类型
System.out.println(num1.toString());//num1转换为字符串类型
}
}
自动装箱和自动拆箱
自动装箱:基本类型转换为引用类型
int num0 = 10;
nteger num1=num0;
自动拆箱:将引用类型转换为基本类型
int num2=num1; //自动调用valueOf();
public class IntegerDemo {
public static void main(String[] args) {
/*
自动装箱:基本类型转换为引用类型
int num0 = 10;
Integer num1=num0;
自动拆箱:将引用类型转换为基本类型
int num2=num1; 自动调用valueOf();
*/
Integer num0 = new Integer(10);
Integer num1 = new Integer(10);
System.out.println(num0.equals(num1));//true
/*
使用new + 构造方法() 不管值是否在-128到127之间还是数值相同,都会创建新的对象,指向不同的地址
*/
System.out.println(num0 == num1);//false
Integer num2 = 127;
Integer num3 = 127;
System.out.println(num2.equals(num3));//true
/*
使用装箱时,在创建对象是,如果值在-128 -- +127 之间,且两个数值相同,那么两个变量会指向同一个地址
因为在此区间内,有一个cache数组,会直接调用里面的数值,不会创建新的对象
*/
System.out.println(num3 == num3);//true
Integer num4 = 128;
Integer num5 = 128;
System.out.println(num4.equals(num5));//true
/*
超过数值范围后,会创建新的对象,指向不同的地址
*/
System.out.println(num4 == num5);//false
}
}
Object
Object 是所有类的父类
import java.util.Objects;
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
/**
重写toString() 方法
*/
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
}
public class ObjectDemo2 {
public static void main(String[] args) {
Person p = new Person("amdin",23);
/*
对象在输出的时候会默认调用toString();方法
当类中没有定义toString()时,会默认调用父类(Object)中的toString()
public String toString() {
return getClass().getName() + "@" + Integer.tioHexStrng(hashCode());
}
本地方法,java语言不实现,调用操作系统实现
public native int hashCode()
Object类中toString()方法会将对象所在地址转为16进制的字符串输出
可以在类中重写Object类中的toString()
*/
System.out.println(p);
}
}
public class ObjectDemo {
public static void main(String[] args) {
Person p = new Person("崔傻狗",23);
System.out.println(p);
/*
Object类中的equals();比较的对象地址是否相等,与"=="功能相同
public boolean equals(Object obj) {
return (this == obj);
}
*/
Person p1 = new Person("崔傻狗",23);
Person p2 = new Person("崔傻狗",23);
System.out.println(p1.equals(p2));
System.out.println(p1==p2);
//其他类基本上都重写了equals() 比较的是内容是否相等
Integer p3=128;
Integer p4=128;
System.out.println(p3.equals(p4));
System.out.println(p3==p4);
String p5 = "abc";
String p6 = "abc";
System.out.println(p5.equals(p6));
System.out.println(p5==p6);
}
}
String
字符串是多个字符组成的字符序列,是常量(不能改变)
String s= "abc"
字符串的两种创建方式
public static void main(String[] args) {
/*
字符串的两种创建方式
String s="abc";
第一次创建时,会在字符串常量池中检测有没有,没有就在常量池中创建一个对象
第二次创建时候,如果常量池中已经存在,直接质量常量池中的对象即可
*/
String s0="abc";
String s1="abc";
System.out.println(s0.equals(s1));//true
System.out.println(s0==s1);//true
/*
new + 构造方法();
只要new出来的对象,在内存中一定是一个独一无二的对象空间
*/
String s2=new String("abc");
String s3=new String("abc");
System.out.println(s2.equals(s3));//true
System.out.println(s2==s3);//false
}
字符串是常量,值不能改变
public static void main(String[] args) {
/*
字符串是常亮,值不能改变,一旦改变是在内存中重新创建一个新的对象
private final char value[];
字符串底层是char数组存储,单个字符存储
final修饰的值不能改变
*/
String s="a";
s+="b";
s+="c";
s+="d";
s+="e";
System.out.println(s);
}
String中常用的方法
public static void main(String[] args) {
String s = "abcdefghra";
// 0123456789
String str="a";
String str1="c";
System.out.println(s.length());
System.out.println(s.charAt(2));
System.out.println("比较两个字符串"+str.compareTo(str1));
System.out.println("将指定的字符串连接到该字符串的末尾"+s.concat(str1));
System.out.println("当且仅当此字符串包含指定的char值序列时才返回true"+s.contains(str));
System.out.println("返回指定字符第一次出现的字符串内的索引"+s.indexOf("a"));
System.out.println("第二次出现的字符串内的索引"+s.indexOf("a",2));
System.out.println("从后查找第二次出现的字符串内的索引"+s.lastIndexOf("a",2));
System.out.println("判断字符串是否为空"+s.isEmpty());
System.out.println("测试此字符串是否以指定的前缀开头"+s.startsWith("c"));
System.out.println("从2开始返回后面的字符串"+s.substring(2));
System.out.println("返回2到7的字符串"+s.substring(2,7));
System.out.println("转小写"+s.toLowerCase());
System.out.println("转大写"+s.toUpperCase());
System.out.println("忽略大小写比较"+s.equalsIgnoreCase("ABCDEFGHRA"));
System.out.println("测试此字符串是否以指定的后缀结尾"+s.endsWith(str1));
System.out.println("测试是否以该字符串开头"+s.startsWith("abc"));
}
public static void main(String[] args) {
/*
替换功能
String replace(char old,char new)
String replace(String old,String new)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
去除字符串两空格
String trim()
*/
String s="abcabc";
System.out.println(s.replace("a","A"));
System.out.println(s.replaceAll("b","B"));
System.out.println(s);
String s1="1.sasd ad.";
System.out.println(s1.replaceFirst("b","B"));
System.out.println(s1.replaceAll("\\w",""));
System.out.println(s1.replaceAll("\\W",""));
String s2=" abc ";
System.out.println(s2.trim()+"\t"+s2.trim().length());
}
String构造方法和转换功能
public class StringDemo {
public static void main(String[] args) {
String s="abc";
byte data[]=s.getBytes();//编码 即就是字符转为字节
System.out.println(Arrays.toString(data));
String s2= new String(data);//解码 即就是字节转为字符
System.out.println(s2);
String s1="我爱学习";
byte d [] = new byte[0];
try {
d = s1.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(Arrays.toString(d));
String s3= null;
try {
s3 = new String(d,0,4,"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(s3);
}
}
public class StringDemo2 {
public static void main(String[] args) {
String s = "abcdEFG";
System.out.println(s.toLowerCase());//转小写
System.out.println(s.toUpperCase());//转大写
String s1="CRR";
System.out.println(s1.concat("是傻狗"));//链接字符效率高于+号的连接
String s2=s1+"傻猪";
System.out.println(s2);
/*
split();将字符串用规定的正则表达式分割为字符串数组
*/
String s3 = "c:r:r:s:b";
String [] s33 = s3.split(":");
System.out.println(Arrays.toString(s33));
String [] s32 = s3.split("\\W");
System.out.println(Arrays.toString(s32));
String s4 = "c|r|r|s|b";
String [] s41 = s4.split("\\W");
String [] s42 = s4.split("\\|");
System.out.println(Arrays.toString(s41));
System.out.println(Arrays.toString(s42));
}
}
StringBuffer 和StringBuilder
区别:
StringBuilder 内容可变
StringBuffer 线程安全 内容可变
String 内容不可变
public class StringBufferDemo {
public static void main(String[] args) {
/*
无参的构造方法,即构建了一个长度为16的char型数组
*/
StringBuffer s = new StringBuffer();//默认长度length = 16
StringBuffer s1 = new StringBuffer(10);//length = 10
StringBuffer s2 = new StringBuffer("crr");//length = 16+s2.length
/*
拼接字符串,在处理大量的字符串拼接时,使用append最优,不会创建新的对象
*/
s2.append("sg");
System.out.println(s2);
/*
在哪个位置插入字符串
*/
s2.insert(1, "吆西");
System.out.println(s2);
/*
删除数值范围内的字符串
*/
s2.delete(2, 3);
System.out.println(s2);
/*
反转字符串
*/
s2.reverse();
System.out.println(s2);
/*
截取数值范围内的字符串
*/
String s3 = s2.substring(2, 4);
System.out.println(s3);
}
}
运行结果:
crrsg
c中国rrsg
c中rrsg
gsrr中c
rr
public class StringBuilderDemo {
public static void main(String[] args) {
/*
StringBuilder 内容可变
StringBuffer 线程安全 内容可变
String 内容不可变
*/
StringBuilder s=new StringBuilder();
StringBuffer s1=new StringBuffer();
String s3=new String();
}
}