Day05-Java基础

举报
java厂长 发表于 2021/10/19 19:25:23 2021/10/19
【摘要】 厂长熬夜给大家写的java基础文章,定时更新。

Day05-Java、

关于作者

  • 作者介绍

🍓 博客主页:作者主页

🍓 简介:JAVA领域优质创作者🥇、一名在校大三学生🎓、在校期间参加各种省赛、国赛,斩获一系列荣誉🏆。

🍓 关注我:关注我学习资料、文档下载统统都有,每日定时更新文章,励志做一名JAVA资深程序猿👨‍💻。

1、String类

1.1两种对象实例化方式

对于String在之前已经学习过了基本使用,就是表示字符串,那么当时使用的形式采取了直接赋值

public class StringText{
	public static void main(String args[]){
	String str =new String( "Hello");     //构造方法
	System.out.print(str);
	}
}

对于String而言肯定是一个类,那么程序之中出现的str应该就是这个类的对象,那么就证明以上的赋值操作实际上就表示要为String类的对象进行实例化操作。

但String毕竟是一个类,那么类之中一定会存在构造方法,String类的构造:

public class StringText{
	public static void main(String args[]){
	String str =new String( "Hello");     //构造方法
	System.out.print(str);
	}
}

发现现在也可以通过构造方法为String类对象实例化。

1.2字符串比较

如果现在有两个int型变量,如果想要知道是否相等,使用“==”进行验证。

public class StringText{
	public static void main(String args[]){
	int x = 10;
	int y = 10;
	System.out.print(x==y);
	}
}

换成String

public class StringText{
	public static void main(String args[]){
		String str1 = "Hello";
		String str2 = new String("Hello");
		String str3 = str2;     //引用传递
		System.out.print(str1== str2);          //false
		System.out.print(str1== str3);          //false
		System.out.print(str2== str3);          //ture
	}       
}

image-20210730181004695

现在使用了“==”的确是完成了相等的判断,但是最终判断的是两个对象(现在的对象是字符串)判断是否相等,属于数值判断------判断的是两个对象的内存地址数值,并没有判断内容,而想要完成字符串内容的判断,则就必须使用到String类的操作方法:public Boolean equals(String str)(将方法暂时变了)

public class StringText{
	public static void main(String args[]){
		String str1 = "Hello";
		String str2 = new String("Hello");
		String str3 = str2;     //引用传递
		System.out.print(str1.equals(str2));          //ture
		System.out.print(str2.equals(str3));          //ture
		System.out.print(str2.equals(str3));          //ture
	}       
}
1.3字符串常量是String的匿名对象

如果在程序之中定义了字符串(使用“””),那么这个就表示一个String对象,因为在各个语言之中没有关于字符串数据类型的定义,而Java将其简单的处理了,所以感觉上存在了字符串数据类型。

**范例:**验证字符串是对象的概念

public class NiMing{
	public static void main(String args[]){
		String str = "Hello";
		System.out.print("Hello".equals(str));     //通过字符串调用方法
	}       
}

匿名对象可以调用类之中的方法与属性,以上的字符串可以调用了equals()方法,那么它一定是一个对象。

**小技巧:**关于字符串与字符串常量的判断

例如:在实际工作之中会有这样一种操作,要求用户输入一个内容,之后判断此内容是否与指定内容相同。

public class NiMing{
	public static void main(String args[]){
		String str = "Hello";
        if(str.equals("Hello")){
		    System.out.print("条件满足");
		}   
	}       
}

但,既然数据是用户自己输入,那么就有可能没有输入内容。

public class TestDemo1{
	public static void main(String args[]){
		String str = null;
        if(str.equals("Hello")){
		    System.out.print("条件满足");
		}   
	}       
}

//报错
    Exception in thread "main" java.lang.NullPointerException
        at NiMing.main(TestDemo1.java:4)
