黑马程序员——基本数据类型和字符串类型

来源:互联网 发布:共产 革命 知乎 编辑:程序博客网 时间:2024/06/05 04:36

                                                                   --------------------- android培训、java培训、期待与您交流! ----------------------



一丶基本数据类型

1、

package study_note_13;/* *1.基本数据类型对象包装类  *|--byte------Byte *|--short-----short *|--int-------Integer *|--long------Long *|--boolean---Boolean *|--float-----Float *|--double----Double *|--char------Character *2.基本数据类型对象包装类的最常见作用就是用于基本数据类型和字符串类型之间的转换 *|--基本数据类型转成字符串 *|--基本数据类型+"" *|--基本数据类型.toString(基本数据类型值):Integer.toString(34)将34整数变成"34"字符串 *|--字符串转成基本数据类型 *|--int parseInt(String s):这是静态调用方式 *|--Integer i=new Integer("")--int num=i.intValue():这是对象调用方式 *3.进制之间的转换 *|--十进制转成其他进制 *|--toBinaryString() *|--toHexString() *|--toOctalString() *|--其他进制转成十进制 *|--parseInt(String s,int radix) */public class IntegerDemo{public static void main(String[] args){// 整数类型的最大值// sop("int max:" + Integer.MAX_VALUE);// 字符串转成整数// int num=Integer.parseInt("123");//必须传入数字格式的字符串// long x=Long.parseLong("123");// sop("num="+(num+4));// sop("x="+(x+4));// sop(Integer.toBinaryString(-6));// sop(Integer.toHexString(60));int x = Integer.parseInt("110", 2);// "110"是字符串,不是数字,二进制中只有0和1,所以"xxx"不能含有0和1以外的数字sop("x=" + x);}public static void sop(String str){System.out.println(str);}}
2、
/* * JDK1.5以后出现的新特性 */public class IntegerDemo1{public static void main(String[] args){// Integer x=new Integer("123");// Integer y=new Integer(123);// sop("x==y:"+(x==y));// sop("x.equals(y):"+x.equals(y));Integer x = 4;// 自动装箱,相当于new Integer(4),可以为null所以别忘记判断是否为nullx = x/* x.intValue() 拆箱原理 */+ 2;// x+2:x进行了自动拆箱,变成了int类型和2进行加法运算,再将和进行装箱赋给xInteger m = 128;Integer n = 128;sop("m==n:" + (m == n));Integer a = 127;// 127是byte的临界点:-128~127Integer b = 127;// 结果为true,因为a和b指向了同一个Integer对象// 当数值再byte范围内时,对于新特性,如果该数值已经存在,则不会开辟新空间sop("a==b:" + (a == b));}public static void sop(String str){System.out.println(str);}

二丶字符串类型

1、String

package study_note_13;/* * String类适用于描述字符串事物。 * 它提供了多个方法对字符串进行操作 * 常见的操作有: * 1.获取: * |--字符串的长度:int length() * |--获取某个位置上的字符:char charAt(int index) * |--获取每个字符的位置:int indexOf(int ch)返回的是ch在字符串中第一次出现的位置 * |--从fromIndex指定位置开始,获取ch在字符串中出现的位置:int indexOf(int ch,int fromIndex) * |--int indexOf(String str):返回的是str在字符串中第一次出现的位置 * |--int indexOf(String str,int fromIndex):从fromIndex指定位置开始,获取str在字符串中出现的位置 * |--int lastIndexOf(int ch):反向索引 * 2.判断 * |--字符串中是否包含某一个子串:boolean contains(str) * |--字符串中是否有内容:boolean isEmpty(),就是判断长度是否为0 * |--字符串是否是以指定内容开头:boolean startsWith(str) * |--字符串是否是以指定内容结尾:boolean endsWith(str) * |--判断字符串内容是否相同:boolean equals(str)复写了Object类中的equals方法 * |--判断字符串内容是否相同并忽略大小写:boolean equalsIgnoreCase(); * 3.转换 * |--将字符数组转成字符串 * 构造函数 * |--String(char[])和String(char[],offset,count):将字符数组中的一部分转成字符串 * 静态方法 * |--static String copyValueOf(char[]) * |--static String copyValueOf(char[],offset,count) * |--static String valueOf(char[]) * |--将字符串转成字符数组 * |--char[] toCharArray() * |--将字节数组转成字符串 * |--String(byte[])和String(byte[],offset,count) * |--将字符串转成字节数组 * |--byte[] getBytes() * |--将基本数据类型转成字符串 * |--static String valueOf(int),static String valueOf(double)...... * 4.替换 * |--Stirng replace(oldchar,newchar); * 5.切割 * |--String[] split(regex) * 6.子串。获取字符串中的一部分 * |--String substring(begin) * |--String substring(begin,end) * 7.转换,去除空格,比较 * |--将字符串转成大写或者小写 * |--String toUpperCase() * |--String toLowerCase() * |--将字符串两端的空格去除 * |--String trim() * |--对两个字符串进行自然书序的比较 * |--int compareTo(string)  */public class StringMethodDemo{public static void method_ctc(){String s="  Hello Java  ";sop(s.toLowerCase());sop(s.toUpperCase());sop(s.trim());String s1="abc";String s2="aaa";sop(s1.compareTo(s2));}public static void method_sub(){String s="abcdef";sop(s.substring(2));//如果角标不存在,会出现字符串角标越界异常sop(s.substring(2,4));//包含头,不包含尾}public static void method_split(){String s="zhangsan,lisi,wangwu";String[] arr=s.split(",");for(int x=0;x<arr.length;x++){sop(arr[x]);}}public static void method_replace(){String s = "Hello java";//String s1 = s.replace('q', 'n');//如果要替换的字符不存在,返回的还是原来的字符串String s1=s.replace("java", "world");sop("s=" + s);sop("s1=" + s1);}public static void method_trans(){char arr[] ={ 'a', 'b', 'c', 'd', 'e', 'f' };String s = new String(arr, 1, 3);sop("s=" + s);String s1 = "zxcvbnm";char[] chs = s1.toCharArray();for (int x = 0; x < chs.length; x++){sop("ch=" + chs[x]);}}public static void method_is(){String str = "ArrayDemo.java";// 判断文件名称是否是Array单词开头sop(str.startsWith("Array"));// 判断文件名称是否是.java文件sop(str.endsWith(".java"));// 判断文件名是否含有Demosop(str.contains("Demo"));}public static void method_get(){String str = "abcdeakpf";// 长度sop(str.length());// 根据索引获取字符sop(str.charAt(4));// 当访问到字符串中不存在的角标时会发生StringIndexOutOfBoundException(字符串角标异常)// 根据字符获取索引sop(str.indexOf('m', 3));// 如果没有找到返回-1,所以可以用来判断字符串中是否含有制定的字符相当于contains方法// 反向索引一个字符出现的位置sop(str.lastIndexOf('a'));}public static void main(String[] args){ method_ctc();//method_sub();//method_split();//method_replace();// method_trans();// method_get();// method_is();/* * String s1 = "abc"; String s2 = new String("abc"); String s3 = "abc"; * System.out.println(s1 == s2); System.out.println(s1 == s3); */}public static void sop(Object obj){System.out.println(obj);}}
2、StringBuffer
package study_note_13;/* * 1.StringBuffer是字符串缓冲区,是一个容器.  * |--长度是可变化的 * |--可以字节码操作多个数据类型 * |--最终会通过toString方法变成字符串 * 2.功能 * |--存储:create * |--StringBuffer append():将指定数据作为参数添加到已有数据的结尾处 * |--StringBuffer insert(index,数据):可以将数据插入到指定index位置上 * |--删除:delete * |--StringBuffer delete(start,end):删除缓冲区的数据,包含start,不包含end * |--StringBuffer deleteCharAt(index):删除指定位置的字符 * |--获取:read * |--char charAt(int index) * |--int indexOf(String str) * |--int lastIndexOf(String str) * |--int length() * |--String substring(int start,int end) * |--修改:update * |--StringBuffer replace(int start,int end,String str) * |--void setCharAt(int index,char ch) * |--反转:reverse * |--StringBuffer reverse() * |--将缓冲区中指定数据存储到指定字符数组中 * |--void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin) * 3.JDK1.5版本之后出现StringBuffer * |--StringBuffer是线程同步(一般多线程使用) * |--StringBuilder是线程不同步(一般单线程使用,建议使用) * 4.java升级三因素 * |--提高效率 * |--简化代码 * |--提高安全性 */public class StringBufferDemo{public static void main(String[] args){method_add();method_del();method_update();method_set();}public static void method_set(){StringBuffer sb = new StringBuffer("abcdef");char[] chs = new char[6];sb.getChars(1, 4, chs, 1);for (int x = 0; x < chs.length; x++){sop("chs[" + x + "]=" + chs[x] + ";");}}public static void method_update(){StringBuffer sb = new StringBuffer("abcde");// sb.replace(1, 4, "java");sb.setCharAt(2, 'k');sop(sb.toString());}public static void method_del(){StringBuffer sb = new StringBuffer("abcde");// sb.delete(1, 3);// 清空缓冲区// sb.delete(0, sb.length());sb.deleteCharAt(2);sop(sb.toString());}public static void method_add(){StringBuffer sb = new StringBuffer();sb.append("abc").append(true).append(34);// 方法调用链sb.insert(1, "qq");// StringBuffer sb1=sb.append(34);// sop("sb==sb1:"+(sb==sb1));sop(sb.toString());// sop(sb1.toString());}public static void sop(String str){System.out.println(str);}}




                                                                   --------------------- android培训、java培训、期待与您交流! ----------------------

                                                                                                详细请查看:http://edu.csdn.net/heima