一.object类
1.object是层次结构的根类,每个类都使用object作为父类
Class c=s.getClass();//object运行时的类
String name=s.getName();//以字符串显示当前类
2.object中的一个方法public String toString();//返回该字符串的表示
直接输出对象的名称,实际执行了object中的tostring();输出的全部类名@十六进制数据
如果不想输出地址值,而是输出对象名称,就需要重写object中的tostring(),一般情况自动生成即可
3.public boolean equals(object obj)指示其他对象是否与此对象相等
==比较的是地址是否相同
正常情况下,equal方法在底层执行的是==比较地址值
在自定义类中,重写object中得equal方法,比较的是成员变量的值是否相同
package org6; class student{ } public class demo1 { public static void main(String[]args) { student s1=new student(); student s2=new student(); System.out.println(s1.hashCode());//相当于一个地址值118352462 System.out.println(s2.hashCode());//1550089733 Class c1=s1.getClass();//object运行时的类 System.out.println(c1);//class org6.student String name=c1.getName();//以字符串显示当前的类 System.out.println(name);//org6.student } }equal方法重写后比较的是两个对象的内容
二 .Scanner
1.Scanner类: 简单文本扫描器。
键盘录入的步骤 1)需要创建键盘录入对象 Scanner sc =new Scanner(System.in); 2)导入包 ctrl+shift+o 3)接收数据 XXX 变量名= sc.nextXXX(); 2.构造方法: public Scanner(InputStream source): 以输入流的形式录入数据的 InputStream:字节输入流: InputStream in = System.in ; //底层执行返回的是一个字节输入流(标准输入流)
3.Scanner类的一些常见的方法: XXX 变量名 = 键盘录入对象.nextXXX(); 在Scanner类中一个判断功能: public boolean hasNextXXX():当前扫描器判断是否有下一个可以录入的XXX类型数据 nextXXX(); java.util.InputMismatchException:录入的数据和接收的数据类型不匹配异常
package org8; import java.util.Scanner; public class practice2 { public static void main(String[]args) { Scanner sc=new Scanner(System.in); if(sc.hasNextInt()) { int x=sc.nextInt(); System.out.println(x); } else { System.out.println("输入的数据错误"); } } }
4.键盘录入两个int类型的数据,分别输出
键盘录入两个String类型的数据,分别输出
先录入一个String类型的数据,在录入int类型的数据
先录入一个int类型的数据,在录入String类型的数据,有问题,第二个数据是字符串类型,又由于录入下一个数据要"空格"本身是一个字符串,所以后面的字符串没有接收入,应该重新创建对象
键盘录入两个String类型的数据,分别输出
先录入一个String类型的数据,在录入int类型的数据
先录入一个int类型的数据,在录入String类型的数据,有问题,第二个数据是字符串类型,又由于录入下一个数据要"空格"本身是一个字符串,所以后面的字符串没有接收入,应该重新创建对象
public static void main(String[]args) { Scanner sc=new Scanner(System.in); int x=sc.nextInt(); //String s=sc.nextLine(); Scanner sc2=new Scanner(System.in); String s=sc2.nextLine(); System.out.println(x+"---"+s); }
扫描二维码关注公众号,回复:
963804 查看本文章
三.String
1.String:表示字符串:
字符串是常量;它们的值在创建之后不能更改。
2. String是一种特殊的引用类型:
默认值:null
3.构造方法:
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):将一个字符串常量构造成一个字符串对象
字符串是常量;它们的值在创建之后不能更改。
2. String是一种特殊的引用类型:
默认值:null
3.构造方法:
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):将一个字符串常量构造成一个字符串对象
常用的方法:
public int length()返回此字符串的长度
public static void main(String[]args) { byte[] b= {97,98,99,100}; char[]a= {'a','b','c','d'}; String s1=new String(b); String s2=new String(b,2,2); String s3=new String(a); String s4=new String(a,2,2); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); }
4.String字符串的最大的特点:
字符串一旦被赋值,其值不能被改变
字符串一旦被赋值,其值不能被改变
String作为形式参数:它的效果和基本类型是一样的,形式参数的改变对对实际参数没有影响 (它一旦赋值,不能被改变
public static void main(String[]args) { String s1="hello"; String s2="world"; System.out.println(s1); System.out.println(s2); } public static void method(String s1,String s2) { s1=s1+"java"; s2="hello"; }
5.String类的常用的判断功能
boolean equals(Object obj):将此字符串与指定的对象比较
boolean equalsIgnoreCase(String str)将此 String 与另一个 String 比较,不考虑大小写
boolean contains(String str):判断当前大川中是否包含子字符串 (重点)
boolean startsWith(String str):以当前str字符串开头(重点)
boolean endsWith(String str):以当前str字符串结尾(重点)
boolean isEmpty():判断字符串是否为空
public String concat(String str):字符串的特有功能:拼接功能和+拼接符是一个意思
boolean equals(Object obj):将此字符串与指定的对象比较
boolean equalsIgnoreCase(String str)将此 String 与另一个 String 比较,不考虑大小写
boolean contains(String str):判断当前大川中是否包含子字符串 (重点)
boolean startsWith(String str):以当前str字符串开头(重点)
boolean endsWith(String str):以当前str字符串结尾(重点)
boolean isEmpty():判断字符串是否为空
public String concat(String str):字符串的特有功能:拼接功能和+拼接符是一个意思
public static void main(String[]args) { String s1="helloworld"; String s2="hello"; System.out.println(s1.equals(s2)); System.out.println(s1.contains(s2)); System.out.println(s1.startsWith("hel")); }6.模拟登陆
public static void main(String[]args) { Scanner sc=new Scanner(System.in); String Name="jeffery"; String pwd="8023"; System.out.println("请输入用户名和密码"); for(int i=0;i<3;i++) { String userName=sc.nextLine(); String userPwd=sc.nextLine(); if(Name.equals(userName)&&pwd.equals(userPwd)) { System.out.println("登录成功"); break; } else { System.out.println("你还有"+(2-i)+"次机会"); } } }
7.String类的常用获取功能:
public int length():获取字符串的长度
public char charAt(int index)返回指定索引处的 字符
public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引
public int indexOf(int ch,int fromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
public int indexOf(String str,int fromIndex)回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索。
8.截取功能
public String substring(int beginIndex):从指定位置开始截取,默认截取到末尾,返回新的字符串
public String substring(int beginIndex, int endIndex):从指定位置开始到指定位置末尾结束,包前不包含
9.String的常用转换功能:
public byte[] getBytes() :将字符串转换为字节数组
public char[] toCharArray() :将字符串转换成字符数组(重点)
public static String valueOf(int i):将int类型的数据转换成字符串(重点)这个方法可以将任何类型的数据转化成String类型
public String toLowerCase():转成小写
public String toUpperCase():字符串中所有的字符变成大写
public byte[] getBytes() :将字符串转换为字节数组
public char[] toCharArray() :将字符串转换成字符数组(重点)
public static String valueOf(int i):将int类型的数据转换成字符串(重点)这个方法可以将任何类型的数据转化成String类型
public String toLowerCase():转成小写
public String toUpperCase():字符串中所有的字符变成大写
10.String类型的其他功能:
public String replace(char oldChar,char newChar):将大字符串中的某个字符替换掉成新的字符
public String replace(String oldStr,String newStr):将大串中的某个子字符串替换掉
public String trim():去除字符串两端空格
public int compareTo(String anotherString)按字典顺序比较两个字符串
public String replace(char oldChar,char newChar):将大字符串中的某个字符替换掉成新的字符
public String replace(String oldStr,String newStr):将大串中的某个子字符串替换掉
public String trim():去除字符串两端空格
public int compareTo(String anotherString)按字典顺序比较两个字符串