0
点赞
收藏
分享

微信扫一扫

22、JAVA进阶——实体类应用

✅作者简介:热爱国学的Java后端开发者,修心和技术同步精进。

🍎个人主页:乐趣国学的博客

🍊个人信条:不迁怒,不贰过。小知识,大智慧。

💞当前专栏:JAVA开发者成长之路

✨特色专栏:国学周更-心性养成之路


🥭本文内容:JAVA进阶——实体类应用

目录

​​一、Java API​​

​​二、枚举​​

​​        1、 枚举概述​​

​​        2、使用枚举类的作用及好处​​

​​三、包装类​​

​​        1、 包装类概述​​

​​        2、 包装类和基本数据类型的转换​​

​​四、Math类​​

​​五、Random类​​

​​六、String、StringBuffer、StringBuilder类​​

​​        1、 String类概述​​

​​        2、 String类常用方法​​

​​        3、使用StringBuffer类处理字符串​​

​​        4、使用StringBuilder类处理字符串​​

​​        5、 String类、StringBuffer类及StringBuilder类对比​​

22、JAVA进阶——实体类应用_包装类

一、Java API

(1)Java应用程序编程接口(Java Application Programming Interface,Java API)是运行库的集合,预先定义了一些接口和类。

(2)程序员可以直接使用已经打包好的接口和类来开发具体的应用,节约了程序员大量的时间和精力。

(3)API除了有“应用程序编程接口”的意思外,还特指API的说明文档,也称帮助文档。

(4)Java语言的强大之处在于它提供了多种多样的类库,从而大大提高了程序员的编程效率和质量。

(5)Java API提供了如下常用的包。

        -->java.lang:编写Java程序时最广泛使用的包,自动导入到所有的程序中,包含了Java程序的基础类和接口。包装类、Math类、String类等常用的类都包含在此包中,java.lang包还提供了用于管理类的动态加载、外部进程创建、主机环境查询和安全策略实施等“系统操作”的类。

        -->java.util:包含了系统辅助类,特别是Collection、List和Map等集合类。

        -->java.io:包含了与输入/输出有关的类,如文件操作等类。

        -->java.net:包含了与网络有关的类,如Socket、ServerSocket等类。

        -->java.sql:包含了与数据库相关的类,如Connection、Statement等类。

二、枚举

1、 枚举概述

        (1)从Java SE 5.0开始,Java程序设计语言引入了一种新的类型——枚举(Enum)。

        (2)枚举是指由一组固定的常量组成的类型。使用关键字enum定义。

        (3)定义枚举语法格式如下:

                [Modifer] enum enumName{

                        enumContantName[,enumContantName2...[;]]

                        //[field,method]

                }

                -->Modifer是访问修饰符,如public等。

                -->enum是关键字。

                -->enumContantName[,enumContantName2...[;]]表示枚举常量列表,枚举常量之间以逗号隔开。

                -->//[field,method]表示其他的成员,包括构造方法,置于枚举常量的后面。

                -->在枚举中,如果除了定义枚举常量,还定义了其他成员,则枚举常量列表必须以分号(;)结尾。

(4)枚举其实就是一种类型,是java.lang.Enum类的子类,继承了Enum类的许多有用的方法。

2、使用枚举类的作用及好处

        (1)在Java中,通常使用枚举表示一组个数有限的值,用于实现对输入的值进行约束检查。

        (2)在程序中使用枚举的好处总结如下:

                -->枚举可以使代码更易于维护,有助于确保为变量指定合法的、期望的值。

                -->枚举更易于编程时输入,使用枚举赋值,只需要输入枚举名,然后输入一个点(.),就能将所有的值显示出来。

                -->枚举使代码更清晰,允许使用描述性的名称表示数据,使用时直观方便。

        (3)案例:定义一个性别的Genders枚举类和Student测试类

Genders枚举类代码:

package cn.bdqn.demo01;

public enum Genders {
,,东方不败
}

Student测试类代码:

package cn.bdqn.demo01;

