9.常用类

1. 接口中的方法

静态方法: 调用用接口名.方法()

default方法: 默认方法。有方法体的不需要实现(覆盖)

package com.etc.lesson09;

public interface A {
	
	void abc();
	
	public default void a3() {
		System.out.println("=====================");
	}
	
	// 接口中是可以有静态方法的
	public static void a2() {
		System.out.println("----------------------");
	}

}
class B implements A {

	@Override
	public void abc() {
	}

	@Override
	public void a3() {
		System.out.println("==||==============>");
	}
}

public class TestMain1 {

	public static void main(String[] args) {
		A.a2();
		
		A a = new B();
		a.a3();
	}
}

2. java.lang包:

默认包: 不需要import。

3.包装类

包装类型和基本类型可以隐式转换。

场景: 基本类型没有方法。通过包装类来调用。

int a = Integer.parseInt("01110") ;  有可能java.lang.NumberFormatException: For input string: "020a"
Integer b = Integer.valueOf("34234211");  
※ 方法返回值不同
   
    a = b;
	b = a;

// 如何判定变量是 某个基本类型
	isInteger(1); // 1 隐式转换成Integer
	// Object 是所有的类的父类,Integer,
	public static boolean isInteger(Object i) {
		return i instanceof Integer;
	}
   
==  基本类型比较的是值
==  对象比较的是地址
		// new 开空间
		Integer a = 1;
		System.out.println(a == 1);
		
		Integer a1 = 111;
		System.out.println(a1 == 111);
		
		Integer a2 = new Integer(1);
		Integer a3 = new Integer(1);
		System.out.println(a2 == a3);
		System.out.println(a2 == 1);

		Integer a4 = 128;
		Integer a4 = 128;
		System.out.println(128 == 128);

基本类型包装类型
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

4. String类

  • char charAt(int index)

    • 获取下标处字符
  • concat(String str) :

    • 字符串拼接
  • StartWith: 以什么开头

  • endsWith():以什么结尾

  • equals(): 字符串内容判定 ※和==区别

  • equalsIgnoreCase(): 忽略大小写匹配

  • format(Locale l, String format, Object… args) : 句柄

  • getBytes() : 返回字节数组,可以指定 编码UTF-8

  • toCharArray(); 返回字符数组

  • indexOf(String ch) : 返回下标, 不匹配返回-1

    • if (str.indexOf() != -1) {
          
      }
      
  • lastIndexOf() 从后面开始

  • intern() : 获取常量值 相当于String str = “aaa”;

  • static join(): 拼接字符串

  • length() : 长度的方法,字符的个数

    • "hello world".length()==11;   "中国".length() == 2
      
  • matches(String regex) : 匹配正则??

  • replace(charsequence oldChar, charsequence newChar) : 替换字符串

  • replaceAll(String regex, String replacement) : 替换字符串。 第一个参数是regex正则

  • split(String regex) : 按正则 拆分字符串变成 字符串数组

    • "hello world".split("").length() == 11;
      ",a,b,c,".split(",").length == 4; // [,a,b,c]
      
  • substring(startindex, endindex) ; [n, m) ; 从下标处截取, 左闭右开。

  • trim() : 去除前后空格

  • toLowerCase() : 变小写

  • toUpperCase() : 变大写

  • valueOf(boolean b) : 基本类型转字符串类型: 反过来包装类


		
		// 获取下标处字符
		String str = "hello world";
		System.out.println(str.charAt(0));
		
		// 字符串拼接
		String str1 = str.concat("!");
		System.out.println(str1);
		
		// 开始和结束判断
		System.out.println("http://www.baidu.com".startsWith("http://"));
		System.out.println("meinv.gif".endsWith(".gif"));
		
		// Object类的方法 equals 对象类型内容比较
		// 字面量直接赋值
		String str2 = "hello world";
		// 内容相等
		System.out.println(str.equals(str2));
		// 地址相等
		System.out.println(str == str2);
		System.out.println("----------");
		// 🤢 变量+"" 是方法重载 。 返回值一般都有new
		System.out.println(str == str2 + "");
		// 🤢 常量+常量 编译直接结果
		System.out.println(str == "hello world" + ""); // 1 + 1
		
		// ???????? 
		System.out.println(str1 == "hello world!");
		
		String str3 = new String("hello world");
		System.out.println(str.equals(str3));
		System.out.println(str == str3);
		
		// 句柄
		String str4 = String.format("welcome,%s, di %d ci!", new Object[]{"TOM", 2});
		System.out.println(str4);
		
		try {
			// 可以按 编码 把字符串编程 字节数组 : 解决乱码问题
			byte[] bs = "aaaaa".getBytes("UTF-8");
			String sss = new String(bs, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// 存在是下标,不存在是-1
		int index = "hello world".indexOf("o"); // "hello world".indexOf("aasklfa");
		System.out.println(index);
		System.out.println("hello world".lastIndexOf("o"));
		
		String str5 = new String("hello world");
		String str6 = str5.intern(); // 把字符串常量从堆上获取
		System.out.println(str6 == "hello world"); // true
		
		// ...不定个数参数 多存在源碼中
		System.out.println(add(1));
		System.out.println(add(1, 1));
		System.out.println(add(1, 1,3,4,5,4,6,34,42,42));
		System.out.println(add(new int[]{2,23,23,1,131}));
		System.out.println(add()); // 0
		// 数组的定义三种方式;
		// 1. 直接赋值
		int[] ax1 = {1, 3};
		// 2. 标准定义
		int[] ax2 = new int[2]; // {0, 0}  String[] ax3 = new String[2]; {null, null}
		// 3. 传参数简写
		System.out.println(new int[]{1, 3});
		
		// 字符串数组拼接
		String str7 = String.join("@", new String[] {"a", "b", "c"});
		System.out.println(str7);
		// 把字符串拆成  字符串数组  (split参数是正则表达式)
		String[] strs8 = str7.split("@");
		for (String item: strs8) {
			System.out.println(item);
		}
		System.out.println("----------");
		// .是正则的特殊字符  ,  \.  java不认识,再加一个\\.
		String[] strs9 = ".a.b.c.d.".split("\\.");
		System.out.println(strs9.length);
		for (String item: strs9) {
			System.out.println(item);
		}
		System.out.println("----------");
		strs9 = ".a.b.c.d.".split("");
		for (String item: strs9) {
			System.out.println(item);
		}
		
		System.out.println("aa".length()); // 方法
		System.out.println(args.length);   // 属性
		
		String str10 = str.replace("o", "?");
		System.out.println(str10);
		System.out.println(str);
		
		String str11 = str.replaceAll("[ao]", "?"); // 正则
		System.out.println(str11);
		
		String str12 = "    aa   asdfsadf dfasf   ".trim(); // 去除前后空格
		System.out.println(str12);
		
		
	
posted @ 2021-04-10 12:29  剑心空明  阅读(0)  评论(0编辑  收藏  举报  来源