//现在将代码反过来操作:
        public class TestDemo1{
	public static void main(String args[]){
		String str = null;
        if("Hello".equals(str)){
		    System.out.print("条件满足");
		}   
	}       
}

因为字符串常量是匿名对象,匿名对象不可能为null。

1.4String两种实例化方式区别

1、分析直接赋值方式

String str = "Hello";     //定义字符串

image-20210730181725468

发现现在只开辟额一块堆内存空间和一块栈内存空间。

2、构造方法赋值

String  str = new String("Hello");

image-20210730182113204

使用构造方法赋值的方式开辟的字符串对象,实际上会开辟两块空间,其中有一块空间就爱那个成为垃圾。

public class TestDemo2{public static void main(String args[]){		String str1 = new String("Hello");		String str2 = "Hello";   //入池		String str3 = "Hello";  //使用池中对象		System.out.print(str1==str2);          //false		System.out.print(str2==str3);          // ture 		System.out.print(str1==str3);          // false	}       }

通过上面的程序可以发现,使用构造方法实例化String对象,不会入池,只能自己使用。可是在String类之中为了方便操作提供了一种称为手工入池的方法:public String intern()。

public class TestDemo2{public static void main(String args[]){		String str1 = new String("Hello").intern();    //手工入池		String str2 = "Hello";   //入池		String str3 = "Hello";  //使用池中对象	    System.out.print(str1==str2);          //ture		 	 	    			System.out.print(str2==str3);          //ture		System.out.print(str1==str3);          //ture	}       }
1.5字符串常量不可改变

字符串类的操作特点决定:字符串不可能去修改里面的内容。

public class TestDemo3{	public static void main(String args[]){		String str = "Hello";		str += "World";		str += "!!!";		System.out.print(str);	}}

image-20210730200637440

通过以上的代码可以发现,字符串内容的更改,实际上改变的是字符串对象的引用过程,那么一下的代码应该尽量避免:

public class TestDemo3{	public static void main(String args[]){		String str = "Hello";		for(int x=0;x<1000;x++){			str += x;		}		System.out.print(str);	}       }
  • 字符串赋值只用直接赋值模式进行完成
  • 字符串的比较采用equals()方法进行实现
  • 字符串没有特殊的情况不要改变太多
1.6开发中String必用

任何一个类的文档由如下几个部分组成

