java自学笔记_JAVA自学笔记(3)

这篇博客详细介绍了JAVA中字符串的创建、特点、常量池、获取方法、比较、截取、转化等基础知识,并探讨了static关键字的用途,包括静态成员变量、静态方法和静态代码块的应用。此外,还展示了Arrays类在数组操作中的简单应用,如排序和倒序输出。
摘要由CSDN通过智能技术生成

JAVA的心动之旅

Day1 字符串String

1.0 字符串的特点以及创建一个字符串

2772ed0609e06b19524b892dbc6b3a5c.png

public class Practice {//构建字符串的3+1种方法

public static voidmain(String[] args) {//第一种

String one=newString();

System.out.println("输出的字符串为:"+one);//第二种

char str[]={'A','B','C'};

String two=newString(str);

System.out.println("输出的字符串为:"+two);//第三种

byte []bytes={97,98,99,100};

String three=newString(bytes);

System.out.println("输出的字符串为:"+three);//+1种 直接构造 字符串常量不可改变

String four="HelloWorld";

System.out.println("输出的字符串为:"+four);

}

}

打印结果:

输出的字符串为:

输出的字符串为:ABC

输出的字符串为:abcd

输出的字符串为:HelloWorld

2.0 字符串的常量池

08b9b5a35cecc7a3d4ade48f16d85d48.png

3.0 字符串的获取方法

b6c7bd9e398d382d181cf6814d65ea15.png

public classPractice {public static voidmain(String[] args) {

String one="Hello";

System.out.println("字符串的长度为:"+one.length());

String two="say ".concat(one);

System.out.println("字符串为:"+two);for(int i=0;i

{char ch=two.charAt(i);

System.out.print(ch+" ");

}

String three="o";

System.out.println("\n"+one.indexOf(three));

String four="h";

System.out.println(one.indexOf(four));

}

}

打印结果

字符串的长度为:5

字符串为:say Hello

s a y H e l l o

4

-1

4.0 字符串的比较 ==为地址的比较

fa289b6a4405f6c444cb37ddaa189e2a.png

5.0 字符串的截取

7d1d7f8454576a2c0678323c69a32eac.png

public classPractice {public static voidmain(String[] args) {

String one="hellobts";

String two=one.substring(5);

System.out.println(two);

String three=one.substring(0, 5);

System.out.println(three);

}

}

打印结果:

bts

hello

6.0 字符串的转化方法

9d5e326d990f2a63d16f3e61cd5ed8ed.png

public classPractice {public static voidmain(String[] args) {

String one="hellobts";char a[]=one.toCharArray();for(int i=0;i

{

System.out.print(a[i]+" ");

}

System.out.println();byte []bytes=one.getBytes();for(int i=0;i

{

System.out.print(bytes[i]+" ");

}

System.out.println();

String two=one.replace("hello", "love");

System.out.println(two);

}

}

打印结果:

h e l l o b t s

104 101 108 108 111 98 116 115

lovebts

Day2 static 关键字

1.0 static 概述

一旦用了static 关键字,那么这样的内容不再属于对象自己,它是属于类的,所以凡是本类的对象,都共享一份。

2.0 static 修饰成员变量

public classStudents {private static int idcounter=0;//每当创建一个对象(new)计数器++

staticString room;privateString name;private intage;private intid;publicStudents() {this.id=++idcounter;

}public Students(String name, intage) {this.name =name;this.age =age;this.id=++idcounter;

}public intgetId() {returnid;

}public void setId(intid) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}

}

public classPractice {public static voidmain(String[] args) {

Students one=new Students("田小娟",22);

one.room="101教室";

Students two=new Students("徐穗珍",22);

System.out.println("姓名:"+one.getName()+" "+"年龄:"+one.getAge()+" 教室:"+one.room+

" 学号"+one.getId());

System.out.println("姓名:"+two.getName()+" "+"年龄:"+two.getAge()+" 教室:"+two.room+

" 学号"+two.getId());

}

}

3.0 static 修饰方法

19d8ace274f3bdee48db76dec95083c3.png

4.0 静态代码块

e260739fa32779f94d8f3187edfea9c7.png

Day3 与Arrays相识

5259ffa5d3e59efa019f20185dec10f4.png

1.0 简单的应用

importjava.util.Arrays;public classPractice {public static voidmain(String[] args) {int []num={3,89,45,235,43,79};

String str=Arrays.toString(num);

System.out.println(str);

Arrays.sort(num);for(int i=0;i

{

System.out.print(num[i]+" ");

}

}

}

打印结果:

[3, 89, 45, 235, 43, 79]

3 43 45 79 89 235

2.0 字符串倒序

importjava.util.Arrays;public classPractice {public static voidmain(String[] args) {

String str="aihfjsdfhuwefwnf";//定义一个随机的字符串,并将字符串排序后倒序输出//需将字符串先转化为字符数组,才能使用Arrays

char []chars=str.toCharArray();

Arrays.sort(chars);for(int i=chars.length-1;i>=0;i--)

{

System.out.print(chars[i]+" ");//w w u s n j i h h f f f f e d a

}

}

}

Math类方法(百度)

1e864c4179a39f3fd5e0e96fe4f54353.png

488c06e86be84f98714505eadaf40db7.png

991745bd968f3e46165f6f4fd3aa4a4e.png

913417891cc44019ba57bd995a3f64d9.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值