资源描述
,NCCP 2.0,NCCP 2.0,NCCP 2.0,NCCP 2.0,NCCP 2.0,NCCP 2.0,NCCP 2.0,JAVASE,java.lang,中的常用类,本章学习目标,了解,java.lang,包,掌握包装类,掌握,String,和,StringBuffer,类,运用类,Math,、,Class,和,Object,类,包装类简介,包装类,-1,int num=10;,原始数,据类型,原始数据类型声明的变量,对象,不是对象,是对象,怎么变,成对象,java.lang,提供,原始数据类型,包装类,包装类,-2,包装类,-3,public class LangDemo,public static void main(String args),Boolean objBool=new Boolean(true);,Character objChar=new Character(X);,Integer objInt=new Integer(100);,Long objLong=new Long(2568);,Double objDou=new Double(3.1415);,System.out.println(objBool);,System.out.println(objChar);,System.out.println(objInt);,System.out.println(objLong);,System.out.println(objDou);,注:,包装类的静态方法,valueOf(),,用于将字符串转成相应包装类的对象。,public class LangDemo,public static void main(String args),String str=128;,/,如果转换失败,将会引发,NumberFormatException,异常,/,引发失败的原因:,1,。数值超过范围,2.,字符串不是表示数值,Byte objByte=Byte.valueOf(str);,Short objShort=Short.valueOf(str);,Integer objInt=Integer.valueOf(str);,Long objLong=Long.valueOf(str);,System.out.println(objByte);,System.out.println(objShort);,System.out.println(objInt);,System.out.println(objLong);,注:,除了,Boolean,和,Character,类以外,其他的包装类都有静态的,parseXXX(),方法。(,XXX,指代具体的数据类型),public class ParseTest,public static void main(String args),String str=116;,int i=Integer.parseInt(str);,short s=Short.parseShort(str);,byte b=Byte.parseByte(str);,long l=Long.parseLong(str);,float f=Float.parseFloat(str);,double d=Double.parseDouble(str);,System.out.println(i);,System.out.println(s);,System.out.println(b);,System.out.println(l);,System.out.println(f);,System.out.println(d);,范例演示,包装类,-4,包装类,-5,public class CharacterDemo,public static void main(String args),char charArray=*,7,b,A;,for(int i=0;i charArray.length;i+),if(Character.isDigit(charArrayi),System.out.println(charArrayi+,是一个数字。,);,if(Character.isLetter(charArrayi),System.out.println(charArrayi+,是一个字母。,);,if(Character.isWhitespace(charArrayi),System.out.println(charArrayi+,是一个空格。,);,if(Character.isLowerCase(charArrayi),System.out.println(charArrayi+,是小写形式。,);,if(Character.isUpperCase(charArrayi),System.out.println(charArrayi+,是大写形式。,);,范例演示,String,类,String,类,字符串字面量,对象,未修改的原始字符串,使用,String,类的方法,可以更改字符串版本,原始字符串保持不变,String,类,构造方法,范例演示,public class StringDemo,public static void main(String args),char aryChar=N,e,w,F,u,t,u,r,e;,String str1=NewFuture;,/,利用一个字符串常量值创建新的字符串,String str2=new String(NewFuture);,/,利用一个字符型数组创建新的字符串,String str3=new String(aryChar);,System.out.println(str1);,System.out.println(str2);,System.out.println(str3);,Storage,String pool&stack&heap,String pool,(,字符串池,),:JVM has a String pool,it saves a lot of String objects that created by shorthand,initializer,.Data sharing,Stack,(栈),:Store primitive data,(,char,、,byte,、,short,、,int,、,long,、,float,、,double,、,boolean,),and constructor.Data sharing.,heap,(堆),:,Store Object.,Constructing Strings,Format:String,newString,=new,String(stringLiteral,);,String message=“,stringLiteral,;,Example:String message=new String(“Hello);,String message=Welcome to Java;,String,str,=new,String(abc,);,Question:How many String objects are created?,Two,Constructor:,publicString(Stringoriginal),/othercode.,Note:The,parameter is a String object,Stringa=,abc,;,String b=,abc,;,Difference between new and shorthand initializer,Question:How many String objects are created?,One,String,类,字符串长度,String str=“,John Smith,”,长度是?,使用length()方法获取,哦,str的长度是“10”,public class StringDemo,public static void main(String args),String str1=John Smith;,String str2=new String(I Love Java);,System.out.println(str1.length();,System.out.println(str2.length();,String,类,字符串比较,1,字符串,1,字符串,2,由,equals(),方法确定,同一个对象,用,=,运算符检查,检查字符串是否指向同一个或不同的对象,检查组成字符串内容的字符,public class StringDemo,public static void main(String args),public static void main(String args),String str1=,学习新技术把握新未来,;,String str2=,学习新技术把握新未来,;,String str3=new String(,学习新技术把握新未来,);,String str4=new String(,学习新技术把握新未来,);,if(str1=str2),System.out.println(str1,和,str2,指向同一字符串,);,else,System.out.println(str1,和,str2,分别指向不同字符串,);,if(str1.equals(str2),System.out.println(str1,和,str2,的内容完全相同,);,else,System.out.println(str1,和,str2,的内容不相同,);,if(str3=str4),System.out.println(str3,和,str4,指向同一字符串,);,else,System.out.println(str3,和,str4,分别指向不同字符串,);,if(str3.equals(str4),System.out.println(str3,和,str4,的内容完全相同,);,else,System.out.println(str3,和,str4,的内容不相同,);,String,类,-,字符串比较,2,String,类,字符串搜索,public class StringDemo,public static void main(String args),String strEmail=;,int index;,System.out.println(E-mail,地址:,+strEmail);,index=strEmail.indexOf();,(,字符出现的索引:,+index);,index=strEmail.indexOf(sun);,(,字符串,sun,出现的索引:,+index);,index=strEmail.lastIndexOf(a);,System.out.println(a,字符最后一次出现的索引:,+index);,String,类,字符串提取,public class StringDemo,public static void main(String args),String str1=Java is OOP;,String str2=new String(NewFuture);,System.out.println(str1.charAt(2);,System.out.println(str1.substring(5);,System.out.println(str1.substring(2,9);,System.out.println(str1.concat(str2);,System.out.println(str1+str2);,System.out.println(str1.replace(a,e);,System.out.println(st
展开阅读全文