day 17

String类、static关键字、Aarrys类、Math类

String类

/*
*
*String 类代表字符串。
*
*字符串的特点:
*1.字符串的内容永不变(重点)
*2.字符串是可以共享使用
*3.字符串效果上相当于一个char[],但是实际就是底层存储的是byte[]
*
*

常用的三种构造方法
1.public String() 创建一个空白的字符串,不包含如何内容
2.public String(char[] array) 根据 字符 数组类创建字符串
3.public String(byte[] array) 根据 字节 数组来创建字符串
最直接的方法
String str=“class5”
*/

public class Demo01String {
public static void main(String[] args) {
String str1=new String();//创建一个空的字符串
System.out.println(“第一个字符串”+str1);
char[] chararray= {‘a’,‘b’,‘c’,‘d’,‘e’};
String str2=new String(chararray);
System.out.println(chararray);//直接打印内容
System.out.println(str2);

		byte[] bytearray= {97,98,99};
		String str3=new String(bytearray);
		System.out.println(bytearray);//byte字节数组 数字转化为字母
		System.out.println(str3);
		//String str4=new
		//
		String str4="class5";
		System.out.println(str4);
	}

}
结果
第一个字符串
abcde
abcde
[B@2f92e0f4
abc
class5

package day17_code;
/*
*
*
*字符串常量也

  • int a = 5;
  • int b =6;
  • 对于基本数据类型 ==比较的是指
  • 对于引用类型 == 比较的地址值
    */

public class dDemo02Stringpoo1 {
public static void main(String[] args) {
String str1=“abc”;
String str2=“abc”;
System.out.println(str2);
char[] chararray= {‘a’,‘b’,‘c’};
String str3=new String(chararray);

	System.out.println(str1==str2);//true
	
	System.out.println(str1==str3);//false
	
	System.out.println(str3==str2);//false

	str2 ="cde";
	System.out.println(str2);
}

}
结果
abc
true
false
false
cde

package demo02;
/*
*
*
*

  • public string substring (index)

  • public string substring (int begin , int end )

  • */
    public class demo03substring {

    public static void main(String[] args) {
    // TODO 自动生成的方法存根
    String str1=“helloclass5”;//
    String str2=str1.substring(5);
    System.out.println(str1);
    System.out.println(str2);
    String str3=str1.substring(2,5);
    System.out.println(str3);
    }

}
结果
helloclass5
class5
llo

static类

/*

如果一个成员变量用static 关键字修饰,那么这个变量不属于对象,而属于所在的类 多个对象共享一份数据
*/

/*

如果用static修饰成员防腐漆, 变成静态方法,静态方法不属于对象,属于类
可以通过对象来调用
也可以通过类直接调用
*/

public class demo04staticMethod {
public static void main(String[] args) {
Myclass obj =new Myclass();
obj.method();//普通方法,必须通过对象调用
obj.methodstatic();//
Myclass.methodstatic();//
}
}
结果
这是成员方法
这是静态方法
这是静态方法

Aarrys类

/*

java.util.Aarrys 是一个肃州区相关的工具类,里面提供大量的静态方法
*备注:
*如果是数值, 默认按升序列 从小到大
*

如果是字符串,sort 默认字母升序
*/

public class demo03Arrays {
public static void main(String[] args) {
int [] intArray= {10,20,30};
System.out.println(intArray);
String intString=Arrays.toString(intArray);
System.out.println(intString);//[10, 20, 30]字符串

int[] array1= {3,3,5,7,8,9,3,5,6};
System.out.println(Arrays.toString(array1));//
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));//对数组进行排序 再转化成字符串输出


String[] array2= {"aaa","bbbb","ccc"};
System.out.println(Arrays.toString( array2));//
Arrays.sort( array2);
System.out.println(Arrays.toString( array2));

}
}

结果

[I@2f92e0f4
[10, 20, 30]
[3, 3, 5, 7, 8, 9, 3, 5, 6]
[3, 3, 3, 5, 5, 6, 7, 8, 9]
[aaa, bbbb, ccc]
[aaa, bbbb, ccc]

Math类

package demo03;

public class demo04Math {
public static void main(String[] args) {
//abs
System.out.println(Math.abs(3.14));
System.out.println(Math.abs(0));
System.out.println(Math.abs(-3.14));
//向上取整
System.out.println(Math.ceil(3.91));
System.out.println(Math.ceil(3.51));
System.out.println(Math.ceil(3.0001));
//向下取整
System.out.println(Math.floor(3.91));
System.out.println(Math.floor(3.51));
System.out.println(Math.floor(3.0001));
//
System.out.println(Math.round(4.5));
System.out.println(Math.round(4.1));
System.out.println(Math.round(4.6));
}
}

结果

3.14
0
3.14
4.0
4.0
4.0
3.0
3.0
3.0
5
4
5

20200806050630

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值