多看api文档
多看api文档
多看api文档
重要的事情说三遍
字符串相关类(String、StringBuffer)
- java.lang.String代表不可变的字符序列。
- String(byte[] bytes)一种编码格式改变成另外一种编码格式。
- String(byte[] bytes,int offset,int length)从第几个位置开始,数几个字节,构建成字符串。
- String(byte[] bytes,int offset,int length,String charsetName)用一个特定的字符编码,然后把这个字节数组改变成这个字符编码的一个字符串。
其他方法:
- public char charAt (int index)返回字符串中第index个字符
- public int length()返回字符串的长度
- public int indexOf(String str)返回字符串中出现str的第一个位置
- public int indexOf(String str,int fromIndex)返回字符串中从fromIndex开始出现str的第一个位置
- public boolean equalsIgnoreCase(String another)比较字符串与another是否一样(忽略大小写)
- public String replace(char oldChar,char newChar)在字符串中用newChar字符替换oldChar字符。
- public boolean startsWith(String prefix)判断字符串是否以prefix字符串开头。
- public boolean endsWith(String prefix)判断字符串是否以prefix字符串结尾。
- public String toUpperCase()返回一个字符串为该字符串的大写形式
- public String toLowerCase()返回一个字符串为该字符串的小写形式
- public String substring(int beginIndex)返回该字符串从beginIndex开始到结尾的字符串
- public String substring(int beginIndex,int endIndex)返回该字符串从beginIndex开始到endIndex结尾的字符串
- public String trim()返回将该字符串去掉开头和结尾空格后的字符串
静态重载方法: - public static valueOf(Object obj) :任何一种数据类型的对象都可以往里扔,转成字符串。任何对象都当Object看,任何狗、任何猫都当Animal看。然后调用toString方法。传的什么类型,调用谁的toString()。【重写:动态绑定】
- publicString[]split(String regex)可以将一个字符串按照指定的分隔符分割,返回分隔后的字符串数组。
StringBuffer
java.lang.StringBuffer代表可变的字符序列。
StringBuffer与String类似,但StringBuffer可以对其字符串进行改变。
StringBuffer常见的构造方法:
StringBuffer():创建一个不包含字符序列的“空”的StringBuffer对象。
StringBuffer(String str):创建一个StringBuffer对象,包含与String对象str相同的字符序列。
StringBuffer 和 StringBuilder区别
如果你读过《Think in Java》,而且对里面描述HashTable和HashMap区别的那部分章节比较熟悉的话,你一定也明白了原因所在。对,就是支持线程同步保证线程安全而导致性能下降的问题。HashTable是线程安全的,很多方法都是synchronized方法,而HashMap不是线程安全的,但其在单线程程序中的性能比HashTable要高。StringBuffer和StringBuilder类的区别也在于此,新引入的StringBuilder类不是线程安全的,但其在单线程中的性能比StringBuffer高。
基本数据类型包装类
main函数拿到的任何类型都是字符串,怎么把字符串转换成相应的基础数据类型呢?
例如:“3.14”–>double。
基础类型(四类八种)一般都分配在栈上,可你就是想让它分配到堆空间上需要将其包装成一个对象,有一系列类,叫做基础类型的包装类。
Class Byte
Class Boolean
Class Long
Class Float
Class Double
Class Character
static Byte parseByte(String s)
static Double parseDouble(String s)
static toString()
构造方法:Integer(int value)
Integer(String s)
static long valueOf(String s):把字符串转成相对应的数据类型包装类的对象。
Long (long value):把long类型的数封装成long类型的对象,new Long(value)。
long longValue():把long类型的对象里面的数拿出来。
怎么把long类型的转换成int类型呢?前面加(int)但是,实际中内部执行的是intValue()方法。
怎么把long类型的转换成float类型呢?floatValue()
怎么把long类型的转换成double类型呢?doubleValue()
数和数之间的转换,内部是这些方法在执行!
包装类的常见方法:
- public static final int MAX_VALUE 最大的int型数
- public static final int MIN_VALUE 最小的int型数
- public long longValue() 返回封装数据的long类型
- public double doubleValue()返回封装数据的double类型
- public int intValue()返回封装数据的int值
- public static int parseInt(String s) 将字符串解析成int型数据,返回该数据。
- public static Integer ValueOf(String s) throws NumberFormatException
返回Integer对象,其中封装的整型数据为字符串s所表示。
File类
Java.io.File类代表系统文件名(路径和文件名)。
并不代表物理上的文件。通过IO才能读出里面数据。
常见构造方法:
- public File(String pathname):以pathname为路径创建File对象,如果pathname是相对路径,则默认的当前路径在系统属性user.dir中存储。(不是在硬盘上创建这样一个文件或路径,是在内存中创建一个名字叫这个的File对象,可以用public boolean exsits()查看是否真正存在这个文件)
- public File(String parent,String child):以parent为父路径,child为子路径创建File对象。
- File的静态属性String separator 存储了当前系统的路径分隔符。
枚举类
java.lang.Enum枚举类型
只能够取特定值中的一个
public enum EMessage {
/**以下是http消息处理ID-----*/
CLIENT_INSTALLATION(0, "test1"),
CLIENT_UPGRADE(1, "test2"),
CLIENT_START(2, "test3"),
/**----------------*/
;
private final String mUrlPath;
public final int index;
EMessage(int index, String url) {
this.index = index;
this.mUrlPath = url;
}
public String getUrlPath() {
return mUrlPath;
}
}
枚举类型而不是一个变量!
定义了一个类型就好比说定义了Person、Cat类型,用此类型再来定义变量。
EMessage.values()获取对象数组