包
作用: 包是用来分门别类的管理各种不同类的,类似于文件夹,建包有利于类的管理和维护。 格式: package 包名(用.分割层次) 注意事项: 要调用自己所在包下的其他类,可以直接调用。(同一个包下的类,可以直接使用) 要调用其他包下的类,则必须在当前类中导包, 才可以访问!导包格式:import 包名.类名;(idea自动导包) 要调用Java提供的类,也需要先导包才可以使用;但是Java.lang包下的类是不需要我们导包的,可以直接使用。 要调用多个不同包下的类,而这些类名正好一样,此时默认只能导入一个类,另一个类必须带包名访问(全类名/全限定类名:包名.类名)。
String
String代表字符串对象,可以用来封装字符串数据,并提供了很多操作字符串的方法。
String创建对象封装字符串数据的方式
方式一:直接使用双引号创建
String name = "张三";
方式二:调用String类的构造器初始化字符串对象
//1、无参构造
String s1 = new String();
System.out.println(s1);//空
//2、String类型的参数
String s2 = new String("你好");
System.out.println(s2);//你好
//3、char数组的参数
char[] chars = {'a','嗨','嘻'};
String s3 = new String(chars);
System.out.println(s3);//a嗨嘻
//4、byte数组的参数
byte[] bytes = {97,98,99};
String s4 = new String(bytes);
System.out.println(s4);//abc
String提供常用方法
- public int length() 获取字符串的长度返回(就是字符个数)
- public char charAt(int index) 获取某个索引位置处的字符返回
- public char[] toCharArray(): 将当前字符串转换成字符数组返回
- public String[] split(String regex) 把字符串按照某个字符串内容分割,并返回字符串数组回来
public class Demo2 {
public static void main(String[] args) {
String s = "学习Java";
//1.获取字符串的长度返回(就是字符个数)
System.out.println(s.length());//6
//2.获取某个索引位置处的字符
char c = s.charAt(3);
System.out.println(c);//a
//字符串的遍历
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
System.out.println(ch);
}
System.out.println("----------------");
//3.将当前字符串转换成字符数组返回
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i] + " ");
}//学 习 J a v a
System.out.println();
//4.把字符串按照某个指定内容分割成多个字符串,放到一个字符串数组中返回给我们
String s2 = "张无忌、周芷若、殷素素、赵敏";
String[] names = s2.split("、");
System.out.println(names.length);//4
System.out.println(names[2]);//殷素素
for (int i = 0; i < names.length; i++) {
System.out.print(names[i] + " ");
}//张无忌 周芷若 殷素素 赵敏
System.out.println();
String s3 = "www.com.baidu";
String[] s4 = s3.split("\\.");//用转义字符实现"."的识别
for (int i = 0; i < s4.length; i++) {
System.out.println(s4[i] + " ");
}
//www
//com
//baidu
}
}
- public String substring(int beginIndex, int endIndex) 根据开始和结束索引进行截取,得到新的字符串(包前不包后)
- public String substring(int beginIndex) 从传入的索引处截取,截取到末尾,得到新的字符串返回
public class Demo3 {
public static void main(String[] args) {
//1.根据开始和结束索引进行截取,得到新的字符串(包前不包后)
String s1 = "Java是最好的编程语言之一";
String r1 = s1.substring(5,10);
System.out.println(r1);//最好的编程
//从传入的索引处截取,截取到末尾,得到新的字符串返回
String r2 = s1.substring(5);
System.out.println(r2);//最好的编程语言之一
}
}
-
public String replace(CharSequence target, CharSequence replacement) 使用新值,将字符串中的旧值替换,得到新的字符串
-
public boolean contains(CharSequence s) 判断字符串中是否包含了某个字符串
-
public boolean startsWith(String prefix) 判断字符串是否以某个字符串内容开头,开头返回true,反之返回false
public class Demo4 {
public static void main(String[] args) {
//使用新值,将字符串中的旧值替换,得到新的字符串
String info = "这个电影简直是个垃圾,垃圾电影!!";
String r3 = info.replace("垃圾","**");
System.out.println(info);//这个电影简直是个垃圾,垃圾电影!!
System.out.println(r3);//这个电影简直是个**,**电影!!
//判断字符串中是否包含了某个字符串
String info2 = "我爱果果,果果也爱我!!Java,Java";
System.out.println(info2.contains("果果"));//true
System.out.println(info2.contains("java"));//false
System.out.println(info2.contains("Java"));// true
System.out.println(info2.contains("Java2"));// false
System.out.println("-------------");
//判断字符串是否以某个字符串内容开头,开头返回true
String s = "我爱果果呀!";
System.out.println(s.startsWith("我"));//true
System.out.println(s.startsWith("我爱"));//true
System.out.println(s.startsWith("我爱果"));//true
System.out.println(s.startsWith("我爱果果"));//true
System.out.println(s.startsWith("我爱果果2"));// false
}
}
String使用时的注意事项
-
只要是以”...“方式写出的字符串对象,会存储到字符串常量池,且相同内容的字符串只存储一份(这样可以节省内存)。
-
通过new方式创建字符串对象,每new一次还会产生一个新的对象放在栈内存中。
第一次new,堆中会产生两个”abc“对象,一个会存在常量池中,当再new时,只会产生一个”abc“对象了.
String s2 = new Strng("abc");与String s1 = "abc";是不一样的,他们的内存分配方式不同。
-
String的对象是不可变字符串对象。(因为内存中已存在的不可变,所以成为不可变字符串)
使用String实现用户登录案例
需求:
系统正确的登录名和密码是:hello/123456,请在控制台开发一个登录界面,接收用户输入的登录名和密码,判断用户是否登录成功,登录成功后展示:“欢迎进入系统!”,即可停止程序(注意:要求最多给用户三次登录机会)
分析:
-
提示用户输入用户名
-
提示用户输入密码
-
比较输入的用户名和密码,是否与系统正确的用户名和密码一致
-
一模一样:提示用户登录成功,退出程序
-
不一样:提示用户名或密码错误,重新返回登录,可尝试三次
-
三次输入均失败,停止程序
//方法1:
public class Demo1 {
public static void main(String[] args) {
//设置一个正确的用户名和密码
String okName = "hello";
String okPassworg = "123456";
//创建键盘输入工具
Scanner sc = new Scanner(System.in);
for (int i = 1; i <= 3; i++) {
System.out.println("请输入您的登录用户名:");
String name = sc.next();
System.out.println("请输入您的登陆密码:");
String password = sc.next();
if(name.equals(okName) && password.equals(okName)){
System.out.println("登录成功~," + name + "欢迎您!");
break;
}else {
if(3-i <= 0){
System.out.println("用户名或密码错误,您已经没有机会了,稍后再试!!!");
}else {
System.out.println("用户名或密码错误,您还有" + (3-i) + "次机会!");
}
}
}
}
}
//方法2:
public class Demo2 {
public static void main(String[] args) {
//1.开发一个登录界面
int i = 0;
for (i = 1; i <= 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请您输入登录名称:");
String loginName = sc.next();
System.out.println("请您输入登录密码:");
String password = sc.next();
//5.调用登录方法,判断是否登录成功
boolean l = login(loginName, password);
if(l){
System.out.println("恭喜您,欢迎进入系统~~");
break;
}else {
System.out.println("登录名或者密码错误,请您确认~~");
}
}
if(i>3) {
System.out.println("请稍后再试~~");
}
}
//2.开发一个登录方法,接收用户的登录名和密码,返回认证结果
public static boolean login(String loginName, String password){
String okLoginName = "hello";
String okPassword = "123456";
return okLoginName.equals(loginName) && okPassword.equals(password);
}
}
使用String来开发验证码
需求:
实现随机产生指定位数验证码,验证码的每位可能是数字、大写字母、小写字母。
分析:
-
设计一个方法,该方法接收一个整型参数,最终要返回对应位数的随机验证码。
-
方法内定义2个字符串变量:1个用来记住生成的验证码,1个用来记住要用到的全部字符。
-
定义for循环控制生成多少位随机字符,每次得到一个字符范围内的随机索引,根据索引提取该字符,把该字符交给code变量连接起来,循环结束后,在循环外返回code即可。
-
主程序中,调用该方法即可得到随机验证码了。
public class Demo8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要的验证码位数:");
int n = sc.nextInt();
System.out.println(getCode(n));
}
//根据传入的验证码长度,生成一个验证码并返回
public static String getCode(int num){
//定义一个验证码字符串,默认“”
String code = "";
//定义一个字符串,包含所有允许的验证码字符
String data = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random r = new Random();
//从1~num循环生成验证码
for(int i = 1;i <= num;i++){
int index = r.nextInt(data.length());
code += data.charAt(index);
}
return code;
}
}
编程练习
1.请定义一个方法用于判断一个字符串是否是对称的字符串,并在主方法中测试方法。 例如:"abcba"、"上海自来水来自海上"均为对称字符串。
public class Test01 {
public static void main(String[] args) {
String s1 = "abcba";
System.out.println(symmetry(s1));
String s2 = "上海自来水来自海上";
System.out.println(symmetry(s2));
String s3 = "上海自来水来自海上呀";
System.out.println(symmetry(s3));
}
//定义一个方法用于判断一个字符串是否是对称的字符串
public static boolean symmetry(String s){
for (int i = 0, j = s.length()-1; i < j; i++,j--) {
if(s.charAt(i) != s.charAt(j)){
return false;
}
}
return true;
}
}
2.请编写程序,由键盘录入一个字符串,统计字符串中英文字母和数字分别有多少个。
比如:Hello12345World中字母:10个,数字:5个。
实现步骤:
1.创建键盘录入Scanner类的对象
2.获取键盘录入的字符串,保存String变量str中
3.定义两个int变量count1和count2,初始值0,分别用来统计字母和数字的数量
4.遍历字符串(方式一: for + charAt(索引) 方式二: toCharArray() + for遍历数组)
4.1 获取当前字符保存char变量ch中
4.2 判断char变量ch中如果是字母,计数器count1的值增加1
4.3 判断char变量ch中如果是数字,计数器count2的值增加1
5.for循环结束后,输出count1和count2的值
public class Test02 {
public static void main(String[] args) {
//1.创建键盘录入Scanner类的对象
Scanner sc = new Scanner(System.in);
//2.获取键盘录入的字符串,保存String变量str中
System.out.println("请输入一个字符串: ");
String str = sc.nextLine();
//3.定义两个int变量count1和count2,初始值0,分别用来统计字母和数字的数量
int count1 = 0, count2 = 0;
//4.遍历字符串(方式一: for + charAt(索引) 方式二: toCharArray() + for遍历数组)
for (int i = 0; i < str.length(); i++) {
//4.1 获取当前字符保存char变量ch中
char ch = str.charAt(i);
//4.2 判断char变量ch中如果是字母,计数器count1的值增加1
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
count1++;
} else if (ch >= '0' && ch <= '9') {
//4.3 判断char变量ch中如果是数字,计数器count2的值增加1
count2++;
}
}
//5.for循环结束后,输出count1和count2的值
System.out.println("字符串: " + str + "中有" + count1 + "个字母");
System.out.println("字符串: " + str + "中有" + count2 + "个数字");
}
}
3.请编写程序,由键盘录入一个字符串,把字符串中的所有字母都变成大写后输出,再把字符串中的所有字母变成小写后输出。
比如:
键盘录入字符串:Hello12345World~!#$
控制台输出:
转换后的大写字符串: HELLO12345WORLD~!#$
转换后的小写字符串: hello12345world~!#$
public class Test03 {
public static void main(String[] args) {
//1.创建键盘录入Scanner类的对象
Scanner sc = new Scanner(System.in);
//2.获取键盘录入的字符串,保存String变量str中
System.out.println("请输入一个字符串: ");
String str = sc.nextLine();
//调用方法,把字符串str转换成大写并输出
System.out.println(myToUpperCase(str));
//调用方法,把字符串str转换成小写并输出
System.out.println(myToLowerCase(str));
}
/*
1.定义方法myToUpperCase,完成把方法参数字符串转换成大写字符串并返回转换后的结果
三要素:
1.方法名称: myToUpperCase
2.参数列表: String s
3.返回值类型: String
注意:
只有小写字母才可以转换成大写字母,其它字符一概不管
小写字母如何转换成对应的大写字母:
小写字母的ASCII码值 减去 32 就是对应的大写字母的ASCII码值
//Hello12345World~!#$
实现步骤:
1.定义String变量str,初始值"",用来拼接字符串
2.遍历方法参数字符串
2.1 获取当前字符,保存char变量ch中
2.2 判断如果ch中是小写字母,把ch中的小写字母变成大写字母
2.3 把ch中的字符拼接到str中
3.for循环结束后,直接返回str
*/
public static String myToUpperCase(String s) {
//1.定义String变量str,初始值"",用来拼接字符串
String str = "";
//2.遍历方法参数字符串
for (int i = 0; i < s.length(); i++) {
//2.1 获取当前字符,保存char变量ch中
char ch = s.charAt(i);
//2.2 判断如果ch中是小写字母,把ch中的小写字母变成大写字母
if (ch >= 'a' && ch <= 'z') {
//ch -= 32;//等价于: ch = (char)( ch - 32)
ch = (char) (ch - 32);
}
//2.3 把ch中的字符拼接到str中
str += ch;
}
//3.for循环结束后,直接返回str
return str;
}
/*
1.定义方法myToLowerCase,完成把方法参数字符串转换成小写字符串并返回转换后的结果
三要素:
1.方法名称: myToLowerCase
2.参数列表: String s
3.返回值类型: String
注意:
只有小写字母才可以转换成大写字母,其它字符一概不管
大写字母如何转换成对应的小写字母:
大写字母的ASCII码值 加上 32 就是对应的小写字母的ASCII码值
实现步骤:
1.定义String变量str,初始值"",用来拼接字符串
2.遍历方法参数字符串
2.1 获取当前字符,保存char变量ch中
2.2 判断如果ch中是大写字母,把ch中的大写字母变成小写字母
2.3 把ch中的字符拼接到str中
3.for循环结束后,直接返回str
*/
public static String myToLowerCase(String s) {
//1.定义String变量str,初始值"",用来拼接字符串
String str = "";
//2.遍历方法参数字符串
for (int i = 0; i < s.length(); i++) {
//2.1 获取当前字符,保存char变量ch中
char ch = s.charAt(i);
//2.2 判断如果ch中是大写字母,把ch中的大写字母变成小写字母
if (ch >= 'A' && ch <= 'Z') {
//ch += 32;//等价于: ch = (char)( ch + 32)
ch = (char) (ch + 32);
}
//2.3 把ch中的字符拼接到str中
str += ch;
}
//3.for循环结束后,直接返回str
return str;
}
}