final 在 Java 中有什么作用???Java 中的 Math.round(-1.5) 等于多少???String 是基本数据类型吗???字符串常用操作有哪些???

4. final 在 Java 中有什么作用???Java 中的 Math.round(-1.5) 等于多少???String 是基本数据类型吗???字符串常用操作有哪些???

完成:第一遍

4. final 在 Java 中有什么作用???

final可修饰类、属性和方法
特征:凡是引用final关键字的地方皆不可修改

修饰类:该类不能被继承;
修饰方法:该方法不能被重写;
修饰变量:表示变量只能一次赋值以后值不能被修改(常量)

final修饰基本数据类型数据, 数据的值初始化后将不能被改变
当final修饰引用类型数据, 也就是修饰对象时,
引用在初始化后将永远指向一个内存地址, 不可修改
但该内存地址中保存的对象信息, 是可修改的

5. Java 中的 Math.round(-1.5) 等于多少???

-1

6. String 是基本数据类型吗???

不是, String是一个对象,是引用数据类型

基础的数据类型有
int,short,long,char,boolean,float,double,byte

对应的基本类型的包装类:
Integer,Short,Long,Character,Float,Double,Byte

7. 字符串常用操作有哪些???

方法:charAt
作用:取出指定索引的字符

String str = "hello";
char c = str.charAt(0);

方法:toCharArray
作用:将字符串变为字符数组
知识点:如果想将字符数组或字节数据转换为字符串,可以构造方法传入


String str = "hello";
char[] data = str.toCharArray();

方法:equals
作用:相等判断


String stra = "Hello";
String strb = "HELLO";
System.out.println(stra.equals(strb));// false
System.out.println(stra.equalsIgnoreCase(strb));// true

方法:compareTo()
作用:判断大小


		String stra = "Hello";
		String strb = "HELLO";
		System.out.println(stra.compareTo(strb));
		// 可以利用大小等于0的方式来判断大小
		if (stra.compareTo(strb) > 0) {
			System.out.println("大于");

方法:indexOf
作用:返回满足条件单词的第一个字母索引

	
String str = "helloworld";
// 返回满足条件单词的第一个字母索引
System.out.println(str.indexOf("world"));

方法:contains
作用:返回boolean可以查询到数据


String str = "helloworld";
if (str.contains("world")) {
    	System.out.println("可以查询到数据");
}

方法:startsWith
方法:endsWith
作用:开头或结尾判断

String str = "##@@helloworld**";
System.out.println(str.startsWith("##"));
System.out.println(str.startsWith("@@", 2));
System.out.println(str.endsWith("**"));

方法:replaceAll
方法:replaceFirst
作用:返回替换的结果

String str = "helloworld";
String resultA = str.replaceAll("l", "_");
String resultB = str.replaceFirst("l", "_");
System.out.println(resultA);
System.out.println(resultB);

方法:substring
作用:从字符串之中截取部分的子字符串


String str = "helloworld";
String resultA = str.substring(5);
String resultB = str.substring(0, 5);
System.out.println(resultA);
System.out.println(resultB);

方法:split
作用:将字符串按照指定的内容拆分为数组


        String str = "hello world java world";
		String result[] = str.split(" ");
		for (int x = 0; x < result.length; x++) {
			System.out.println(result[x]);
		}

方法:concat或者+号拼接
作用:字符串的连接


		String stra = "hello";
		String strb = stra + "world";
//String strb = stra.concat("world");等价

方法:toLowerCase
方法:toUpperCase
作用:转小写与大写


        String str = "(*(*Hello(*(*";
		System.out.println(str.toLowerCase());
		System.out.println(str.toUpperCase());
		

方法:trim
作用:去掉空格

        String str = "      Hello world    ";
		System.out.println(str);
		System.out.println(str.trim());

方法:length
作用:取得字符串的长度
知识点: 数组对象.length;
String对象.length()

        String str = "helloworld";
		System.out.println(str.length());

方法:isEmpty
作用:判断是否为空字符串
如果觉得isEmpty不方便可以使用"".equals(str)

        String str = "helloworld";
		System.out.println(str.isEmpty());//false
		System.out.println("".isEmpty());//true

你可能感兴趣的:(【Java,基础】)