public class Student {
String name;
//将性别定义为Genders枚举类型,那么在给对象的性别属性赋值的时候,只能赋予枚举类中已经声明好的数据
Genders sex;

public static void main(String[] args) {

Student stu = new Student();
stu.name = "尼古拉斯·赵四";
stu.sex = Genders.;
}

}

三、包装类

1、 包装类概述

        (1)Java语言是面向对象的,但是Java中的基本数据类型却不是面向对象的,这在实际开发中存在很多的不便。为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类,称为包装类。

        (2)包装类均为java.lang包中,包装类和基本数据类型的对应关系如下表所示。

22、JAVA进阶——实体类应用_jvm_02


        (3)包装类的用途主要有两个:

           -->包装类作为和基本数据类型对应的类存在,方便对象的操作。

           -->包装类包含每种基本数据类型的相关属性,如最大值、最小值等,以及相关的操作方法。

2、 包装类和基本数据类型的转换

        (1)基本数据类型转换为包装类

       在Java中,基于基本数据类型数据创建包装类对象通常可以采用如下两种方式。

          1)使用包装类的构造方法

                        -->public Type(type value)。

                        -->public Type(String value)。

                其中,Type表示包装类,参数type为基本数据类型。

                不能使用第二种构造方法创建Character类的包装类对象,只能是Character charValue = new Character('x')这种形式。

          2)使用包装类的valueOf()方法

                   a)包装类中一般包含静态的重载的valueOf()方法,它可以接收基本数据类型数据和字符串作为参数并返回包装类的对象。

                   b)以Integer包装类为例,valueOf()方法的定义如下表所示

22、JAVA进阶——实体类应用_字符串_03

                   c)Character类的valueOf()方法只有一个版本的定义,即valueOf(char c),它返回一个表示指定char值的Character对象。        

package cn.bdqn.demo02;

public class Demo02 {

public static void main(String[] args) {

// 所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例
byte num1 = 10;
Byte byte1 = new Byte(num1);
System.out.println(byte1);

short num2 = 100;
Short short1 = new Short(num2);

int num3 = 200;
Integer int1 = new Integer(num3);

long num4 = 300;
Long long1 = new Long(num4);

float num5 = 12.5F;
Float float1 = new Float(num5);

double num6 = 99.99;
Double double1 = new Double(num6);

char num7 = '你';
Character char1 = new Character(num7);

boolean num8 = false;
Boolean boolean1 = new Boolean(true);

//除Character类外,其他包装类可将一个字符串作为参数构造它们的实例
Byte byte2 = new Byte("100");
System.out.println(byte2);

Short short2 = new Short("129");

Integer int2 = new Integer("1234");

Long long2 = new Long("9999");

Float float2 = new Float("12.5");
System.out.println(float2);

Double double2 =new Double("12.99");

Boolean boolean2 = new Boolean("qwert");
System.out.println(boolean2);

/*
* 注意:
* Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),
* 则该Boolean对象表示true,否则表示false
*
* 当Number包装类构造方法参数为String 类型时,字符串不能为null,
* 且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常
*
*/

}

}

        (2)包装类转换成基本数据类型

        包装类转换成基本数据类型通常采用如下的方法:public type typeValue();

        其中,type指的是基本数据类型,如byteValue()、charValue()等,相应的返回值为byte、char。

package cn.bdqn.demo02;

public class Demo03 {

public static void main(String[] args) {
// 包装类常用方法
//XXXValue():包装类转换成基本类型

Integer int1 = new Integer("100");
int num1 =int1.intValue();
System.out.println(num1);

//static toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)
String result1 =Boolean.toString(true);
System.out.println(result1);
//toString():将包装类编程字符串
Double num2 = new Double(12.55);
String result2 =num2.toString();
System.out.println(result2);

//static parseXXX():把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)
int result3 =Integer.parseInt("123");
System.out.println(result3);

//所有包装类都有如下方法(基本类型->包装类)
//public static Type valueOf(type value)
Float num3 = Float.valueOf(22.5F);
//除Character类外,其他包装类都有如下方法(字符串->包装类)
// public static Type valueOf(String s)
Float num4 =Float.valueOf("99.9");



}

}

         (3)基本类型和包装类的自动转换

        1)在Java SE 5.0版本之后程序员不需要编码实现基本数据类型和包装类之间的转换,编译器会自动完成。

             Integer intObject = 5; //装箱:基本数据类型转换为包装类

             int intValue = intObject; //拆箱:包装类转换为基本数据类型

        2)虽然Java平台提供了基本数据类型和包装类的自动转换功能。程序员在程序中也不能只使用对象,而抛弃了基本数据类型。

        3)包装对象只有在基本数据类型需要用对象表示时才使用,包装类并不是用来取代基本数据类型的。

