Object类和String类

1 篇文章 0 订阅
1 篇文章 0 订阅

Object类

1.注解

语言通常不需要的类声明从继承Object因为继承是隐式。
因为.NET Framework 中的所有类都派生自Object,在中定义的每个方法Object类是所有的对象中的可用系统中。 派生类可以和重写其中的某些方法,包括:
Equals -支持对象之间的比较。
Finalize -自动回收对象之前,请执行清理操作。
GetHashCode 生成对应于对象的值的数字,以支持使用哈希表。
ToString — 生成描述类的实例的用户可读文本字符串。

2.性能注意事项

如果您正在设计的类,如集合,必须处理任何类型的对象,可以创建接受的实例的类成员Object类。但是,装箱和取消装箱类型的过程会带来性能开销。 如果您知道您的新类将频繁地处理某些值类型可以使用两个策略之一来装箱的成本降至最低。
创建一个接受的常规方法Object类型和一组特定于类型的方法重载接受每个类要频繁处理预期的值类型。 如果存在的特定于类型的方法接受调用的参数类型,没有值类型装箱会发生,并调用特定于类型的方法。 如果没有任何方法自变量调用的参数类型相匹配,该参数进行装箱,并调用常规方法。
设计你的类型和其成员来使用泛型。 创建您的类的实例并指定泛型类型参数时,公共语言运行时将创建封闭式泛型类型。 泛型方法是特定于类型,可以不装箱调用参数的情况下调用。
虽然有时很有必要,若要开发通用类,接受并返回Object类型,您可以通过提供特定于类型的类来处理常用的类型提高性能。 例如,提供特定于设置和获取布尔值的类消除了装箱和取消装箱的布尔值的成本。
构造函数
Object() 初始化 Object 类的新实例。

3.方法

Equals(Object) 确定指定的对象是否等于当前对象。
Equals(Object, Object) 确定指定的对象实例是否被视为相等。
Finalize() 在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。
GetHashCode() 作为默认哈希函数。
GetType() 获取当前实例的 Type。
MemberwiseClone() 创建当前 Object 的浅表副本。
ReferenceEquals(Object, Object) 确定指定的 Object 实例是否是相同的实例。
ToString() 返回表示当前对象的字符串。

4.线程安全性

公共静态 (Shared在 Visual Basic 中) 的此类型的成员是线程安全。 不保证实例成员都是线程安全。

String类

表示文本,即一系列 Unicode 字符。String类是不可变的,对String类的任何改变,都是返回一个新的String类对象。 String 对象是 System.Char 对象的有序集合,用于表示字符串。String 对象的值是该有序集合的内容,并且该值是不可变的。

一、String类常用方法

1、求字符串长度

public int length()//返回该字符串的长度
1 String str = new String(“asdfzxc”);
2 int strlength = str.length();//strlength = 7

2、求字符串某一位置字符

public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。
1 String str = new String(“asdfzxc”);
2 char ch = str.charAt(4);//ch = z

3、提取子串

用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:
1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
1 String str1 = new String(“asdfzxc”);
2 String str2 = str1.substring(2);//str2 = “dfzxc”
3 String str3 = str1.substring(2,5);//str3 = “dfz”

4、字符串比较

1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。
3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。
1 String str1 = new String(“abc”);
2 String str2 = new String(“ABC”);
3 int a = str1.compareTo(str2);//a>0
4 int b = str1.compareToIgnoreCase(str2);//b=0
5 boolean c = str1.equals(str2);//c=false
6 boolean d = str1.equalsIgnoreCase(str2);//d=true

5、字符串连接

public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
1 String str = “aa”.concat(“bb”).concat(“cc”);
2 相当于String str = “aa”+“bb”+“cc”;

6、字符串中单个字符查找

1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
1 String str = “I am a good student”;
2 int a = str.indexOf(‘a’);//a = 2
3 int b = str.indexOf(“good”);//b = 7
4 int c = str.indexOf(“w”,2);//c = -1
5 int d = str.lastIndexOf(“a”);//d = 5
6 int e = str.lastIndexOf(“a”,3);//e = 2

7、字符串中字符的大小写转换

1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
1 String str = new String(“asDF”);
2 String str1 = str.toLowerCase();//str1 = “asdf”
3 String str2 = str.toUpperCase();//str2 = “ASDF”

8、字符串中字符的替换

1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
1 String str = “asdzxcasd”;
2 String str1 = str.replace(‘a’,‘g’);//str1 = “gsdzxcgsd”
3 String str2 = str.replace(“asd”,“fgh”);//str2 = “fghzxcfgh”
4 String str3 = str.replaceFirst(“asd”,“fgh”);//str3 = “fghzxcasd”
5 String str4 = str.replaceAll(“asd”,“fgh”);//str4 = “fghzxcfgh”

二、字符串与基本类型的转换

1、字符串转换为基本类型

java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:
1)public static byte parseByte(String s)
2)public static short parseShort(String s)
3)public static short parseInt(String s)
4)public static long parseLong(String s)
5)public static float parseFloat(String s)
6)public static double parseDouble(String s)
例如:
1 int n = Integer.parseInt(“12”);
2 float f = Float.parseFloat(“12.34”);
3 double d = Double.parseDouble(“1.124”);

2、基本类型转换为字符串类型

String类中提供了String valueOf()放法,用作基本类型转换为字符串类型。
1)static String valueOf(char data[])
2)static String valueOf(char data[], int offset, int count)
3)static String valueOf(boolean b)
4)static String valueOf(char c)
5)static String valueOf(int i)
6)static String valueOf(long l)
7)static String valueOf(float f)
8)static String valueOf(double d)
例如:
1 String s1 = String.valueOf(12);
2 String s1 = String.valueOf(12.34);

3、进制转换

使用Long类中的方法得到整数之间的各种进制转换的方法:
Long.toBinaryString(long l)
Long.toOctalString(long l)
Long.toHexString(long l)
Long.toString(long l, int p)//p作为任意进制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值