  • 类的相关定义,包括这个类的名字,有哪些父类,有哪些接口。
  • 类的相关简介。包括基本使用
  • 成员摘要(field):属性就是一种成员,会列出所有成员的信息项
  • 构造方法说明(Constructor),列出所有构造方法的信息
  • 方法信息(Method),所有类中定义好的可以使用的方法
  • 成员、构造、方法的详细信息
1.7字符串和字符数组

字符串就是一个字符数组,所有在String类中有字符串转变为字符数组,字符数组转换为字符串的方法。

方法名称 类型 描述
public String(char[] value) 构造 将字符数组中的所有内容变为字符串
public String(char[] value, int offset, int count) 构造 将字符数组中的所有内容变为字符串 offset-开始 count-个数
public char charAt(int index) 普通 返回char指定字符的索引值
public char[] toCharArray() 普通 将字符串转化为字符数组

charAt方法

public class TestDemo4{	public static void main(String args[]){		String str = "Hello";		System.out.println(str.charAt(0));		//如果现在超过了字符串的长度,则会产生异常StringIndexOutOfBoundsException		System.out.println(str.charAt(10));	}}

字符串和字符数组的转化是重点

//字符串转化为字符数组public class TestDemo4{	public static void main(String args[]){		String str = "helloworld";		char data [] = str.toCharArray();		for(int i = 0; i < data.length; i++){		data[i] -= 32;	//转大写字母简化模式更简单			System.out.print(data[i] + "、");		}			}}
//字符数组转化为字符串public class TestDemo4{	public static void main(String args[]){		String str = "helloworld";		char data [] = str.toCharArray();		for(int i = 0; i < data.length; i++){		data[i] -= 32;	//转大写字母简化模式更简单			System.out.print(data[i] + "、");		}		System.out.println();		System.out.println(new String(data));//字符串数组全部转化为字符数组		System.out.println(new String(data,1,4));//字符串数组部分转化为字符数组	}}

image-20210730204340346

判断字符串是否由数字组成

public class TestDemo5{	public static void main(String args[]){		String str1 = "helloworld";		String str = "1234567890";		Judgenum(str);		Judgenum(str1);	}	public static void Judgenum(String str){		char data [] = str.toCharArray();		boolean judge = true;		for(int i = 0; i < data.length; i++){			if(data[i]>= '0' && data[i]<= '9'){				judge = false;			}		}		if(judge){			System.out.println(str+"是由字母组成");		}else			System.out.println(str+"是由数字组成");	}}

image-20210730214040542

1.8字节和字符串
方法名称 类型 描述
public String(byte[] bytes) 构造 将部分字节数组变为字符串
public String(byte[] bytes, int offset,int length) 构造 将部分字节数组变为字符串 bytes——要解码为字符的字节 offset——要解码的第一个字节的索引 length——要解码的字节数
public byte[] getBytes() 普通 将字符串变为字节数组
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException 普通 编码转换编码
//将字符串通过字节流转化为大写public class TestDemo6{	public static void main(String args[]){		String str = "helloworld";		byte data [] = str.getBytes();//字符串转换为字节数组		for(int i = 0; i < data.length ; i++){			System.out.print(data[i]+"、");			data[i] -= 32;		}		System.out.println(new String(data));//字节数组转化为字符串	}}

image-20210730221957667

一般情况下,在程序之中如果想要操作字节数组只有两种情况:

**1、**需要进行编码的转化;

2、 数据要进行传输的时候。

**3、**二进制文件适合字节处理

1.9字符串比较
方法名称 类型 描述
public boolean equals(String anObject) 普通 区分大小写比较
public boolean equalsIgnoreCase(String anotherString) 普通 不区分大小写比较
public int compareTo(String anotherString) 普通 比较两个字符串的大小关系

如果现在要比较两个字符串的大小关系,那么就必须使用comepareTo()方法完成,而这个方法返回int型数据,而这个int型数据有三种结果:大于(返回结果大于0)、小于(返回结果小于0)、等于(返回结果为0).

public class CompareTo{	public static void main(String args[]){	String str1 = "HELLO";	String str2= "hello";	System.out.println(str1.compareTo(str2));		}}
1.10字符串查找
方法名称 类型 描述
public boolean contains(String s) 普通 判断一个子字符串是否村存在
public int indexOf(String str) 普通 返回字符串中第一次出现字符串的索引
public int indexOf(String str, int fromIndex) 普通 从指定地方开始查找子字符串的位置
public int lastIndexOf(String str) 普通 从后向前查找子字符串的位置
public int lastIndexOf(String str, int fromIndex) 普通 从指定位置由后向前查找
public boolean startsWith(String prefix) 普通 从头判断是否以某字符串开头
public boolean startsWith(String prefix,int toffset) 普通 从指定位置判断是否以字符串开头
public boolean endsWith(String suffix) 普通 判断以某字符串结尾
public class TestDemo7{	public static void main(String args[]){		String str = "helloworld";		System.out.println(str.contains("world"));		//true		//使用indexOf()进行查找		System.out.println(str.indexOf("world"));		System.out.println(str.indexOf("java"));		//JDK1,5之前这样使用		if(str.indexOf() != -1){			System.out.println("可以查找到指定的内容");		}	}}
  1. 基本上所有的查找现在都是通过contains()方法完成。
  2. 需要注意的是,如果内容重复indexOf()它只能返回查找的第一个位置。
  3. 在进行查找的时候往往会判断开头或结尾。
public class TestDemo7{	public static void main(String args[]){		String str = "**@@helloworld##";		System.out.println(str.startsWith("**"));	//true		System.out.println(str.startsWith("@@",2));	//true		System.out.println(str.endsWith("##"));	//true	}}
1.11字符串的替换
方法名称 类型 描述
public String replaceAll(String regex,String replacement) 普通 替换所有的内容
public String replaceFirst(String regex,String replacement) 普通 替换首内容
public class TestDemo7{	public static void main(String args[]){		String str = "**@@helloworld##";				System.out.println(str.replaceAll("l","_"));	//**@@he__owor_d##	}}
1.12字符串的拆分
方法名称 类型 描述
public String[] split(String regex) 普通 将字符串全部拆分
public String[] split(String regex,int limit) 普通 将字符串部分拆分
public class TestDemo8{	public static void main(String args[]){		String str = "hello world hello zsr hello csdn";		String result [] = str.split(" ");	//按照空格进行拆分		//hello、world、hello、zsr、hello、csdn、		for(int i = 0; i < result.length ; i++){			System.out.print(result[i]+"、");			}		System.out.println();		//部分拆分		String result1 [] = str.split(" ",3);	//按照空格进行拆分		//第二个参数 从第几个位置开始不进行拆分操作		//hello、world、hello zsr hello csdn、		for(int i = 0; i < result1.length ; i++){			System.out.print(result1[i]+"、");			}	}}
//拆分ip地址public class TestDemo9{//吴国发现内容无法拆分,就需要用到“\\”进行转义	public static void main(String args[]){	//120	//11	//219	//223:57114		String str = "120.11.219.223:57114";		String result [] = str.split("\\.");		for(int i = 0 ; i < result.length ; i++){			System.out.println(result[i]);		}	}}
1.12字符串的截取
方法名称 类型 描述
public String substring(int beginIndex) 普通 从指定位置截取到结尾
public String substring(int beginIndex,int endIndex) 普通 截取部分内容
public class TestDemo10{	public static void main(String args[]){		String str = "helloworld";		//world		System.out.println(str.substring(5));		//hello		System.out.println(str.substring(0,5));		}}
1.13其他操作方法
方法名称 类型 描述
Public String trim() 普通 去掉左右空格,保留中间空格
public String toUpperCase() 普通 将全部字符串转大写
public String toLowerCase() 普通 将全部字符串转小写
public String intern() 普通 字符串入对象池
public String concat() 普通 字符串连接

思考题:

1.现在给出了如下一个字符串格式:“姓名:成绩|姓名:成绩|姓名:成绩”,例如:给定的字符串是:“Tom:90|Jerry:80|tony:89”,要求可以对以上数据进行处理,将数据按照如下的形式显示:姓名:Tom,成绩:90;
public class Exam1{	public static void main(String args[]){		String str = "Tom:90|Jerry:80|tony:89";		String data [] = str.split("\\|");		for(int i = 0 ; i < data.length ; i++){			String result [] = data[i].split(":");			System.out.print("姓名 = " + result[0] + ",");			System.out.println("年龄 = " + result[1]);		}				/*姓名 = Tom,年龄 = 90		  姓名 = Jerry,年龄 = 80		  姓名 = tony,年龄 = 89*/	}}
2.1、 给定一个email地址,要求验证其是否正确,提示:可以简单的验证一下,重点验证“@”和“.”。标准如下:

1.email长度不短于5

2.@和.不能做开头或结尾

3.@和.顺序要有定义

public class Exam2{	public static void main(String args[]){		String email = "1016942589.@qqcom";		char date[] = email.toCharArray();		if (date.length>5&&email.startsWith("@")==false && email.startsWith(".")==false && email.endsWith("@")==false&&email.endsWith(".")==false && email.indexOf(".") >email.indexOf("@"))		{System.out.println("正确");		}else{System.out.println("错误");}	}}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。