package cn.bdqn.demo02;

public class Demo04 {

public static void main(String[] args) {
//装箱和拆箱:实现的是基本数据类型和包装类对象之间的自动转换
//装箱:将基本数据类型直接赋值给包装类对象
//拆箱:将包装类对象直接赋值给基本数据类型变量

//装箱
int num1 = 10;
Integer int1 =num1;
Integer int2 = 100;

//拆箱
Character char1 = new Character('h');
char num2 =char1;

//JDK1.5之后,基本数据类型和包装类对象可以直接进行数学运算
Integer int3 = new Integer(300);
int num3 = 300;
Integer int4 =int3+num3;
int sum =int3+num3;

}

}

 四、Math类

(1)java.lang.Math类提供了一些基本数学运算和几何运算的方法。

(2)此类中的所有方法都是静态的。这个类是final类,因此没有子类。

(3)Math类常见方法如下:

        -->static double abs(double a):返回double值的绝对值。例如,Math.abs(-3.5)返回3.5。

        -->static double max(double a,double b):返回两个double值中较大的一个。

                例如,Math.max(2.5,90.5);返回90.5。

        -->static double random():返回一个随机的double值,该值大于等于0.0且小于1.0。

(4)随机获取一个[num1,num2)之间的整数(num2>num1)公式:
        int num = (int)(Math.random()*(num2-num1)+num1);

public static void main(String[] args) {

double num =Math.PI;
System.out.println(num);

//求一个数的绝对值,正数的绝对值是它本身,负数的绝对值是其相反数
System.out.println(Math.abs(100));
System.out.println(Math.abs(-100));

//求近似数
//ceil(num):返回比num大的最小的整数
System.out.println(Math.ceil(9.1));//10.0
//floor(num):返回比num小的最大的整数
System.out.println(Math.floor(9.8));//9.0
//round(num):四舍五入
System.out.println(Math.round(9.9));

//求最大值和最小值
System.out.println(Math.max(25, 18));
System.out.println(Math.min(99, 33));

//求一个数的指定次幂
System.out.println(Math.pow(2, 10));
System.out.println(Math.pow(8, 3));

//求随机数
//Math.random():返回一个[0.0,1.0)之间的double类型数据
double random1 = Math.random();
System.out.println(random1);
//Math.random()*10:返回一个[0.0,10.0)之间的double类型的数据
double random2 = Math.random()*10;
System.out.println(random2);
//(int)(Math.random()*10):返回一个[0,10)之间的int类型的数据
int random3 = (int)(Math.random()*10);
System.out.println(random3);
//(int)(Math.random()*(num2-num1)+num1):返回一个[num1,num2)(num2>num1)之间的int类型的数据
//随机获取一个[63,81)之间的int类型的数据
int random4 =(int)(Math.random()*(81-63)+63);
System.out.println(random4);
}

五、Random类

22、JAVA进阶——实体类应用_包装类_04


 代码展示:

public static void main(String[] args) {
//创建Random类对象
Random random = new Random();

//获取随机数
int num1 =random.nextInt();
System.out.println(num1);

int num2 =random.nextInt(10);
System.out.println(num2);

System.out.println(random.nextBoolean());

Random r1 = new Random(100);
Random r2= new Random(100);
System.out.println(r1.nextInt());
System.out.println(r2.nextInt());

}

六、String、StringBuffer、StringBuilder类

