4.22Object类,Scanner类,String类,StringBuffer类

Object类

/**
 * Object类(任何类都继承于Object类)
 * getClass() 返回Object正在运行时的类,以String形式
 * getName()  返回当前类的全类名,以String形式
 * hashCode() 返回对象的哈希码值
 * */
/**
 * public String toString()返回对象的字符表示(建议所有类重写toString)
 * */
/**
 * equals方法
 * == : 该符号比较的是两个对象的地址
 * equals:默认比较的是两个对象的地址,一般需要进行方法重写
 * */

import javax.management.MXBean;

class Person extends Object{
	private int age;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Person() {
	}

	public Person(int age) {
		this.age = age;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		return true;
	}
	
}
public class Text1 {
	public static void main(String[] args) {
		Person p = new Person(12);
		Class c = p.getClass();
		System.out.println(c);//class org.westos_object类博客练习.Person
		System.err.println(p.hashCode());//118352462
		System.out.println(c.getName());//org.westos_object类博客练习.Person
		System.out.println(p.getClass().getName());//org.westos_object类博客练习.Person
		String str = p.toString();
		System.out.println(str);//org.westos_object类博客练习.Person@70dea4e返回全类名@地址的String类型
		Person p1 = new Person(12);
		boolean b = (p==p1);
		System.out.println(b);//比较的是地址
		//重写前System.out.println(p.equals(p1));//比较的是地址 flase
		//重写后
		System.out.println(p.equals(p1));//true  比较的是地址中的内容
	}
}

Scanner类

import java.util.Scanner;//导入java.util包

/**
 *Scanner类:是一个简单的文本扫描器
 *使用前需要创建对象
 *			如Scanner sc = new Scanner(System.in);然后调包Ctrl+Shift+o
 *			输入值是int型							nextInt()
 *			输入值是String型						nextLine()
 *			输入时***类型							next***()
 *			判断是否下一个录入的数据是否是***类型		hasNext***()	
 * */
public class Text1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个int类型数字");
		if(sc.hasNextInt()) {//先进行输入在进行判断
			int num = sc.nextInt();
			System.out.println("num:"+num);
		}else {
			System.out.println("数据类型有误...");
		}
	}
}

String类

/**
 * String表示的是字符串,字符串是常量,一旦创建就不能更改,但是对象可以被重新赋值
 * String是一种特殊的引用类型,默认值是null
 * 构造方法:
 * 		String():无参构造
		String(byte[] bytes) :将字节数转换成字符串
		public String(byte[] bytes, int index,int length):将字节数组的一部分转换成字符串
		public String(char[] value):将字符数组转化成字符串     			toCharArray():将字符串转换成字符
 		public String(char[] value, int index, int count):将字符数组的一部分转换成字符串
 		public String(String original):将一个字符串常量构造成一个字符串对象
 	注意:String的长度使用的是length()方法.数组中的是length属性,两者不一样
 * */
public class Text1 {
	public static void main(String[] args) {
		String str = "I love Java!";
		char[] ch = str.toCharArray();
		System.out.println(ch);
		System.out.println("--------------");
		char[] ch1 = {'a','b','c'};
		String str2 =  new String(ch1);//需要创建String类的对象
		System.out.println(str2);
	}
}
/**
 **/
