Java提供了有功能强大的类库,因而在进行Java应用开发时得心应手。
Object类中常用的方法:
public int hashCode()返回该对象的哈希码值, 把它理解地址值(不是实际的地址);
public final Class getClass()返回此 Object 的运行时类;
Class类中有一个方法:
public String getName()以 String 的形式返回此类名;
public String toString(); 回该对象的字符串表示;
Integer:是int类型的包装类类型它里面提供了一个方法:
public static String toHexString(int i):以十六进制表示对字符串形式;
public boolean equals(Object obj)指示其他某个对象是否与此对象“相等”。
==:比较的值相等(地址值)
equals:本身属于Object类中的方法,默认比较的是地址值是否相同;
Object 类的 clone 方法执行特定的复制操作。首先,如果此对象的类不能实现接口 Cloneable,则会抛出 CloneNotSupportedException。
Scanner类
Scanner sc =new Scanner(System.in);
2)导入包:import java.util.Scanenr; 快捷键:ctrl+shift+o
3)接收数据
XXX 变量名= sc.nextXXX();
public boolean hasNextXXX():当前扫描器判断是否有下一个可以录入的XXX类型数据;
解决方法,重新创建录入对象
字符常量是用双引号括起来的一系列Java合法字符,如:"wo ai Java";
字符串常量声明格式:
String():无参构造
String(byte[] bytes) :将字节数组转换成字符串;
public String(byte[] bytes, int index,int length):将字节数组的一部分转换成字符串;
public String(char[] value):将字符数组转化成字符串 ;
public String(char[] value, int index, int count):将字符数组的一部分转换成字符串;
public String(String original):将一个字符串常量构造成一个字符串对象;
show:
public int length()返回此字符串的长度;
public String concat(String str):字符串的特有功能:拼接功能和“+”拼接符是一个意思;
boolean equals(Object obj):将此字符串与指定的对象比较;equals方法:默认的比较是地址值,String底层重写了equals方法,所以比较的是内容是否相同;
boolean equalsIgnoreCase(String str)将此 String 与另一个 String 比较不考虑大小写;
boolean contains(String str):判断当前大字符串中是否包含str符串(重点);
boolean startsWith(String str):以当前str字符串开头(重点);
boolean endsWith(String str):以当前str字符串结尾(重点);
boolean isEmpty():判断字符串是否为空;
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)返回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索;
截取功能
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类型的数据转换成字符串(重点),
value(Datatype i)方法可以将任何类型的数据转化成String类型;
public String toUpperCase():将字符串中所有的字符变成大写;
public String replace(char oldChar,char newChar):将大字符串中的某个字符替换掉成新的字符;
public String replace(String oldStr,String newStr):将大串中的某个子字符串替换掉;
public String trim():去除字符串两端空格;
如果比原字符串顺序后,返回一个小于0的数;
如果比原字符串顺序前,返回一个大于0的数;
如果顺序相等,返回0;
例题:
1、输入字符数据按字典顺序重新排序:
但:
以上代码等同于:
StringBuffer类
StringBuffer的构造方法:
StringBuffer() :无参构造的形式,初始容量16;
StringBuffer(int capacity) :指定容量构造一个字符串缓冲区;
StringBuffer(String str) 构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容;
StringBuffer的获取功能:
public int length()返回长度;
public int capacity()返回当前容量 (如果超过容量,系统自动分配(存储字符串的时候,英文的));
public StringBuffer append(String/boolean....) :在字符串缓冲区中追加数据(在末尾追加),并且返回字符串缓冲区本身;
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字符串去替换,返回值是字符串缓冲区本身(包前也包后);
String和StringBuffer之间的相互转换
2)StringBuilder的运行速度大于StringBuffer,StringBuffer大于String;
3) StringBuilder:线程不安全的,不同步的,执行效率高,并且单线程中优先采用StringBuilder,StringBuffer:线程安全的,同步的,执行效率低;
*基本类型对应都有一个包装类型,目的就为了将基本数据类型可以String类型之间进行互相转换
* byte Byte
* short Short
* int Integer
* long Long
* float Float
* double Double
* char character
* boolean Boolean
静态成员常量:
public static final int MAX_VALUE:值为 231-1 的常量,它表示 int 类型能够表示的最大值。
public static final int MIN_VALUE:值为 -231 的常量,它表示 int 类型能够表示的最小值。
构造方法:(没有无参构造)
Integer(int value) :构造一个新分配的 Integer 对象,它表示指定的 int 值。
Integer(String s) :造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
public static String toBinaryString(int i):以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
public static String toOctalString(int i):以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。
public static String toHexString(int i):以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
public static int parseInt(String s):将字符串类型所标示的int型转换为int型。
构造方法:
public Character(char value): 构造一个新分配的 Character 对象,用以表示指定的 char 值。
public static boolean isDigit(char ch):确定指定字符是否为数字。
public static boolean isLowerCase(int ch): 确定是否是小写字母字符。
public static boolean isUpperCase(int ch):确定是否大写字母字符。
public static int toLowerCase(int codePoint):将字符转换为小写;
public static int toUpperCase(int codePoint):将字符转换为大写;
Object类
Object:是类层次结构的根类.每个类都使用 Object 作为超类(父类)Object类中常用的方法:
public int hashCode()返回该对象的哈希码值, 把它理解地址值(不是实际的地址);
public final Class getClass()返回此 Object 的运行时类;
Class类中有一个方法:
public String getName()以 String 的形式返回此类名;
public String toString(); 回该对象的字符串表示;
Integer:是int类型的包装类类型它里面提供了一个方法:
public static String toHexString(int i):以十六进制表示对字符串形式;
class Student {
int age;
}
public class Objecttext {
public static void main(String[] args) {
Student s=new Student();
System.out.println("该对象的类:"+s.getClass());
String str=s.getClass().getName();
System.out.println("该对象的类名:"+str);
System.out.println("该对象的hashCode值:"+s.hashCode());//类似地址值并非地址值
String str1=Integer.toHexString(s.hashCode());//将哈希码值转化为16进制
System.out.println("十六进制地址值:"+str1);
System.out.println("该对象对应的字符串:"+s.toString());
System.out.println("该对象对应的字符串:"+str+"@"+str1);
}
}
显示:
该对象的类:class org.test.c.Student
该对象的类名:org.test.c.Student
该对象的hashCode值:2018699554
十六进制地址值:7852e922
该对象对应的字符串:org.test.c.Student@7852e922
该对象对应的字符串:org.test.c.Student@7852e922
public boolean equals(Object obj)指示其他某个对象是否与此对象“相等”。
==:比较的值相等(地址值)
equals:本身属于Object类中的方法,默认比较的是地址值是否相同;
class People{//创建一个人类
private String name;
private int age;
public People() {
super();
}
public People(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class EqualsTest {
public static void main(String[] args) {
People s1=new People("阿威",22);
People s2=new People("阿威",22);
People s3=s1;//s1指向的地址赋给s3
if(s1==s2) {//"=="比较的是两个对象地址值是否相等
System.out.println(true);
}
else {
System.out.println(false);
}
boolean t=s1.equals(s2);
System.out.println(t);//equals方法也是比较两个对象的地址值
System.out.println(s1.equals(s3));
}
}
显示:
false
false
true
在自定义的类中,重写Object中的equals()方法,比较是两个对象的成员变量的值是否相同,自动生成即可!
class Student{
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override//重写equals方法,比较两个对象内容是否相等
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class EqualsTest2 {
public static void main(String[] args) {
Student s1=new Student("小明",28);
Student s2=new Student("小明",28);
System.out.println(s1.equals(s2 ));
}
}
显示:
true
protected Object clone()创建并返回此对象的一个副本
Object 类的 clone 方法执行特定的复制操作。首先,如果此对象的类不能实现接口 Cloneable,则会抛出 CloneNotSupportedException。
class Student implements Cloneable{//实现Cloneable接口
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override//重写接口中的clone方法
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Clonetest {
public static void main(String[] args) throws CloneNotSupportedException {
Student s1=new Student("小明",22);
System.out.println(s1.getName()+" "+s1.getAge());
Object s2=s1.clone();
Student s3=(Student)s2;//向下转型
System.out.println(s1.getName()+" "+s1.getAge());
}
}
显示:
小明 22
小明 22
Scanner类
键盘录入的步骤
1)需要创建键盘录入对象Scanner sc =new Scanner(System.in);
2)导入包:import java.util.Scanenr; 快捷键:ctrl+shift+o
3)接收数据
XXX 变量名= sc.nextXXX();
public boolean hasNextXXX():当前扫描器判断是否有下一个可以录入的XXX类型数据;
import java.util.Scanner;
public class Scannertest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个int型数据:");
int x=0;
if(sc.hasNextInt()) {
x=sc.nextInt();
}
else {
System.out.println("输入的数据类型不匹配");
}
System.out.println(x);
}
}
显示:
请输入一个int型数据:
asds
输入的数据类型不匹配
0
***先录入一个int类型的数据,在录入String类型的数据,有问题,第二个数据是字符串类型,又由于录入下一个数据要"空格"本身是一个字符串,所以无法继续录入解决方法,重新创建录入对象
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个int型数据:");
int x=sc.nextInt();
System.out.println("请输入一个字符串型数据:");
String y=sc.nextLine();
System.out.println(x+"-------"+y);
}
}
显示:
223
请输入一个字符串型数据:
223------- //控格相当录入字符串数据
String类
String是一种特殊的引用类型:默认值是:NULL;字符常量是用双引号括起来的一系列Java合法字符,如:"wo ai Java";
字符串常量声明格式:
String s1=new String();
s1="HelloWorld";
String s2=new String("HelloWorld");
String s3="HelloWorld";
构造方法:
String():无参构造
String(byte[] bytes) :将字节数组转换成字符串;
public String(byte[] bytes, int index,int length):将字节数组的一部分转换成字符串;
public String(char[] value):将字符数组转化成字符串 ;
public String(char[] value, int index, int count):将字符数组的一部分转换成字符串;
public String(String original):将一个字符串常量构造成一个字符串对象;
show:
//字符串常用构造方法
public class StringDemo {
public static void main(String[] args) {
String s=new String ();
System.out.println(s);//输出字符串s对象
String s1=new String("helloworld");//将一个字符串常量构造成一个字符串对象
System.out.println(s1.toString()); //toString方法被String类重写
byte[] a= {97,98,99,100,65,66,67,68};
String s2=new String(a);//将字节数组转化为字符串
System.out.println(s2);
String s3=new String(a,4,4);//将字节数组的一部分转化为字符串
System.out.println(s3);
char[] b= {'我','爱','J','A','V','A'};
String s4=new String(b);//将字符数组转化为字符串
System.out.println(s4);
String s5=new String(b,2,4);//将字符数组的一部分转化为字符串
System.out.println(s5);
}
}
显示:
helloworld
abcdABCD
ABCD
我爱JAVA
JAVA
常用的方法:
public int length()返回此字符串的长度;
public String concat(String str):字符串的特有功能:拼接功能和“+”拼接符是一个意思;
public class StringDemo2 {
public static void main(String[] args) {
String s="abcdefgh";
System.out.println(s);
s+=s;//拼接字符
System.out.println(s);
s=s.concat("ijk");//拼接方法
System.out.println(s);
System.out.println(s.length());//计算字符串长度
}
}
显示:
abcdefgh
abcdefghabcdefgh
abcdefghabcdefghijk
19
字符串的比较判断方法:
boolean equals(Object obj):将此字符串与指定的对象比较;equals方法:默认的比较是地址值,String底层重写了equals方法,所以比较的是内容是否相同;
boolean equalsIgnoreCase(String str)将此 String 与另一个 String 比较不考虑大小写;
boolean contains(String str):判断当前大字符串中是否包含str符串(重点);
boolean startsWith(String str):以当前str字符串开头(重点);
boolean endsWith(String str):以当前str字符串结尾(重点);
boolean isEmpty():判断字符串是否为空;
public class StringDemo3 {
public static void main(String[] args) {
String s=new String("asdf");
String s1="asdf";
if(s==s1) {//比较地址是否相等
System.out.println(true);
}
else {
System.out.println(false);
}
System.out.println(s.equals(s1));//比较值是否相等
String s2="ASDF";
System.out.println(s.equalsIgnoreCase(s2));//比较值不考虑大小写
String s3="as";
String s4="af";
System.out.println(s.contains(s3));//判断此字符串是否包含该字符串
System.out.println(s.contains(s4));
System.out.println(s.startsWith("asd"));//判断此字符串是否以该字符串开头
System.out.println(s.endsWith(s1));//判断此字符串是否以该字符串结尾
String s5=new String();
System.out.println(s5.isEmpty());//判断此字符串是否为空
System.out.println(s.isEmpty());
}
}
显示:
false
true
true
true
false
true
true
true
false
例题:模拟登录,给三次机会,并提示还有几次,如果登录成功,玩一个猜数字小游戏。
import java.util.Scanner;
public class StringDemo4 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String name="admin";//定义用户名
String password="12345678";//定义密码
System.out.print("请输入用户名:");
do {
String s=sc.nextLine();//输入用户名
if(name.equals(s)) {//判断输入的用户名是否相等
break;
}
System.out.print("输入的用户名不存在,请重新输入:");
}while(true);
System.out.print("请输入密码:");
for(int i=2;i>=0;i--) {
String m=sc.nextLine();//输入密码
if(password.equals(m)) {//判断输入的密码是否相等
System.out.println("通过!开始猜数字游戏");
guess();//调用猜数字方法
break;
}
else if(i==0) {
System.out.println("账户已被封锁");
}
else {
System.out.print("输入的密码错误,你还有"+i+"次机会,请重新输入:");
}
}
}
public static void guess() {//设置猜数字方法
Scanner sc2=new Scanner(System.in);
int sum=(int)(Math.random()*100);//使用Math类中的random方法设置一个随机数
System.out.println("请输入你猜要的数字:");
while(true) {
int g=sc2.nextInt();
if(sum>g) {
System.out.println("你猜小了");
}
else if(sum<g) {
System.out.println("你猜大了");
}
else {
System.out.println("恭喜你猜对了");
break;
}
}
}
}
显示:
请输入用户名:abmin
输入的用户名不存在,请重新输入:admin
请输入密码:1234567
输入的密码错误,你还有2次机会,请重新输入:123456
输入的密码错误,你还有1次机会,请重新输入:12345678
通过!开始猜数字游戏
请输入你猜要的数字:
50
你猜小了
75
你猜大了
60
你猜小了
70
你猜大了
65
你猜小了
68
恭喜你猜对了
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)返回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索;
截取功能
public String substring(int beginIndex):从指定位置开始截取,默认截取到末尾,返回新的字符串;
public String substring(int beginIndex, int endIndex):从指定位置开始到指定位置末尾结束,包前不包后;
public class StringTest {
public static void main(String[] args) {
String s=new String("asdfghjk");
System.out.println(s.length());//获取字符串的长度
System.out.println(s.charAt(3));//返回指定索引处的字符
System.out.println(s.indexOf('f'));//返回指定字符在此字符串中第一次出现处的索引
System.out.println(s.indexOf('f',2));//返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
System.out.println(s.indexOf("fgh"));//返回指定子字符串在此字符串中第一次出现处的索引
System.out.println(s.indexOf("fgh",1));//返回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索
String f=s.substring(4);//从指定位置开始截取,默认截取到末尾,返回新的字符串
System.out.println(f);
String f1=s.substring(4, 7);//从指定位置开始到指定位置末尾结束,包前不包含后
System.out.println(f1);
}
}
显示:
8
f
3
3
3
3
ghjk
ghj //包前不包后
String的常用转换功能:public byte[] getBytes() :将字符串转换为字节数组;
public char[] toCharArray() :将字符串转换成字符数组(重点);
public class StringTest2 {
public static void main(String[] args) {
String a= "asdfghjkl";
byte[] b=a.getBytes();//将字符串转化为字节数组
for(byte y:b) {
System.out.print(y+" ");
}
System.out.println();
char[] c=a.toCharArray();//将字符串转化为字符数组
for(char x:c) {
System.out.print(x+" ");
}
System.out.println();
String s="acbDEF012#*";
byte[] f=s.getBytes();
for(byte y:f) {
System.out.print(y+" ");
}
System.out.println();
char[] e=s.toCharArray();
for(char x:e) {
System.out.print(x+" ");
}
}
}
显示:
97 115 100 102 103 104 106 107 108
a s d f g h j k l
97 99 98 68 69 70 48 49 50 35 42
a c b D E F 0 1 2 # *
public static String valueOf(int i):将int类型的数据转换成字符串(重点),
value(Datatype i)方法可以将任何类型的数据转化成String类型;
public class StringTest3 {
public static void main(String[] args) {
int x1=123456;
char x2='x';
char[] a1= {'a','b','c'};
double x3=123435.454;
boolean x4=true;
System.out.println(String.valueOf(x1));
System.out.println(String.valueOf(x2));
System.out.println(String.valueOf(x3));
System.out.println(String.valueOf(x4));
System.out.println(String.valueOf(a1));
}
}
显示:
123456
x
123435.454
true
abc
public String toLowerCase():将字符串中所有的字符变成小写;public String toUpperCase():将字符串中所有的字符变成大写;
public class StringTest4 {
public static void main(String[] args) {
String s1="abcdef";
String s2="GHIJKL";
String s3="aBccUU";
System.out.println(s1.toLowerCase());
System.out.println(s2.toUpperCase());
System.out.println(s3.toLowerCase());
}
}
显示:
abcdef
GHIJKL
abccuu
String类型的其他功能:
public String replace(char oldChar,char newChar):将大字符串中的某个字符替换掉成新的字符;
public String replace(String oldStr,String newStr):将大串中的某个子字符串替换掉;
public String trim():去除字符串两端空格;
public class StringTest5 {
public static void main(String[] args) {
String s1="asdfghjkl";
String s2="*****";
char s4='#';
String ss=" sdf ";//有空格的字符串
System.out.println(ss);
ss=ss.trim();//去除前后空格
System.out.println(ss);
System.out.println(s1.replace('f', s4));//将字符串中的'f'替换为'#'
System.out.println(s1.replace('s','a'));//将字符串中的's'替换为'a'
System.out.println(s1.replaceAll("sdfg",s2));//将字符串中的"adfg"替换为"*****"
}
}
显示:
sdf
sdf
asd#ghjkl
aadfghjkl
a*****hjkl
public int compareTo(String anotherString)按字典顺序比较两个字符串;
public class Text345 {
public static void main(String[] args) {
String s="abc";
System.out.println(s.compareTo("def"));
}
}
显示:
-3
返回的是int型数据
如果比原字符串顺序后,返回一个小于0的数;
如果比原字符串顺序前,返回一个大于0的数;
如果顺序相等,返回0;
例题:
1、输入字符数据按字典顺序重新排序:
import java.util.Scanner;
public class BeautfulDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s=sc.nextLine();
char[] w=s.toCharArray();//将字符串转化为字符数组
for(int x=0;x<w.length-1;x++) {//选择排序
for(int y=x+1;y<w.length;y++) {
if(w[x]>w[y]) {
char t=w[x];
w[x]=w[y];
w[y]=t;
}
}
}
s=String.valueOf(w);//将字符数组转化为字符串
System.out.println(s);
}
}
显示:
请输入一个字符串:
febdca
abcdef
2、输入字符数组按字典顺序排序
public class BeautifulDemo2 {
public static void main(String[] args) {
String[] arr= {"Hello","World","Java","C++","Java"};//创建一个字符串数组
for(int x=0;x<arr.length-1;x++) {
for(int y=x+1;y<arr.length;y++) {//选择排序
if(arr[x].compareTo(arr[y])>0) {//使用compareTo()方法进行比较
String t=arr[x];
arr[x]=arr[y];
arr[y]=t;
}
}
}
for(String x:arr) {
System.out.println(x);
}
}
}
public class BeautifulDemo2 {
public static void main(String[] args) {
String[] arr= {"Hello","World","Java","C++","Java"};//创建一个字符串数组
for(int x=0;x<arr.length-1;x++) {
for(int y=0;y<arr.length-1-x;y++) {//冒泡排序
if(arr[y].compareTo(arr[y+1])>0) {//使用compareTo()方法进行比较
String t=arr[y];
arr[y]=arr[y+1];
arr[y+1]=t;
}
}
}
for(String x:arr) {
System.out.println(x);
}
}
}
显示:
Hello
Java
Java
World
3、统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
import java.util.Scanner;
public class StringTest2 {
public static void main(String[] args) {
//定义三个统计变量
int bigCount = 0 ;
int smallCount = 0 ;
int numberCount = 0 ;
//键盘录入对象
Scanner sc = new Scanner(System.in) ;
System.out.println("请您输入一个字符串:");
String line = sc.nextLine() ;
//将字符串遍历
for(int x =0 ; x < line.length() ; x ++) {
//可以通过索引获取每一个字符
char ch = line.charAt(x) ;
//判断他是否大写字母,小写字母,还是数字字符
if(ch >= 'a' && ch<='z') {
//小写字母字符
smallCount ++ ;
}else if(ch>='A' && ch<='Z') {
bigCount++;
}else if(ch>='0' && ch<'9') {
numberCount ++;
}
}
System.out.println("大写字母字符有:"+bigCount+"个");
System.out.println("小写字母字符有:"+smallCount+"个");
System.out.println("数字字母字符有:"+numberCount+"个");
}
}
显示:
请您输入一个字符串:
asddAFDF123asd
大写字母字符有:4个
小写字母字符有:9个
数字字母字符有:3个
字符串是常量;它们的值在创建之后不能更改。
但:
String s1="abc";
System.out.println(s1);
s1+="defg";
System.out.println(s1);
输出的是:abc
abcdefg
看似s1被改变了,其实在执行s1+="defg";时系统会把第一个创建的s1对象删除掉,从新创建一个s1对象。
以上代码等同于:
String s2="abc";
String s3=s2+"defg";
System.out.println(s3);
StringBuffer类
StringBuffer:线程程安全的可变字符序列;
可将字符串缓冲区安全地用于多个线程。可以在必要时对这些方法进行同步,因此任意特定实例上的所有操作就好像是以串行顺序发生的,该顺序与所涉及的每个线程进行的方法调用顺序一致。StringBuffer的构造方法:
StringBuffer() :无参构造的形式,初始容量16;
StringBuffer(int capacity) :指定容量构造一个字符串缓冲区;
StringBuffer(String str) 构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容;
StringBuffer的获取功能:
public int length()返回长度;
public int capacity()返回当前容量 (如果超过容量,系统自动分配(存储字符串的时候,英文的));
public class Demo2 {
public static void main(String[] args) {
StringBuffer s=new StringBuffer("axscdvfb");
System.out.println(s);
// StringBuffer s2="asdfghj";不能将字符串直接赋值给StringBuffer对象
System.out.println(s.length());//获取字符串长度
System.out.println(s.capacity());//获取当前容量
}
}
显示:
axscdvfb
8
24
StringBuffer的添加功能(实际开发中用的多):
public StringBuffer append(String/boolean....) :在字符串缓冲区中追加数据(在末尾追加),并且返回字符串缓冲区本身;
public StringBuffer insert(int offset,String str):将当前str字符串添加到指定位置处,它返回字符串缓冲区本身;
public class Demo2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer() ;
sb.append("hello") ;
sb.append('A');
sb.append(true);
sb.append(100);
sb.append("world") ;
sb.append(3.14) ;
System.out.println("sb:"+sb);
StringBuffer sb2=new StringBuffer("IJava");
String s="Love";
sb2.insert(1, s);
System.out.println(sb2);
}
}
显示:
sb:helloAtrue100world3.14
ILoveJava
StringBuffer的删除功能:
public StringBuffer deleteCharAt(int index):移除指定位置处的字符;
public StringBuffer delete(int start,int end):移除从指定位置处到end-1处的子字符串;(包前不包后);
public class Demo3 {
public static void main(String[] args) {
StringBuffer sf=new StringBuffer();
sf.append("Hello");
sf.append("*TheBeautiful");
sf.append("World");
System.out.println(sf);
sf.deleteCharAt(5);
System.out.println(sf);
sf.delete(5, 17);
System.out.println(sf);
}
}
显示:
Hello*TheBeautifulWorld
HelloTheBeautifulWorld
HelloWorld
StringBuffer的反转功能:
public StringBuffer reverse() :将缓冲区中的字符序列反转取代,返回它(字符串冲)本身;
public class Demo4 {
public static void main(String[] args) {
StringBuffer sd=new StringBuffer("avaJ爱我");
System.out.println(sd);
sd.reverse();
System.out.println(sd);
}
}
显示:
avaJ爱我
我爱Java
StringBuffer的截取功能:
public String substring(int start):从指定位置开始截取,默认截取到末尾,返回值不在是缓冲区本身,而是一个新的字符串;
public String substring(int start,int end) :从指定位置开始到指定位置结束截取,包前不包后,返回值不在是缓冲区本身,而是一个新的字符串;
public class Demo5 {
public static void main(String[] args) {
StringBuffer sg=new StringBuffer("abcdefghijk");
System.out.println(sg);
String sg2=sg.substring(2);
System.out.println(sg2);
String sg3=sg.substring(1, 5);
System.out.println(sg3);
}
}
显示:
abcdefghijk
cdefghijk
bcde
StringBuffer的替换功能:
public StringBuffer replace(int start,int end,String str)从指定位置到指定位置结束,用新的str字符串去替换,返回值是字符串缓冲区本身(包前也包后);
public class Demo5 {
public static void main(String[] args) {
StringBuffer sg=new StringBuffer("abcdefghijk");
System.out.println(sg);
sg.replace(4, 8, "****");
System.out.println(sg);
}
}
显示:
abcdefghijk
abcd****ijk
String和StringBuffer之间的相互转换
public class Demo6 {
public static void main(String[] args) {
//将String转换为StringBuffer;
String s="abcdefg";
StringBuffer s2=new StringBuffer();
// s2=s; 不能直接转换
s2.append(s);//方式一使用拼接方法
System.out.println(s2);
StringBuffer s3=new StringBuffer(s);//方式二使用构造方法
System.out.println(s3);
// StringBuffer s4=(StringBuffer)s;不能强制转换
// 将StringBuffer转换为String
StringBuffer t=new StringBuffer("qwertyui");
// String t2=t;也不可直接转换
String t2=t.toString();//使用toString()方法
System.out.println(t2);
String t3=new String(t);//使用构造方法
System.out.println(t3);
}
}
显示:
abcdefg
abcdefg
qwertyui
qwertyui
面试题:StringBuffer,String,StringBuilder的区别?
1)String是常量字符串,不可改变;StringBuffer和StringBuilder都是变量字符串;2)StringBuilder的运行速度大于StringBuffer,StringBuffer大于String;
3) StringBuilder:线程不安全的,不同步的,执行效率高,并且单线程中优先采用StringBuilder,StringBuffer:线程安全的,同步的,执行效率低;
Integer类
Integer是int类型的包装类类型;*基本类型对应都有一个包装类型,目的就为了将基本数据类型可以String类型之间进行互相转换
* byte Byte
* short Short
* int Integer
* long Long
* float Float
* double Double
* char character
* boolean Boolean
静态成员常量:
public static final int MAX_VALUE:值为 231-1 的常量,它表示 int 类型能够表示的最大值。
public static final int MIN_VALUE:值为 -231 的常量,它表示 int 类型能够表示的最小值。
构造方法:(没有无参构造)
Integer(int value) :构造一个新分配的 Integer 对象,它表示指定的 int 值。
Integer(String s) :造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
public class Demo7 {
public static void main(String[] args) {
Integer i=new Integer("10000");
// Integer i=new Integer("abcde");它表示 String 参数所指示的 int 值,不能是字符值
System.out.println(i);
Integer i2=new Integer(10000);
System.out.println(i2);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
}
显示:
10000
10000
2147483647
-2147483648
Integer提供了静态功能:
public static String toBinaryString(int i):以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
public static String toOctalString(int i):以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。
public static String toHexString(int i):以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
public static int parseInt(String s):将字符串类型所标示的int型转换为int型。
public class Demo8 {
public static void main(String[] args) {
int i=1024;
System.out.println(Integer.toBinaryString(i));
System.out.println(Integer.toOctalString(i));
System.out.println(Integer.toHexString(i));
String i2="1024";
int i3=Integer.parseInt(i2);
System.out.println(i3);
}
}
显示:
10000000000
2000
400
1024
Character类
Character 类在对象中包装一个基本类型 char 的值。Character 类型的对象包含类型为 char 的单个字段。 以确定字符的类别(小写字母,数字,等等);构造方法:
public Character(char value): 构造一个新分配的 Character 对象,用以表示指定的 char 值。
Character c=new Character('a');
System.out.println(c);
Character类的判断功能:
public static boolean isDigit(char ch):确定指定字符是否为数字。
public static boolean isLowerCase(int ch): 确定是否是小写字母字符。
public static boolean isUpperCase(int ch):确定是否大写字母字符。
public class Demo9 {
public static void main(String[] args) {
System.out.println("isDigit():"+Character.isDigit('A'));
System.out.println("isDigit():"+Character.isDigit('a'));
System.out.println("isDigit():"+Character.isDigit('0'));
System.out.println("----------------------");
System.out.println("isDigit():"+Character.isUpperCase('A'));
System.out.println("isDigit():"+Character.isUpperCase('a'));
System.out.println("isDigit():"+Character.isUpperCase('0'));
System.out.println("----------------------");
System.out.println("isDigit():"+Character.isLowerCase('A'));
System.out.println("isDigit():"+Character.isLowerCase('a'));
System.out.println("isDigit():"+Character.isLowerCase('0'));
}
}
显示:
isDigit():false
isDigit():false
isDigit():true
----------------------
isDigit():true
isDigit():false
isDigit():false
----------------------
isDigit():false
isDigit():true
isDigit():false
两个转换功能:
public static int toLowerCase(int codePoint):将字符转换为小写;
public static int toUpperCase(int codePoint):将字符转换为大写;
public class Demo10 {
public static void main(String[] args) {
System.out.println("a的大写字母为:"+Character.toUpperCase('a'));
System.out.println("B的小写字母为:"+Character.toUpperCase('B'));
}
}
显示:
a的大写字母为:A
B的小写字母为:B