1、 String类概述

        (1)在Java中,字符串被作为String类型的对象来处理。

        (2)String类位于java.lang包中,默认情况下,该包被自动导入所有的程序。

        (3)创建String对象的方法如下代码所示

                String s = "Hello World";

                String s = new String("Hello World");

        (4)String类提供了许多有用的方法来操作字符串,比如获取字符串长度、对两个字符串进行比较、连接两个字符串以及提取一个字符串中的某一部分。

        (5)字符串是一个字符序列,每一个字符都有自己的位置,字符串事实上也是一个字符数组,因此它的索引位置从0开始到(字符串长度-1)结束。

2、 String类常用方法

        (1)求字符串长度:length()

        (2)字符串比较:equals(字符串2)

        (3)忽略带小写的字符串比较:equalsIgnoreCase(字符串2)

        (4)转换字符串中的英文字符为小写:toLowerCase()

        (5)转换字符串中的英文字符为大写:toUpperCase()

public static void main(String[] args) {
// 创建String类对象
String str1 = new String("qwertyuiop");
String str2 = "QWErtyuiop";

//String类中包含很多方法实现不同的功能
//char charAt(int index) 返回指定索引处的 char 值。
char ch1 =str1.charAt(2);
System.out.println("下标为2的字符:"+ch1);

//int length() 返回此字符串的长度。
int length =str1.length();
System.out.println("str1的长度:"+length);

// boolean equals(Object anObject) 将此字符串与指定的对象比较。 String类中重写了Object类中的equals()方法,比较的是两个字符串的内容是否相同
boolean result =str1.equals(str2);
System.out.println("str1和str2字符串内容相同:"+result);
// boolean equalsIgnoreCase(String anotherString) 将此 String 与另一个 String 比较,不考虑大小写
System.out.println("str1和str2字符串内容相同:"+str1.equalsIgnoreCase(str2));

String str3 = "QWERT";
String str4 = "asdfg";
// String toLowerCase() 将此 String 中的所有字符都转换为小写
//String toUpperCase() 将此 String 中的所有字符都转换为大写。
String newStr3 =str3.toLowerCase();
System.out.println(str3);
System.out.println(newStr3);
System.out.println(str4.toUpperCase());
System.out.println(str4);


}

        (6)字符串的连接:concat(字符串2)

public static void main(String[] args) {

String str1 = "hello";
String str2 = "java";
System.out.println(str1 + str2);

// String concat(String str) 将指定字符串连接到此字符串的结尾。
String newString = str1.concat(str2);
System.out.println(newString);
System.out.println(str1);
System.out.println(str2);

System.out.println(3+5+"hello");//8hello
System.out.println(3+"hello"+5);//3hello5
System.out.println("hello"+3+5);//hello35
System.out.println(3+""+5+"hello");//35hello

}

        (7)字符串提取和查询

22、JAVA进阶——实体类应用_java_05


public static void main(String[] args) {
// 字符串常用提取方法
String str = "qweratyuioperauiop";

//int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。
int index1 =str.indexOf(97);
System.out.println(index1);//4
int index2 = str.indexOf('a');
System.out.println(index2);//4
// int indexOf(int ch, int fromIndex) 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
int index3 = str.indexOf(97, 5);
System.out.println(index3);//13

// int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引(指定字符串中第一个字符的下标)。
int index4=str.indexOf("aty");
System.out.println(index4);
//int indexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
int index5 =str.indexOf("aty", 5);
System.out.println(index5);

//int lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引。
int index6 =str.lastIndexOf(97);
System.out.println(index6);

//String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
// String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串,包含开始下标的字符,不包含结束下标的字符。
String subString1=str.substring(5);
System.out.println(str);
System.out.println(subString1);
String subString2 =str.substring(5, 10);
System.out.println(subString2);

// String trim() 返回字符串的副本,忽略前导空白和尾部空白。
String string = " qwert yuiop ";
String trimString=string.trim();
System.out.println(string);
System.out.println(trimString);
}

        (8)字符串拆分:split(separator,limit)

                -->separator是可选项,表示根据匹配指定的正则表达式来拆分此字符串。如果匹配不上,则结果数组只有一个元素,即此字符串。

                -->limit可选项,该值用来限制返回数组中的元素个数。