public class Text2 {
	public static void main(String[] args) {
		String str = new String("abc");
		int num = 1;
		change(str,num);//String作为形式参数:它的效果和基本类型是一样的,形式参数的改变对对实际参数没有影响 (它一旦赋值,不能被改变)
		System.out.println(str);//abc
		System.out.println(num);//1
		str+="110";	
		System.out.println(str);
	}
	public static void change(String s1,int num) {
		s1 = s1+"110";
		num= num+1;
	}
}
String类常用方法
//String 类型常用方法:
/**
 * 常用获取功能:
 * 			public int length():返回字符串长度
 * 			public char charAt(int num):返回索引处的字符
 * 			public int indexOf(int ch):返回指定字符第一次出现的索引
 * 			public int indexOf(int ch,int num):返回指定字符第一次出现时的索引,从给定位置开始搜索
 * 			public int indexOf(String str):返回指定字符串第一次出现处的索引
 * 			public int indexOf(String str,int num):返回指定字符串第一次出现处的索引,从给定位置开始搜索
 * 截取功能:
 * 			public String substring(int beginIndex):从指定位置开始截取,默认截取到末尾,返回新的字符串
 * 			public String substring(int beginIndex, int endIndex):从指定位置开始到指定位置末尾结束,包前不包后
 * 转换功能:
 * 			public byte[] getBytes() :将字符串转换为字节数组
 *			public char[] toCharArray() :将字符串转换成字符数组(重点)
 *			public static String valueOf(int i):将int类型的数据转换成字符串(重点) 	这个方法可以将任何类型的数据转化成String类型
 *			public String toLowerCase():转成小写
 *			public String toUpperCase():字符串中所有的字符变成大写
 *其他功能:
 *			public String replace(char oldChar,char newChar):将大字符串中的某个字符替换掉成新的字符
 *			public String replace(String oldStr,String newStr):将大串中的某个子字符串替换掉
 *			public String trim():去除字符串两端空格
 *			public int compareTo(String anotherString)按字典顺序比较两个字符串
 * */
public class Text3 {
	public static void main(String[] args) {
		String s = "helloworld";
		System.out.println(s.length());//10
		System.out.println(s.charAt(2));//l
		System.out.println(s.indexOf('d'));//9
		System.out.println(s.indexOf("llo"));//2
		System.out.println(s.substring(2));//lloworld
		System.out.println(s.substring(2, 5));//llo
		System.out.println(s.toUpperCase());//HELLOWORLD
		System.out.println(s.replace('h', 'H'));//Helloworld
		String str = "   kkk   ";
		System.out.println(str);//   kkk   
		System.out.println(str.trim());//kkk
	}
}

StringBuffer类

/**
 *StringBuffer类
 *StringBuffer:线程安全的可变字符序列
 *其构造方法:
 *			StringBuffer();无参构造,初始容量16,指的是capacity
 *			StringBuffer(int capacity);指定容量构造一个字符串缓冲区
 *			StringBuffer(String str);构造一个字符串缓冲区,将其内容初始化为指定字符串内容
 *其获取功能:
 *			public int length()返回字符串缓冲区长度;
 *			public int capacity()返回字符缓冲区串容量
 *添加功能:
 *			public StringBuffer append(String/boolean...)在字符串缓冲区末尾追加数据,然后返回StringBuffer;
 *			public StringBuffer insert(int offset,String str):将当前str字符串添加到指定位置处,它返回字符串缓冲区本身	
 *删除功能:
 *			public StringBuffer deleteCharAt(int index):移除指定位置处的字符
 * 			public StringBuffer delete(int start,int end):移除从指定位置处到end-1处的子字符串
 * 翻转功能:
 * 			public StringBuffer reverse() :将缓冲区中的字符序列反转取代,返回它(字符串冲)本身
 * 截取功能:
 * 			public String substring(int start):从指定位置开始截取,默认截取到末尾,返回值不在是缓冲区本身,而是一个新的字符串
 *			public String substring(int start,int end) :从指定位置开始到指定位置结束截取,包前不包后,返回值不在是缓冲区本身,而是一个新的字符串
 *替代功能:
 *			public StringBuffer replace(int start,int end,String str)用新的str替换指定长度区间的字符,返回StringBuffer
 **/
public class Text1 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("abc");
		System.out.println(sb);//abc
		System.out.println(sb.length());//3
		System.out.println(sb.capacity());//16+3=19
		System.out.println(sb.append("def"));//abcdef
		System.out.println(sb.insert(1,"b"));//abbcdef
		System.out.println(sb.delete(2,3));//abcdef
		System.out.println(sb.reverse());//fedcba
		System.out.println(sb.substring(3));//cba
		System.out.println(sb.replace(2,4,"hello"));//fehelloba
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值