java基础笔记(5)基本数据类型的包装类,String 类

1.基本数据类型的包装类

  • Byte
  • Short
  • Integer
  • Long
  • Character
  • Float
  • Double
  • Boolean
    将基本数据类型,放到包装类的过程, 装箱–> 拥有了很多新增的功能
    int --> Integer
    将包装类 转成 基本数据类型的过程, 拆箱 —> 回归到基本数据类型
    Integer --> int
    JDK 1.5以前需要手动装箱和拆箱
  1. 手动装箱XXXValue()
  2. 重载方法, 要求参数字符串中只能包含数字 包含其他的就会发生NumberFormatException异常
    JDK1.5以后 可以自动装箱和拆箱
public class Test {
    
    
	public static void main(String[] args) {
    
    
		int i = 3;
		System.out.println(Integer.MIN_VALUE);	
		  // 1. 手动装箱
		Integer integer = Integer.valueOf(7);
		Integer integer2 = Integer.valueOf("123");
		System.out.println(integer2);		
		Integer integer3 = new Integer(6);		
		//手动拆箱
		int i4 = integer.intValue();
		System.out.println(i4);
		byte b = integer2.byteValue();
		  short s = integer2.shortValue();
		  long l = integer2.longValue();
		  float f = integer2.floatValue();
		  double d = integer2.doubleValue();	
		  // 自动装箱	  		  
		  Integer integer4 = 5;
		  System.out.println(integer4);
		  //自动拆箱
		  int i2 = integer4;
		  System.out.println(i2);		  
		  Byte b2 = 3;
		  Double double1 = 4.5;	  
		  byte b3 = b2;
	}
}

static boolean isLetter(char ch)
确定指定字符是否为字母
isLowerCase(char ch)
确定指定字符是否为小写字母。
isUpperCase(char ch)
确定指定字符是否为大写字母。
static char toLowerCase(char ch)
使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为小写。
static char toUpperCase(char ch)
使用取自 UnicodeData 文件的大小写映射信息将字符参数转换为大写

public class Test3 {
    
    
	public static void main(String[] args) {
    
    
		Character c = '2';
		char c2 = c;		
		// 字母: 各个国家常用的文字		
		System.out.println("是否是字母"+Character.isLetter('好'));		
		System.out.println("判断是否是大写"+Character.isUpperCase('a'));
		System.out.println("判断是否是小写"+Character.isLowerCase('C'));		
		System.out.println("转成大写"+Character.toUpperCase('a'));
		System.out.println("转成小写"+Character.toLowerCase('B'));
	}
}

2.String 最终类,
内容是不允许修改的, 一旦修改就是会产生新的对象
String 实质上其实是是利用字符数组帮助存储数据的

import java.util.Arrays;
public class Test {
    
    
	public static void main(String[] args) {
    
    
		String str = "abc";
		String str2 = new String("abc");
		// 有很多方法是利用了下标的
		// StringIndexOutOfBoundsException 字符串下标越界
		char c = str.charAt(0);
		System.out.println(c);		
		//将字符串转成数组
		char[] cs = str.toCharArray();
		System.out.println(Arrays.toString(cs));		
		byte[] bytes = str.getBytes();
		System.out.println(Arrays.toString(bytes));		
	//  将数组换成 字符串
		byte[] bs = new byte[]{
    
    98,99,100};
		String str3 = new String(bs);
		System.out.println(str3);
		String str4 = new String(bs, 1, 2);// offset 从下标为几开始转, length 转几个    下标+length<=数组的长度
		System.out.println(str4);		
		char[] cs2 = new char[]{
    
    'a','b','c'};
		String string = new String(cs2, 1, 2);
		System.out.println(string);		
		String string2 = new String();
		System.out.println(string2);// ""
	}
}

判断方法
equals 判断两个字符串内容是否相同
contains 判断字符串中是否包含指定的字符串
startsWith 字符串是否以指定的字符串开头
endsWith 字符串是否以指定的字符串结尾

public class Test2 {
    
    
	public static void main(String[] args) {
    
    
		String string = "a"+"b";
		String string2 = string.concat("cdeccc");// 拼接字符串
		System.out.println(string2);		
		// 判断方法
		// equals  判断两个字符串内容是否相同
		// contains 判断字符串中是否包含指定的字符串
		boolean b = string2.contains("adb");
		System.out.println(b);
		// startsWith  字符串是否以指定的字符串开头
		boolean b2 = string2.startsWith("abcde");
		System.out.println(b2);
		// endsWith     字符串是否以指定的字符串结尾
		boolean b3 = string2.endsWith("cde");
		System.out.println(b3);		
		// 查找参数在调用者字符串中第一次出现的下标
		int index = string2.indexOf("c");
		System.out.println(index);
		 查找参数在调用者字符串中最后一次出现的下标
		int index2 = string2.lastIndexOf("c");
		System.out.println(index2);		
		// 转成大写
		String upperCase = string2.toUpperCase();
		System.out.println(upperCase);
		// 转成小写
		String lowerCase = upperCase.toLowerCase();
		System.out.println(lowerCase);		
		// length 得到字符串的长度
		System.out.println(lowerCase.length());		
		// 去除字符串中前边和后面空白
		lowerCase = "  a b  c  ";
		String string3 = lowerCase.trim();
		System.out.println(string3);
	}
}

boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。
String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
String replaceFirst(String regex, String replacement)
使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串
String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。

public class Test3 {
    
    
	public static void main(String[] args) {
    
    
		String  str = "abcaaabdaabaa#def#ff#hhh";
		String string = str.replace("#", "?");//replace("#", "?"); 将所有符合条件的字符串全部替换, 不支持正则表达式
		System.out.println(string);		
		// 只替换第一个符合正则表达式的部分
		String first = str.replaceFirst("[abcdef]+", "?");
		System.out.println(first);
		// 替换所有符合正则表达式的部分
		String all = str.replaceAll("[abcdef]+", "?");
		System.out.println(all);		
		// 字母 数字 下划线     6 ~8位
		// 手机号  11位数
		// 身份证号 18位 
		String string2 = "aaaa";
		boolean matches = string2.matches("[a]+");
		System.out.println(matches);		
		// subString 截取
		String string3 = "abcdefg";
	}
}

猜你喜欢

转载自blog.csdn.net/Echoxxxxx/article/details/112444735