public static void main(String[] args) {
String song = "长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";

// String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。
String[] strs = song.split(" ");
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}

String str = "qwertyuiop";
// boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
System.out.println(str.endsWith("iops"));
// boolean startsWith(String prefix, int toffset)
// 测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
System.out.println(str.startsWith("qwe"));

// byte[] getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte
// 数组中
byte[] bytes = str.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.print(bytes[i] + " ");
}

System.out.println();

// char[] toCharArray() 将此字符串转换为一个新的字符数组。
char[] chs =str.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.print(chs[i]+" ");
}

}

3、使用StringBuffer类处理字符串

        (1)StringBuffer类也是Java提供的用于处理字符串的一个类,而且它是比String类更高效的存储字符串的一种引用数据类型。

        (2)StringBuffer类对字符串进行连接操作时,使用StringBuffer类可以大大提高程序的执行效率。

        (3)如何使用StringBuffer类

                StringBuffer类位于java.util包中,是String类的增强类。StringBuffer类提供了很多方法可供使用。

                StringBuffer 对象名 = new StringBuffer("字符串内容");

        (4)常用的StringBuffer类方法

                1)toString()方法:将StringBuffer类型的字符串转换为String类型的对象。

                2)append(参数)方法:将参数连接到字符串后,可以将任何类型的值追加到字符串后。

                3)insert(位置,参数)方法:将参数插入到字符串指定位置后并返回。参数值可以是包括String的任何类型。

4、使用StringBuilder类处理字符串

        (1)java.lang.StringBuilder是JDK 5.0版本新增的类,它是一个可变的字符序列。

        (2)此类提供一个与StringBuffer类兼容的API,被设计用作StringBuffer类的一个简易替换,在大多数实现中,它比StringBuffer执行要快。

        (3)使用StringBuilder类处理字符串的方法与StringBuffer类基本一样。

public static void main(String[] args) {

StringBuffer sb1 = new StringBuffer("qwertyuiop");

StringBuffer sb2 = sb1.append("asdfg");
System.out.println(sb1);
System.out.println(sb2);

StringBuffer sb3 = sb1.delete(2, 5);
System.out.println(sb1);//qwyuiopasdfg
System.out.println(sb3);

sb1.insert(3, "opq");
System.out.println(sb1);//qwyopquiopasdfg

sb1.reverse();
System.out.println(sb1);

/*
* 一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,
* 但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。
* 如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。
* 在 StringBuilder 上的主要操作是 append 和 insert 方法,可重载这些方法,以接受任意类型的数据。
* 每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串生成器中。
* append 方法始终将这些字符添加到生成器的末端;而 insert 方法则在指定的点添加字符
*/
}

5、 String类、StringBuffer类及StringBuilder类对比

      String、StringBuffer、StringBuilder这3个类在处理字符串时有各自的特点和实用场合,具体如下:

   (1)String:字符串常量

            String是不可变的对象,在每次对String类型进行改变时其实都等同于生成了一个新的String对象,然后指向新的String对象,所以经常改变内容的字符串最好不要用String类型,因为每次生成对象都会对系统性能产生影响。

   (2)StringBuffer:字符串变量

            StringBuffer是可变的字符串,在每次对StringBuffer对象进行改变时,会对StringBuffer对象本身进行操作,而不是生成新的对象,再改变对象引用。所以,在字符串对象经常改变的情况下,推荐使用StringBuffer类。

   (3)StringBuilder:字符串变量

            JDK 5.0版本以后提供了StringBuilder类,它和StringBuffer类等价,区别在于StringBuffer类是线程安全的,StringBuilder类是单线程的,不提供同步,理论上效率更高。

        码文不易,本篇文章就介绍到这里,如果想要学习更多Java系列知识,请关注博主,博主带你零基础学习Java知识。与此同时,对于日常生活有困扰的朋友,欢迎阅读我的第四栏目:《国学周更—心性养成之路》,学习技术的同时,我们也注重了心性的养成。

22、JAVA进阶——实体类应用_servlet_06

举报

相关推荐

0 条评论