21天学通Java学习笔记-Day11(常用类)
来源:互联网 发布:linux内核开发教程 编辑:程序博客网 时间:2024/06/01 10:42
java 常用类(重点):
String 类:
String 类代表字符串。创建以后不能更变。
public class tests { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; System.out.println(s1==s2);//true,指向String池中的同一块内存s1 = new String("hello"); s2 = new String("hello"); System.out.println(s1==s2); //false,两个不同的对象 System.out.println(s1.equals(s2)); //true,String类重写了Object类的equals方法,内容相同就相等 char[] c = {'s','u','n',' ','j','a','v','a'}; s1 = new String(c); //将一个字符数组赋值给一个字符串 s2 = new String(c,4,4); //截取字符数组的一部分赋值给字符串(从c中第4个字符开始截取4个) System.out.println(s1); //sun java System.out.println(s2); //java }}
String 类常用方法:
char chasrAt( int index )返回字符串中第index个字符( 字符串实际是一个字符数组)
int length( ) 返回字符串长度
int indexOf( String str, int fromIndex ) 返回字符串从 fromIndex 开始出现 str 的第一个位置
boolean equalsIgnoreCasr( String another ) 比较字符串与 another 是否一样(忽略大小写)
String replace( charoldChar, char newChar ) 在字符串中用 newChar 字符替换 oldChar 字符
boolean startsWith( String prefix ) 判断字符串是否以 prefix 字符串开头
boolean endsWith( String suffix ) 判断字符串是否以 suffix 字符串结尾
String toUpperCase( ) 返回字符串大写形式
String toLowerCase( ) 返回字符串小写形式
String substring( int beginIndex ) 返回该字符串从 beginIndex 开始到结尾的字符串
String substring( int beginIndex, int endIndex ) 返回该字符串从 beginIndex 开始到 endIndex 结尾的子字符串
String trim( ) 返回该字符串去掉开头和结尾空格的字符串
String value Of( ...... ) 可以将基本类型(对象类型)装换为字符串类型(相当于调用toString( )方法)
String[ ] split( String regex ) 可以将一个字符串按照指定的分隔符 regex 分隔成一个个子串,放进一个字符串数组中
StringBuffer 类:
StringBuffer 类代表可变字符序列。
StringBuffer 常用方法:
StringBuffer append( ...... ) 参数可以是基本类型,String,StringBuffer,char数组,对象类型,可以为该 StringBuffer 对象添加字符串序列,返回添加后的该StringBuffer对象引用
StringBuffer insert( int offset , ...... ) 参数可以是基本类型,String,StringBuffer,char数组,对象类型,可以为该 StringBuffer 对象在指定位置插入字符序列,返回修改后的该StringBuffer对象引用
Stringuffer delete( int start , int end ) 可以删除 start 开始到 end-1 为止的一段字符序列,返回修改后的该 StringBuffer 对象引用
StringBuffer reverse( ) 将字符序列逆序排列
枚举类 :
java.lang.Enum 枚举类型
使用关键字 enum
public class TestEnum { public enum MyColor { red, green, blue }; public enum MyDoorOpener { me, mywife }; public static void main(String[] args) { MyColor m = MyColor.red; switch(m) { case red: System.out.println("red"); break; case green: System.out.println("green"); break; default: System.out.println("default"); } System.out.println(m); }}
日期类:
格式化日期:
import java.text.*;import java.util.*;public class testd { public static void main(String[] args)throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); //以这种模式格式化日期 Date d = new Date(); //获得当前时间 System.out.println(sdf.format(d)); //将日期以指定模式输出 }}
System.currentTimeMillis( ) 返回从1970年1月1日零时开始到当前以毫秒为单位的时间。
将String类型转换为日期类型:
import java.sql.*;public class testd { public static void main(String[] args)throws Exception { String s = "1990-11-12 08:24:27"; Timestamp ts = Timestamp.valueOf(s); System.out.println(ts); }}
正则表达式:
用途:1. 字符串匹配(字符匹配);2. 字符串查找;3. 字符串替换
类:1. java.lang.String;2. java.util.regex.Pattern ; 3. java.util.regex.Matcher
java.lang.String 类中与正则表达式相关的方法:
boolean matches( String regex ) 告知这个字符串是否匹配给定的正则表达式。
String replaceAll( String regex , String replacement ) 使用给定的 replacement 替换这个字符串匹配给定的正则表达式的子字符串。
String replaceFirst( String regex , String replacement ) 使用给定的 replacement 替换这个字符串匹配给定的正则表达式的第一个子字符串。
String[ ] split( String regex ) 根据给定正则表达式的匹配拆分此字符串。
String[ ] split( String regex , int limit ) 根据匹配给定的正则表达式来拆分这个字符串。
public class testd { public static void main(String[] args)throws Exception { System.out.println("abc".matches("...")); //true, .代表任何字符 System.out.println("a56t879a".replaceAll("\\d","-")); //a--t--a, \\d 代表一个数字 }}
import java.util.regex.*;public class testd { public static void main(String[] args)throws Exception { Pattern p = Pattern.compile("[a-z]{3}"); //compile()方法:返回在其中编译过此模式的正则表达式。 Matcher m = p.matcher("fgh"); //matcher()方法:创建匹配给定输入与此模式的匹配器。 System.out.println(m.matches()); //matches()方法:尝试将整个区域与模式匹配,当且仅当整个区域序列匹配此匹配器的模式时才返回 true。 }}
关于 . * + ? 的例子代码:
import java.util.regex.*;public class testd { public static void main(String[] args)throws Exception { print("a".matches(".")); //. 所有字符 print("aa".matches("a*")); //* a出现零次或者多次 print("aaa".matches("a+")); //+ a出现一次或者多次 print("a".matches("a?")); //? a出现零次或者一次 print("".matches("a*")); //* a出现零次或者多次 print("".matches("a?")); //? a出现零次或者一次 print("2134567898767895".matches("\\d{3,20}")); // \\d{ ,} 数字出现最少3次,最多20次 print("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\D{1,3}")); // \\. 代表点,\\D 代表非数字 print("192".matches("[0-2][0-9][0-9]")); // [ ] 范围 ,一个[]只匹配一个字符 } public static void print(Object b){ System.out.println(b); }}
关于[ ] 例子代码:
import java.util.regex.*;public class testd { public static void main(String[] args)throws Exception { print("a".matches("[abc]")); //范围 abc print("a".matches("[^abc]")); //范围 非abc,^ 为取反 print("A".matches("[a-z]|[A-Z]")); //范围 a-z或者A-Z print("A".matches("[a-z[A-Z]]")); //范围 a-z或者A-Z print("R".matches("[A-Z&&[RFG]]")); //范围 A-Z 并且在 RFG 中 }public static void print(Object b){ System.out.println(b); }}
关于 \\d , \\D , \\s , \\S , \\w , \\W
import java.util.regex.*;public class testd { public static void main(String[] args)throws Exception { print(" ".matches("\\s")); // \\s 表示空字符 print("a".matches("\\S")); // \\S 非空字符 print("5w".matches("\\w{2}")); // \\w 单词字符(范围[a-zA-Z_0-9]字母加下划线加数字) {3} 正好出现三次 print("1qazxsw2".matches("\\W{2,7}")); // \\W 非单词 {2 , 7}最少出现2次,最多出现7次 print("R".matches("\\D")); // \\D 非数字 print("3".matches("\\d")); // \\d 数字 print("\\".matches("\\\\")); // 匹配一个反斜杠"\\" , 需要"\\\\" }public static void print(Object b){ System.out.println(b); }}
关于边界符 ^ , $ , \\b 例子代码:
import java.util.regex.*;public class testd { public static void main(String[] args)throws Exception { print("hello world".matches("^he.*")); // ^ 表示以什么开头 print("hello java".matches(".*va$")); // $ 表示以什么结尾 print("hello sir".matches("^h[a-z]{1,3}o\\b.*r$")); // \\b 表示单词边界(空格,换行等符号) }public static void print(Object b){ System.out.println(b); }}
邮箱匹配的正则表达式:
import java.util.regex.*; public class testd { public static void main(String[] args)throws Exception {String email1 = "show_123@126.com";String email2 = "454768978@qq.com.cn";print(email1.matches("[\\w[.-]]+@[\\w[.-]]+.[\\w]+"));print(email2.matches("[\\w[.-]]+@[\\w[.-]]+.[\\w]+"));} public static void print(Object b){System.out.println(b);}}
分组:
import java.util.regex.*;public class testd { public static void main(String[] args)throws Exception { Pattern p = Pattern.compile("(\\d{1,5})([a-z]{3})"); //用小括号表示分组,一个小括号表示一个分组,根据左小括号表示是第几个分组 String s = "123aaa-4567bbb-89ccc-000"; Matcher m = p.matcher(s); while(m.find()) { //find() 尝试查找与该模式匹配的输入序列的下一个子序列。 print(m.group(1)); //group() 返回由以前匹配操作所匹配的输入子序列。 0 代表原来的字符,1代表第一个分组 } }public static void print(Object b){ System.out.println(b); }}
反射机制:
ClassLoader 的类加载机制
并非一次性加载,而是需要的时候加载(运行期间动态加载)
static 语句块在加载后执行一次
dynamic (动态)语句块每次 new 新的对象都会执行(等同于构造方法中的语句,用的较少)
public class testd { public static void main(String[] args){ new A(); System.out.println("-------"); new B(); //new 出来才会加载 new C(); new C(); //static 内容只会加载一次 new D(); new D(); //动态语句每次都会加载 }}class A{}class B{}class C{ static { System.out.println("CCCCCCC"); }}class D{ { System.out.println("DDDDDD"); }}
public class testd { public static void main(String[] args)throws Exception{ String s = "T"; Class c = Class.forName(s); // forName( )回与带有给定字符串名的类或接口相关联的 Class 对象 c.newInstance(); // newInstance( ) 创建此 Class 对象所表示的类的一个新实例。 }}class T{ static { System.out.println("T loaded"); }public T() { System.out.println("T constructed!"); }}
- 21天学通Java学习笔记-Day11(常用类)
- 传智播客-Java学习笔记day11
- 毕向东Java视频学习笔记【Day11 异常+object类】
- 黑马程序员 Java学习笔记 (day11)
- C++ 学习笔记day11
- Android学习笔记day11
- java笔记--day11--类object之toString()
- java笔记--day11--类object之equals()
- [Java视频笔记]day11
- Java基础day11笔记
- java基础学习day11
- java学习day11
- java学习day11
- java学习day11
- 机器学习学习笔记.day11
- 黑马程序员 java学习笔记 Day11:反射机制详解
- 毕向东Java视频学习笔记【Day11-Day13 多线程】
- 毕向东Java视频学习笔记【Day11-Day13 多线程】
- hadoop 集群安装
- 哈夫曼树以及哈夫曼编码的实现
- 21天学通Java学习笔记-Day10(网路编程)
- MVC,MVP,MVVM
- mysql增删改查语句
- 21天学通Java学习笔记-Day11(常用类)
- 漫谈linux文件IO--io流程讲的很清楚
- java nio 简介
- (Android 基础知识review)打电话
- android学习笔记--->intent 之action
- 从带人看管理的两面
- 逃离迷宫-----广搜+dp
- magento 开启 template path hints
- Zstack之RSSI信号值获取以及显示