Java学习第十三章 之 常用API
1 /* 2 3 Object类是所有类的超类,Java中所有的类都直接或间接的继承这个类 4 5 toString返回当前对象中的内容,对Object类默认的操作来说,返回的对象的类型+@+内存地址值 6 7 equals比较两个对象的内容是否相等,对Object类默认的操作来说,比较的是地址值 8 9 */ 10 11 12 13 14 15 /* 16 17 String字符串类,字符串是常量,创建之后不能改变 18 19 boolean equals(Object obj) 判断俩个字符串中的内容是否相同 20 21 boolean equalsIgnoreCase(String str) 判断俩个字符串中的内容是否相同,忽略大小写 22 23 boolean contains(String str) 判断该字符串中,是否包含给定的字符串 24 25 boolean startsWith(String str) 判断该字符串中,是否以给定的字符串开头 26 27 boolean endsWith(String str) 判断该字符串中,是否以给定的字符串结尾 28 29 boolean isEmpty() 判断该字符串中是否是空串 30 31 int length() 获取字符串的长度 32 33 char charAt(int index) 获取该字符串中指定位置上的字符 34 35 String substring(int start) 从指定位置开始,到末尾结束,截取该字符串,返回新字符串 36 37 String substring(int start,int end) 从指定位置开始,到指定位置结束,截取该字符串,返回新字符串 38 39 40 41 int indexOf(int ch ) 获取给定的字符串,在该字符串中第一次出现的位置 42 43 44 45 int indexOf(String str) 获取给定的字符串,在该字符串中第一次出现的位置 46 47 48 49 int indexOf(int ch,int fromIndex) 从指定位置开始,获取给定的字符,在该字符中第一次出现的位置 50 51 52 53 byte[] getBytes() 把字符串转换成字节 54 55 56 57 char[] toCharArray() 把字符串转换成字符数组 58 59 60 61 String replace(char old,char new) 在字符串中,将给定的旧字符,用新字符替换 62 63 64 65 String replace(String old,String new) 在字符串中,将给定的旧字符串,用新字符串替换 66 67 68 69 String trim() 去除字符串两端的空格,中间的不会去除,返回一个新的字符串 70 71 72 73 String toLowerCase() 把该字符串转换成小写字符串 74 75 76 77 String toUpperCase() 把该字符串转换成大写字符串 78 79 80 81 int indexOf(String str,int fromIndex) 从指定位置开始,获取给定的字符串,在该字符串中第一次出现的位置 82 83 84 85 */ 86 87 88 89 /* 90 91 StringBuffer和StringBuilder字符串缓冲区,是一个可变的字符序列,StringBuilder普遍用在单线程,执行效率比StringBuffer快 92 93 public StringBuffer append(String str) 在原有字符串缓冲区内容基础上,在末尾追加内容 94 95 public StringBuffer insert(int offset,String str) 在原有字符串缓冲区内容基础上,在指定位置上插入新内容 96 97 public StringBuffer deleteCharAt(int index) 在原有字符串缓冲区内容基础上,删除指定位置上的内容 98 99 public StringBuffer delete(int start,int end) 在原有字符串缓冲区内容基础上,删除指定位置上的多个内容 100 101 public StringBuffer replace(int start,int end,String str)在原有字符串缓冲区内容基础上,将指定范围内的多个字符,用给定的字符替换 102 103 public StringBuffer reverse() 将字符串缓冲区的内容反转 "abc"----"cba" 104 105 public String substring(int start) 从指定位置开始,到末尾结束,截取该字符串缓冲区,返回新字符串 106 107 public String substring(int start,int end) 从指定位置开始,到指定位置结束,截取该字符串缓冲区,返回新字符串 108 109 */ 110 111 112 113 114 115 /* 116 117 正则表达式: 118 119 public boolean matches(String regex) 判断字符串是否匹配给定的规则 120 121 public String[] split(String regex) 根据正则表达式匹配的规则,拆分此字符串 122 123 public String replaceAll(String regex , String replement) 将符合规则的字符串内容,替换成新的字符串 124 125 */ 126 127 128 129 /* 130 131 Date 日期/时间类: 132 133 构造方法:public Date() 获取系统时间 public Date(long date) 得到一个1970年1月1日0点这个基础上,加上date毫秒值对应的日期时间 134 135 方法:public long getTime();获取日期所对应的毫秒值 136 137 DateFormat是日期/时间格式化子类的抽象类,使用其子类SimpleDateFormat实例化 138 139 构造方法:public SimpleDateFormat()默认的格式化操作 140 141 public SimpleDateFormat(String pattern) 按照指定的格式,进行日期格式化 142 143 方法:public final String format(Date date) 把日期格式成字符串 144 145 public Date parse(String source) 把日期字符串 转成 日期对象 146 147 */ 148 149 150 151 /* 152 153 Calendar日历类,可以获取日历中指定的字段的值 154 155 方法:public static Calendar getInstance() 获取日期对象 156 157 public int get(int filed) 获取时间字段值 158 159 public void add(int filed , int amoubt) 指定字段添加某值 160 161 public void set(int filed , int value) 设置指定字段的值 162 163 public final Date getTime() 获取日历对象转成日期对象 164 165 */ 166 167 168 169 /* 170 171 基本类型包装类: 172 173 基本类型: byte short int long char boolean float double 174 175 包装类型: Byte Short integer Long Character Boolean Float Double 176 177 拆装箱: 178 179 自动装箱:基本数值转对象(int --> Integer) 180 181 自动拆箱:对象转基本数值(Integer --> int) 182 183 方法:public int parseInt(String str) 字符串转基本类型 184 185 public static String toString(int x) 把基本类型int 转字符串 186 187 public static Integer valueOf(int x) 把基本类型x字符串 转Integer对象 188 189 public int intValue() 以int 类型返回成包装类对象的值 190 191 */ 192 193 194 195 /* 196 197 System:系统属性信息工具类 198 199 方法:public static long currentTimeMilis()获取当前系统时间与1970年1月1日0点的毫秒值差 200 201 public static void exit()用来结束Java程序运行, 参数传入一个数字, 用0来表示正常状态, 其他数字表示异常 202 203 public static void gc()用来回收JVM中的垃圾回收器,完成内存中垃圾的清楚 204 205 public static String getProperties() 获取系统属性的值 206 207 public static void arraycopy(Object src , int ,srcPos , Object dest , int destPos , int length)从源数组中赋值一个数组,从指定位置开始,到目标数组的指定位置结束 208 209 */ 210 211 212 213 /* 214 215 Arrays数组工具类: 216 217 方法:public static void sort(int[] a)对数组进行排序 218 219 public static String toString(int[] a)返回指定数组元素内容中的字符串形式 220 221 public static void binarySearch(int[] a , int key) 在指定元素中,查找元素值出现的位置,若没找到,返回位置为-1,数组必须是有序的数组 222 223 */ 224 225 226 227 /* 228 229 Math基本数学运算的方法的数学工具类 230 231 方法:public static double abs(double a)返回double值的绝对值 232 233 public static double ceil(double a)返回比参数大的最小的整数的double值 234 235 public static double floor(double a)返回比参数小的最大的整数的double值 236 237 public static double max(doule a , double b)返回两个参数中较大的一个 238 239 public static double min(double a , double b)返回两个参数中较小的一个 240 241 public static double pow(double a , double b)返回第一个参数的第二个参数次幂的值 242 243 public static double random()返回带正号的double值,该值大于等于0.0且小于1.0 244 245 public static double round(double a)返回参数值的四舍五入的结果 246 247 */