装箱与拆箱
装箱:
将基本类型自动转换为包装类型
int a=10;
Integer aa=Integer.valueOf(a);
Integer aa1=new Integer(a);
Integer aa=a; 自动装箱,直接把基本类型赋值给包装类
就是默认调用valueOf(a);
拆箱:
把包装类型转为基本类型,底层用到的是intValue()
public class InterDemo2 {
public static void main(String[] args) {
/*
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
当自动装箱时,默认调用Integer类中的valueOf()
这个方法内部对 -128--127之间进行缓存(数组),在此区间自动的撞见过,不会创建新的Integer,直接从数组获取
超出此区间后,每次都会new 新的Integer对象
*/
Integer a=10;
Integer b=10;
System.out.println(a==b); //true
System.out.println(a.equals(b));//true
//equals(比较的是内容)
Integer c=128;
Integer d=128;
System.out.println(c==d); //false
System.out.println(c.equals(d));//true
Integer e=Integer.valueOf(-128);
Integer f=Integer.valueOf(-128);
System.out.println(e==f);
}
}
String类
字符串的值不可改变,一旦字符串对象被创建,值就不能改变了
底层存储字符串的数组,是被final修饰的,必须在对象创建之后由构造方法对其赋值
public final char value[]
java中字符串创建的两种方式
方式1:
String s1="abc";
String s2="abc";
在第一次创建s1变量时,会去内存中有一个叫字符串常量池的空间,检索,有没有次内容的一个字符串对象
如果没有,就会在字符串常量池中创建一个字符对象,把对象的地址给s1,
在第二次创建s2变量时,会去字符串常量池中查找,如果有,直接将之前创建的字符对象赋给s2
一旦出现要创建的字符串对象内容一致,返回拿到的是同一个字符串对象的地址
方式2:
String s3=new String("abc");
无论是否存在相同内容的字符串对象,都会创建一个新的字符串对象
public class StringDemo1 {
public static void main(String[] args) {
String s="abc";
s+="aaa";
String s1="abc";
String s2="abc";
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
String s3=new String("abc");
String s4=new String("abc");
System.out.println(s3==s4);//false
System.out.println(s3.equals(s4));//true
}
}
String 构造方法
String();
String("abc");
String(byte[] bytes);
String(char[] value);
转换功能
byte[] getBytes()
char[] toCharArray()
public class StringDemo2 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s1=new String();
String s2=new String("abc");
String s3="abc你好";
byte[] bytes=s3.getBytes();//把字符串转为byte类型
System.out.println(Arrays.toString(bytes));
String s4=new String(bytes,"utf-8");
System.out.println(s4);
String s5="bced";
char[] chars=s5.toCharArray();//字符串---->char数组
System.out.println(Arrays.toString(chars));
Arrays.sort(chars);
String s6=new String(chars);//char数组---->字符串
System.out.println(s6);
}
}
判断功能:
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean isEmpty()
boolean startsWith(String prefix)
boolean endsWith(String suffix)
public class StringDemo3 {
public static void main(String[] args) {
String s="abcd";
System.out.println(s.equals("abcD"));//false
System.out.println(s.equalsIgnoreCase("abcD"));//true
System.out.println(s.contains("ab"));//判断是否包含子串,子串必须连续
System.out.println(s.contains("ac"));//false
System.out.println(s.isEmpty());//判断是否是空串 null关键字不是空串
System.out.println(s.startsWith("a"));//判断是否以指定的子串开头
System.out.println(s.endsWith("d"));//判断是否以指定的子串结尾
}
}
获取功能
length()
char charAt(int index)
int indexOf(String str)
int indexOf(String str,int fromIndex)
String substring(int start)
String substring(int start,int end)
public class StringDemo4 {
public static void main(String[] args) {
String s="abcdcd";
System.out.println(s.length());//获取长度
System.out.println(s.charAt(2));//返回对应索引的字符
System.out.println(s.indexOf("c"));//获取指定字符首次出现的位置
System.out.println(s.indexOf("cd"));//获取指定字符首字母首次出现的位置
System.out.println(s.indexOf("c",s.indexOf("c")+1));//从指定位置查找
System.out.println(s.lastIndexOf("c"));//从后向前查找
String s1=s.substring(3);//从指定位置开始截取字符串,返回一个新的子字符串
String s2=s.substring(0,4);//从指定位置开始到指定位置结束(不包含结束)截取字符串,返回一个新的子字符串
System.out.println(s1);
System.out.println(s);
System.out.println(s2);
}
}
转换功能
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
String toLowerCase()
String toUpperCase()
String concat(String str)
Stirng[] split(分割符);
public class StringDemo5 {
public static void main(String[] args) {
Integer a = null;
//System.out.println(a.toString());
String s = String.valueOf(a);//把替他类型转为字符串,建议使用,避免出现空指针异常
System.out.println(s);
char[] c = {'a', 'b', 'c'};
String s1 = String.valueOf(c);
String s2="abcDEF";
System.out.println(s2.toLowerCase());
System.out.println(s2.toUpperCase());
System.out.println(s2.concat("www"));//将指定字符串拼接到字符串末尾,返回一个新字符串
System.out.println(s2);
String s4="ab;cde;fg";
String[] strings=s4.split(";");//使用指定的字符将字符串分割为数组 正则表达式
System.out.println(Arrays.toString(strings));
}
}
替换功能
String replace(char old,char new)
String replace(String old,String new)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
去除字符串两空格
String trim()
public class StringDemo6 {
public static void main(String[] args) {
String s=" abc defg ";
System.out.println(s.replace('c', 'C'));
System.out.println(s.replace("cd", "CC"));
//replaceAll()使用正则表达式匹配需要替换的内容
System.out.println(s.replaceAll("c", "CC"));
System.out.println(s.replaceFirst("a", "CC"));
System.out.println(s.length());
System.out.println(s.trim().length());
System.out.println(s.replace(" ", ""));
}
}
Math类
abs 绝对值
sqrt 平方根
PI π
pow(double a, double b) a的b次幂
max(double a, double b)
min(double a, double b)
random() 返回 0.0 到 1.0 的随机数
floor(double a) 向下取整
ceil(double a) 向上取整
long round(double a) double型的数据a转换为long型(四舍五入)
public class MathDemo {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.abs(-1));
System.out.println(Math.pow(2,3));
System.out.println(Math.floor(9.9));
System.out.println(Math.ceil(9.1));
System.out.println(Math.round(9.4));
System.out.println(Math.round(9.6));
System.out.println(Math.random());//返回一个大于等于0 小于1的随机数
}
}
Random类
此类用于产生随机数
构造方法
public Random()
成员方法
public int nextInt()
public int nextInt(int n)
import java.util.Arrays;
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random random=new Random();
//随机返回
System.out.println(random.nextBoolean());
System.out.println(random.nextInt());//在int取值范围内随机返回一个结果
System.out.println(random.nextInt(3));//在指定的范围内返回一个随机数,大于等于0 小于给定的值
byte[] bytes=new byte[5];
random.nextBytes(bytes);
System.out.println(Arrays.toString(bytes));
}
}