版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
DAY14 包装类和String类
包装类
问题:
基本数据类型:
引用数据类型:
Object类:是所有类的父类 引用可以指向所有类型的对象
jdk5之前:
中国大陆
Object o = 10;
实现了Object一统java天下
基本数据类型对应的包装类
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer----重点记忆 |
long | Long |
float | Float |
double | Double |
char | Character—重点记忆 |
boolean | Boolean |
以Integer为例
package cn.baizhi.day14;
public class Demo1 {
public static void main(String[] args) {
//1 基本数据类型---->包装类对象 2种方法
//构造方法
/*int a = 10;
Integer integer = new Integer(a);
System.out.println(integer);
System.out.println(integer.toString());
//valueOf(int i)
Integer integer2 = Integer.valueOf(a);
System.out.println(integer2);*/
//2 包装类---> 基本数据类型 1种方法
/*int a = 30;
Integer i = new Integer(a);
//intValue()
int b = i.intValue();
System.out.println(b);*/
//3 基本数据类型---->字符串
int a = 250;
String s = a+"";
//4 将字符串---->基本数据类型 parseInt(String s)
int parseInt = Integer.parseInt(s);
System.out.println(parseInt);
// 5 Integer---->字符串
Integer integer = new Integer(300);
String string = integer.toString();
System.out.println(string);
//6 字符串---->Integer
Integer integer2 = new Integer("23456");
int intValue = integer2.intValue();
int a1 = 300;
System.out.println(intValue+a1);
}
}
关于字符串转int/Integer 出现的异常
注意:当将一个字符串转换为int/Integer 必须要求字符串中的字符必须为纯数字字符
案例:
package cn.baizhi.day14;
public class Demo1 {
public static void main(String[] args) {
String string = "0234567800";//0可以 其他的非数字字符都 不可以
/*int parseInt = Integer.parseInt(string);
System.out.println(parseInt);*/
Integer integer = new Integer(string);
System.out.println(integer);
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "2 345678"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at cn.baizhi.day14.Demo1.main(Demo1.java:6)
自动拆箱和自动装箱
jdk5新的特性
自动拆箱:将包装类的对象直接赋值给简单数据类型
自动装箱:将简单数据类型赋值给包装类引用
案例:
package cn.baizhi.day14;
public class Demo1 {
public static void main(String[] args) {
Integer a = 250;//自动装箱
int b = a;//自动拆箱
}
}
常量池
常量池:在Java的内存中维护了一个 -128 ---- 127的常量池,当创建Integer对象数值超过了常量池的范围就会在堆中创建新的对象
面试题:
package cn.baizhi.day14;
public class Demo1 {
public static void main(String[] args) {
Integer i1 = 120;
Integer i2 = 120;
System.out.println(i1==i2);//true
Integer i3 = 250;
Integer i4 = 250;
System.out.println(i3==i4);//false
}
}
包装类的应用
考试:
张乐乐 : java 0
高健: java 缺考
案例:
package cn.baizhi.day14;
public class Demo1 {
public static void main(String[] args) {
Student student1 = new Student();//缺考
Student student2 =new Student();//考了0分
student1.setName("高健");
student2.setName("张乐乐");
student2.setScore(0);
System.out.println(student1);
System.out.println(student2);
}
}
class Student{
private String name;
private Integer score;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public Student(String name,Integer score) {
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}
}
String类
字符串的创建方式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fuw0e5m7-1571963811001)(E:\corejava\image\2019-01-23_115439.jpg)]
第一种:将刘洋直接放到串池中共享,当在创建字符串刘洋时,先会到串池中看刘洋有没有 如果没有才创建新的字符串 有的话直接用串池中的
//1 直接用字符串赋值
String string1 = "刘洋";
String string2 = "刘洋";
System.out.println(string1==string2);
第二种:通过构造方法创建字符串
//2 通过构造方法----创建了两个对象
String string1 = new String("刘洋");
String string2 = new String("刘洋");
System.out.println(string1==string2);
总结:String string1 = new String("刘洋");这段代码创建了两个对象,参数刘洋创建在串池中用于共享,new 用串池中的刘洋 在堆中创建了一个新的对象副本
字符串的拼接
第一种方式:String 的基础上每次拼接都会产生一个新的对象,这样浪费空间
不可变字符串拼接:地址是不断变化的,内容是不变的
package cn.baizhi.day14;
public class Demo2 {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
System.out.println(s1==s2);//true
s2 = s2+"world";
System.out.println(s1==s2);//
String s3 = "helloworld";
System.out.println(s2==s3);
/*
String s1 = new String("helloworld");
String s2 = new String("helloworld");
System.out.println(s1==s2);
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());*/
}
}
可变字符串:地址是不变的 字符串的内容是可变的
StringBuffer:线程安全的 效率低
StringBuilder:线程不安全的 效率高
案例:字符串反转方法
package cn.baizhi.day14;
public class Demo2 {
public static void main(String[] args) {
String string = "高健是一个娘炮";
StringBuilder stringBuilder = new StringBuilder(string);
StringBuilder reverse = stringBuilder.reverse();
System.out.println(reverse);
}
}
案例:
package cn.baizhi.day14;
public class Demo2 {
public static void main(String[] args) {
/*String s1 = "hello";
String s2 = "hello";
System.out.println(s1==s2);//true
s2 = s2+"world";
s2 =s2+"baizhi";
System.out.println(s1==s2);//
String s3 = "helloworld";
System.out.println(s2==s3);*/
/*
String s1 = new String("helloworld");
String s2 = new String("helloworld");
System.out.println(s1==s2);
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());*/
StringBuilder stringBuilder1 = new StringBuilder("hello");
StringBuilder stringBuilder2 = new StringBuilder("hello");
System.out.println(stringBuilder1==stringBuilder2);//false
System.out.println("1----"+stringBuilder1.hashCode());
System.out.println("2----"+stringBuilder2.hashCode());
StringBuilder stringBuilder3 = stringBuilder2.append("world");
System.out.println(stringBuilder3);
System.out.println("2----"+stringBuilder2.hashCode());
System.out.println(stringBuilder2==stringBuilder3);
}
}
题:
String string1 = "helloworld";
String string2 = "hello"+"world";
System.out.println(string1==string2);//true
String s1 = "hello";
String s2 = "hello";
System.out.println(s1==s2);//true
s2 = s2+"world";
System.out.println(s1==s2);//false
方法学习
1.charAt(int index):可以获取字符串中指定下标的字符案例:
String string = "高健是一个人妖";
char charAt = string.charAt(1);
System.out.println(charAt);
2.contains(CharSequence s):判断某个字符或者字符串在这个字符串中是否存在
案例:
String string = "abcd";
boolean contains = string.contains("ca");
System.out.println(contains);
3.endsWith(String suffix)测试此字符串是否以指定的后缀结束。
startsWith(String prefix) 测试此字符串是否以指定的前缀开始。
案例:
String string = "abcd";
boolean endsWith = string.endsWith("cd");
System.out.println(endsWith);
boolean startsWith = string.startsWith("a");
System.out.println(startsWith);
4.getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
案例:
String string = "abcd";
byte[] bytes = string.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
5.
indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。
indexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
案例:
String string = "abcdaghklahgha";
int indexOf = string.indexOf('a');
System.out.println(indexOf);
int indexOf2 = string.indexOf('a', 1);
System.out.println(indexOf2);
6.length() 返回此字符串的长度。
String string = "abcdaghklahgha";
System.out.println(string.length());
7.isEmpty() 当且仅当 length() 为 0 时返回 true。
案例:
String string = "";
System.out.println(string.length());
boolean empty = string.isEmpty();
System.out.println(empty);
8.lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引
lastIndexOf(String str, int fromIndex) 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
案例:
String string = "ahdadjsal";
int lastIndexOf = string.lastIndexOf('a');//7
System.out.println(lastIndexOf);
int lastIndexOf2 = string.lastIndexOf("a", 4);
System.out.println(lastIndexOf2);//3
9.replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
案例:
String string = "ahdadjsal";
/*String replace = string.replace('a', 'A');
System.out.println(replace);*/
String replaceAll = string.replaceAll("a", "A");
System.out.println(replaceAll);
10.split(String regex) 根据匹配给定的正则表达式来拆分此字符串。
案例:
String string = "aa:.hgl.hfh.j:.akg.:l";\
// \\.---->. :
String[] split = string.split("");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
System.out.println("程序结束");
String string = "1高健22张乐乐13刘洋";
String[] split = string.split("\\d+");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
11.substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串
案例:
String string = "ahgladlg";
String substring = string.substring(2);
System.out.println(substring);//gladlg
//包头不包尾
String substring2 = string.substring(2, 4);//gl
System.out.println(substring2);
12.toCharArray():将字符串转换成一个字符数组
案例:
String string = "张乐乐是个渣男";
char[] charArray = string.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}
13.
toLowerCase() :转小写
toUpperCase():转大写
案例:
String string = "张乐乐是个渣男agasDFGHSFGHSHdasdfsadf";
String lowerCase = string.toLowerCase();
System.out.println(lowerCase);
String upperCase = string.toUpperCase();
System.out.println(upperCase);
//当注册邮箱时会将大写转换成小写
14.trim();
String string = " 张乐乐是个渣男ag asDFGHSF GHSHdasdfsadf ";
String trim = string.trim();
System.out.println(trim);
大数据运算
BigDecimal:可以实现浮点数的精确计算
public BigDecimal divide(BigDecimal divisor,
int scale,
int roundingMode)返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。如果必须执行舍入,以生成具有指定标度的结果,则应用指定的舍入模式。
相对于此遗留方法,应优先使用新的 divide(BigDecimal, int, RoundingMode) 方法。
参数:
divisor - 此 BigDecimal 要除以的值。
scale - 要返回的 BigDecimal 商的标度。
roundingMode - 要应用的舍入模式。
案例:
package cn.baizhi.day14;
import java.math.BigDecimal;
public class Demo4 {
public static void main(String[] args) {
/*int a = 1;
int b = 0;
System.out.println(a/b);*/
/*double a = 1;
double b = 0;
System.out.println(a/b);//Infinity
*/
/*double a = 1;
double b = 0.9;
System.out.println(a-b);//0.09999999999999998
Double c = 1.0;
Double d = 0.9;
System.out.println(c-d);*/
BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("3");
BigDecimal subtract = a.subtract(b);// -
System.out.println(subtract);
System.out.println(a.add(b));// +
System.out.println(a.multiply(b));// *
//System.out.println(a.divide(b));// /
System.out.println(a.divide(b, 4, BigDecimal.ROUND_HALF_UP));
}
}
作业
27选做 其他全做
5.
package cn.baizhi.day14;
import java.util.Random;
public class Demo3 {
public static void main(String[] args) {
//1 创建一个StringBuilder对象
StringBuilder stringBuilder = new StringBuilder();
//2 循环4次 每次随机获取一个字符 进行拼接
String string = "afghaskghaSDFGHDHFKLS9W3E475T67899";
Random random = new Random();
for (int i = 0; i < 4; i++) {
//随机获取字符
//char c = string.charAt((int)(Math.random()*string.length()));
char c = string.charAt(random.nextInt(string.length()));
//拼接
stringBuilder.append(c);
}
//3 打印输出
System.out.println(stringBuilder);
}
}
18.
27.
package cn.baizhi.day14;
import java.util.Random;
public class Demo4 {
public static void main(String[] args) {
/*String string1 = "hello";
String string2 = "hello";
char[] c = {'h','e','l','l','o'};
System.out.println(c);
System.out.println(string2.equals(c));//false
System.out.println(string2.equals(new String(c)));//true
*/
String string = "2345972311184627689275689045782345";
//外层循环 '0'----'9' 基准
for (char i = '0'; i <= '9'; i++) {
//计数器 0
int sum = 0;
//内层循环遍历
for (int j = 0; j < string.length(); j++) {
if (i==string.charAt(j)) {
sum++;
}
}
/*char[] charArray = string.toCharArray();
for (int j = 0; j < charArray.length; j++) {
if (i==charArray[j]) {
sum++;
}
}
*/
//打印输出
System.out.println(i+"在字符串中的个数是:"+sum);
}
}
}
.equals©);//false
System.out.println(string2.equals(new String©));//true
*/
String string = "2345972311184627689275689045782345";
//外层循环 '0'----'9' 基准
for (char i = '0'; i <= '9'; i++) {
//计数器 0
int sum = 0;
//内层循环遍历
for (int j = 0; j < string.length(); j++) {
if (i==string.charAt(j)) {
sum++;
}
}
/*char[] charArray = string.toCharArray();
for (int j = 0; j < charArray.length; j++) {
if (i==charArray[j]) {
sum++;
}
}
*/
//打印输出
System.out.println(i+"在字符串中的个数是:"+sum);
}
}
}