包装类
例子:Byte / Integer / Short / Long / Character / Boolean / Float / Double
Integer常用的属性
Intger:对应的就是int类型
demo:调用类里面的方法,例如调用Integer类里面的方法
通过类名.方法名字
package com.itxs.demo01;
/**
* @Classname : Integer
* @Description : TODO - 对应的是int类型
* @Author : lin_refuel@qq.com
*/
public class INT {
public static void main(String[] args) {
//调用Integer类里面的方法,输出int 类型最大值和最小值
System.out.println("int 最大值是 :" + Integer.MAX_VALUE);
System.out.println("int 最小值是 :" + Integer.MIN_VALUE);
System.out.println("=====================================");
//也可以调用 Long类的方法,输出Long类里面的最大值和最小值
System.out.println("Long 类型最大值是 :" + Long.MAX_VALUE);
System.out.println("Long 类型最小值是 :" + Long.MIN_VALUE);
}
}
运行结果:
Integer对象的获取
将具体的常量包装成一个类,包装成为类后可以直接调用Integer类里面的方法
/**
* @Classname : demo02
* @Description : TODO 将常量封装成为一个包装类对象
* @Author : lin_refuel@qq.com
*/
public class demo02 {
public static void main(String[] args) {
//将常量1314包装成为一个包装类的3个方式
//1.第一种方式
Integer integer = new Integer(1314);
System.out.println(integer);//1314,此时1314已经成为一个类,可以调用Integer里面的方法
System.out.println("=========================");
//2.第二种方式,手动装箱
Integer integer1 = Integer.valueOf(1314);//推荐
System.out.println(integer1);//输出对象integer1,输出1314,同理可以调用Integer里面的方法
System.out.println("=========================");
//3.第三种是自动装箱
Integer integer2 = 1314;//像是定义一个Integer类型对象,把常量1314赋值给这个对象
System.out.println(integer2);//输出1314,同样可以访问Integer里面的方法
System.out.println("=========================");
//使用手动装箱的好处,可以把非整数类型的常量也包装一个Integer类的对象
Integer integer3 = Integer.valueOf("9421");//字符串9421转换成整数进行包装
System.out.println(integer3);// 输出9421,同样可以调用Integer里面方法
}
}
运行结果:
字符串和整数之间的互相转换
int 类型转成为字符串:int to String和 String to int
/**
* @Classname : demo03
* @Description : TODO 字符串和整数之间的转换
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//整数转字符串的三种方式
int num = 9527;
//第一种方式
String s1 = ""+num;//空字符 + 任何东西都会被转为字符串
//length():输出字符串的长度
System.out.println(s1.length());// 4 ,因为9527是4个字符
//第二种方式
//调用Integer里面可以返回字符串的方法,toString(),把num转成字符串进行返回
String s2 = Integer.toString(num);
System.out.println(s2.length()); // 4
//第三种方式,手动装箱,整数转字符串
String s3 = String.valueOf(num);
System.out.println(s3.length());//4
System.out.println("=====================================");
//字符串转整数
//方式一:调用Inter.valueOf,转换成Integer类
Integer i01 = Integer.valueOf("222");
System.out.println(i01 + 1);//222 + 1 = 223
//方式二,调用Inter.parseInt方法,可以把字符串转成整数进行返回
int i02 = Integer.parseInt("222");
System.out.println(i02+1);//222 + 1 = 223
}
}
运行结果:
自动拆箱&装箱
例子:拆箱和装箱
/**
* @Classname : demo04
* @Description : TODO 自动拆箱&装箱
* @Author : lin_refuel@qq.com
*/
public class demo04 {
public static void main(String[] args) {
//把常量9527进行手动装箱,包装成为一个Integer类
Integer integer = Integer.valueOf(9527);//9527被包装成类
System.out.println(integer);//调用这个对象的时候就是拆箱
System.out.println("=============================");
//自动装箱,就是常量直接赋值给Integer类
Integer integer1 = 9527; //9527被自动装箱
System.out.println("integer1拆箱后是:" + integer1);
System.out.println("=============================");
//自动拆箱和自动装箱应用
//等号左边就是把9527+1314的值,自动装箱成为一个Integer类
//等号右边的过程就是拆开箱子拿到9527,然后用9527+1314
integer1 += 1314;// integer1 = integer1 + 1314;
System.out.println(integer1);
System.out.println("=============================");
//自动装箱时,对象里面不能为null(空),通常搭配if进行判断
Integer integer2 = null;
if(null!= integer2)//当integer2里面存在空间时,执行拆箱后+123然后自动装箱
integer2 += 123;
System.out.println(integer2);
}
}
运行结果:
字符类
字符串类
无论是常量还是变量在 Java 中都是字符串对象
字符类特点
方法名 | 说明 |
---|---|
public String() | 创建一个空白字符串对象,不含有任何内容 |
public String(char[] chs) | 根据字节数组内容,来创建字符串对象 |
public String(byte[] bys) | 根据字节数组内容,来创建字符串对象 |
String str = "Hello" | 直接复制的方式创建字符对象,内容就是 Hello |
获取字符串的几种方法
package com.itxs.demo02;
import java.lang.reflect.Array;
import java.util.Arrays;
/**
* @Classname : demo01
* @Description : TODO 获取字符串的几种方式
* @Author : lin_refuel@qq.com
*/
public class demo01 {
public static void main(String[] args){
//获取一个空的字符
String s1 = new String();
System.out.println(s1);//空字符
//通过字符数组的形式
char [] arr = {'j','a','v','a'};
String s2 = new String(arr);
System.out.println(s2);
//通过byte数组的形式
byte [] arr2 = {74,97,118,97};
String s3 = new String(arr2);
System.out.println(s3);
//调用一下字符串.getByte()
byte [] arr3 = "javaSE".getBytes();
String s4 = new String(arr3);
System.out.println(s4);
//直接定义一个字符串的形式
String s5 = "java";
System.out.println(s5);
}
}
运行结果:
字符串之间的比较
package com.itxs.demo02;
import java.util.Scanner;
/**
* @Classname : demo02
* @Description : TODO 字符串之间的比较
* @Author : lin_refuel@qq.com
*/
public class demo02 {
public static void main(String[] args) {
String arr = "hello";
System.out.println("请输入一个字符串");
//建立一个可以输入字符串的对象
String arr2 = new Scanner(System.in).nextLine();
//if(arr == arr2) 比较的是内存地址不是引用数据类型
if(arr.equals(arr2))//调用equals比较的是字符串引用里面存放的内容
System.out.println("两个字符串相等");
else
System.out.println("两个字符串不等");
}
}
运行结果:
字符串常用的方法名字
常用类方法表格
方法名 | 说明 |
---|---|
public char charAt(int index) | 返回给定索引位置的对应的字符 |
public int compareTo(String anotherString) | 比较字符串的大小 |
public String concat(String str) | 字符串连接 |
public boolean equals(Object anObject) | 比较字符串是否相等 |
public byte[] getBytes() | 返回字节数组 |
public int indexOf(int ch) | 查找给定字符的所在位置 |
public int length() | 返 回字符串长度 |
public String[] split(String regex) | 拆分 |
substring(int beginIndex, int endIndex) | 截取 |
public char[] toCharArray() | 返回字符数组 |
trim() | 去掉两端多余的空格 |
public boolean isEmpty() | 判断是否为空串 |
charAt
demo:
/**
* @Classname : demo03
* @Description : TODO charAt
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//charAt可以找到字符串中指定的字符
String arr = "abcdefghijklmnopqrst";
System.out.println(arr.charAt(3));//输出下标为3的字符 d
//可以搭配循环来使用,输出字符串中每个字符
for (int i = 0; i < arr.length(); i++) {
System.out.print(arr.charAt(i) + ",");
}
}
}
运行结果:
compare方法
demo:
import java.util.Scanner;
/**
* @Classname : demo03
* @Description : TODO compare
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//compare方法,可以比较字符串的大小
String arr = "hello";
System.out.println("请输入一个字符串");
String arr2 = new Scanner(System.in).nextLine();//输入字符串对象
//字符串arr如果比arr2大返回正数
//字符串arr如果比arr2小返回负数
//arr和arr2相同返回一个0
System.out.println(arr.compareTo(arr2));
}
}
运行结果:
concat
demo
/**
* @Classname : demo03
* @Description : TODO concat
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//字符串拼接
String arr = "java";
System.out.println(arr.concat("SE"));
}
}
运行结果:
getBytes()
返回字符串里面字符对应码表上的数
/**
* @Classname : demo03
* @Description : TODO getBytes
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
String arr = "java";
//个人理解:字符串里面的每个字符被转成了码表上面的数字,装进了字节数组b里面
byte[] b = arr.getBytes();
System.out.println(b[0]);// 106
// System.out.println(Arrays.toString(b));
//输出字节数组里面的值
for (byte c : b) {
System.out.print(c + ",");
}
}
}
运行结果:
indexOf
demo:
/**
* @Classname : demo03
* @Description : TODO indexOf()
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//indexOf可以找到字符串每个字符的位置
String str = "javaSE";
System.out.println(str.indexOf(83)); // 83对应码表S,S在字符串中的位置应该是4
System.out.println(str.indexOf("E")); // E下标正好是5
}
}
运行结果:
length()
求出一个字符串的长度
demo:
/**
* @Classname : demo03
* @Description : TODO Length()
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//求出字符串的长度
String str = "hello";
System.out.println(str.length());//5
}
}
运行结果:
split()
字符串进行拆开
demo
/**
* @Classname : demo03
* @Description : TODO split()
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//求出字符串拆分
String [] str ="java,SE".split(",");//拆分成两个
//System.out.println(str);
for(String s: str){
System.out.println(s);
}
}
}
运行结果:
toCharArray
demo:
/**
* @Classname : demo03
* @Description : TODO toCharArray()
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//toCharArray,字符串变成了一个字符数组
char[] chars = "abcdefghijklmn".toCharArray();
for(char s : chars){
System.out.print(s + ",");
}
}
}
运行结果:
trim()
demo:
/**
* @Classname : demo03
* @Description : TODO trim
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//删除字符串两端空格
//注意小肆左右两边的空格
String str = " 小肆 ".trim();
System.out.println("***"+str+"***");
}
}
运行结果:
isEmpty()
demo:
/**
* @Classname : demo03
* @Description : TODO isEmpty()
* @Author : lin_refuel@qq.com
*/
public class demo03 {
public static void main(String[] args) {
//空字符也是一个字符
//字符串为空,返回布尔true
System.out.println("".isEmpty());
//字符串不是空的返回布尔 false
System.out.println(" assss".isEmpty());
}
}
运行结果: