java.lang中的常用类.ppt_第1页
java.lang中的常用类.ppt_第2页
java.lang中的常用类.ppt_第3页
java.lang中的常用类.ppt_第4页
java.lang中的常用类.ppt_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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: public String(String original) /other code . ,Note:The parameter is a String object,String a= “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(); System.out.println(“字符出现的索引:“ + index); index = strEmail.indexOf(“sun“); System.out.println(“字符串“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(str1.trim(); ,public String substring(int beginIndex) Returns a new string that is a substring of this string. The substring begins at the specified index and extends to the end of this string. public String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Parameters: the beginning index, inclusive. Returns: the specified substring. Throws: StringIndexOutOfBoundsException if the beginIndex is out of range.,String s1 = “Welcome to Java“; String s2 = s1.substring(0, 11) + “HTML“;,String类更改字符串大小写,public class StringDemo public static void main(String args) String str1 = “Java is OOP“; String str2; str2 = str1.toLowerCase(); System.out.println(str2); str2 = str1.toUpperCase(); System.out.println(str2); ,StringBuffer类-1,StringBuffer类用于表示可以修改的字符串。 使用连接运算符(+)与字符串一起使用自动创建StringBuffer对象。,StringBuffer类-2,StringBuffer类-3,public class StringBufferDemo public static void main(String args) StringBuffer strBuf = new StringBuffer(“Java“); strBuf.append(“ Guide Ver1/“); System.out.println(strBuf); strBuf.append(3); System.out.println(strBuf); strBuf.insert(5, “Student“); System.out.println(strBuf); strBuf.setCharAt(20, .); System.out.println(strBuf); strBuf.reverse(); System.out.println(strBuf); String str = strBuf.toString(); System.out.println(str); ,范例演示,不可变性-1,创建,String类,直接修改,StringBuffer类,与String同等的类,表示可增加和可编写的字符序列,可以将字符插入到中间位置或将字符附加到末端,解决方案,The StringBuffer class is an alternative to the String class. In general, a string buffer can be used wherever a string is used. StringBuffer is more flexible than String. You can add, insert, or append new contents into a string buffer. However, the value of a String object is fixed once the string is created.,StringBuffer类-1,不可变性-2,public class Immutablity public static void main(String args) String str1=“Java“; /创建一个String类的实例变量 String str2=str1; /保存一个引用副本 /创建一个StringBuffer类的实例变量 StringBuffer stringBuffer1=new StringBuffer(“NewFuture“); /保存一个StringBuffer对象引用副本 StringBuffer stringBuffer2=stringBuffer1; System.out.println (“在改变之前:“); System.out.println (“(str1=str2):“+(str1=str2); System.out.println (“(stringBuffer1=stringBuffer2):“+(stringBuffer1=stringBuffer2); /改变值 str1=str1+“ is OOP“; stringBuffer1=stringBuffer1.append(“study Java“); System.out.println (“在改变之后:“); System.out.println (“(str1=str2):“+(str1=str2); System.out.println (“str1=“+str1); System.out.println (“str2=“+str2); System.out.println (“(stringBuffer1=stringBuffer2):“+(stringBuffer1=stringBuffer2); System.out.println (“stringBuffer1=“+stringBuffer1); System.out.println (“stringBuffer2=“+stringBuffer2); ,范例演示,Math类,Math类,静 态 方 法,基本数学运算,几何运算,Math类方法,public class MathDemo /* 构造方法 */ protected MathDemo() /* main 方法演示 Math 类的不同方法 * param args 传递至 main 方法的参数 */ public static void main(String args) /* 此变量存储 num 的值*/ int num = 38; /* 该变量存储 num1 的值*/ float num1 = 65.7f; System.out.println(Math.ceil(num); System.out.println(Math.ceil(num1); System.out.println(Math.floor(num); System.out.println(Math.floor(num1); System.out.println(Math.round(num); System.out.println(Math.round(num1); ,范例演示,Object类-1,是其他所有类的共同父类。 用户类没有扩展自其他类,默认扩展自Object类。,Object类-2,class Student private String mName; private int mAge; public Student(String name, int age) /构造方法 mName = name; mAge = age; /重写Object类中的toString方法 public String toString() String str = “姓名:“ + mName + “, 年龄:“ + mAge + “岁“; return (str); public class ToStringDemo public static void main(String args) Student std = new Student(“张三“, 18); System.out.println(std); /默认调用toString方法 ,范例演示,自动创建对象,Class类-1,无需声明,Class对象,怎么创建呢?,通过对象的getClass()方法,使用Class的静态方法forName(),Class类-2,范例演示,public class ClassDemo public static void main(String args) try /*使用forName方法获得任意一个类的类描述对象 这里以StringBuffer类为例 forName方法有可能抛异常,必须捕捉*/ Class cls = Class.forName(“jav

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论