赞
踩
java.lang包 - 该包是java中的核心包,该包中的所有类由java虚拟机自动导入。
如:String类、System等
java.util - 该包是java中的工具包,改包中提供了大量的工具类和集合类等。
如:Scanner类、Random类、Collections类、Arrays类
java.io包 - 该包是java中的IO包,该包中提供了有关输入输出的类信息。
如:FileInputStream类、FileOutputStream类等
java.net包 - 该包是java的网络包,该包中提供了有关网络编程的类的信息。
如:ServerSocket类、Socket类等
java.lang.Object类是所有类层次结构中的根类。
boolean equals(Object obj):
用于判断调用对象与参数对象是否相等,一般用于比较字符串
- 它是空的自反:对于任何非空的参考价值
x
,x.equals(x)
应该返回true
。- 它是空的对称的:对于任何非空的参考值
x
和y
,x.equals(y)
应该返回true
当且仅当y.equals(x)
返回true
。- 它是空的传递:对于任何非空的参考值
x
,y
,和z
,如果x.equals(y)
返回true
和y.equals(z)
返回true
,然后x.equals(z)
应该返回true
。- 它是空的一致的:对于任何非空的参考值
x
和y
,多次调用x.equals(y)
始终返回true
或始终返回false
,没有提供信息用于equals
比较对象被修改。- 对于任何非空的参考价值
x
,x.equals(null)
应该返回false
。
int hashCode()
:用于返回调用对象的哈希码值(内存地址的编号,对应一块内存)
- 不管是调用同一对象的多个java应用程序执行期间,该
hashCode
方法总是返回相同的整数,没有提供信息用于equals
比较的对象被修改。这个整数不需要保持一致,从一个应用程序的一个执行到另一个执行相同的应用程序。- 如果根据
equals(Object)
法两个对象是相等的,那么调用hashCode
方法每一个对象必须产生相同的整数结果。- 它不是必需的,如果按照
equals(java.lang.Object)
法两个对象是不平等的,然后调用hashCode
方法每一个对象必须产生不同的整数结果。然而,程序员应该意识到,产生不同的整数结果的不平等的对象可能会提高哈希表的性能。
String toString()
:用于返回对象的字符串表示形式。
- 返回对象的字符串表示形式。总的来说,这
toString
方法返回一个字符串,“以文本方式表示”这个对象。其结果应该是一个简洁,但翔实的代表性,是一个容易阅读的人。建议所有子类都重写此方法。
- package day01;
- import java.util.Objects;
- /**
- * @author Mr.乐
- * @data 2022/8/1 21:14
- */
- class Student {
- private String name;
- private int age;
-
- public Student() {
- }
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
- //重写toString方法的目的是为了打印对象内部的内容
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- //自类重写equals和hashCode方法时,就会比较两个对象的内容,而不是比较对象的地址
-
- @Override
- public boolean equals(Object o) {
- //引用都相等
- if (this == o) return true;
- //为空或类型不同
- if (o == null || getClass() != o.getClass()) return false;
- Student student = (Student) o;
- return age == student.age && Objects.equals(name, student.name);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(name, age);
- }
- }
-
- public class Demo01 {
- public static void main(String[] args) {
- //当没有继承任何类的情况下,jvm会自动继承Object类
- //初始化方法定义数组
- int[] arr={1,2,3,4,5,6,7,8,9};
- /*
- * getClass().getName() + '@' + Integer.toHexString(hashCode())
- * getClass().getName为包名+类名
- * Integer.toHexString(hashCode())为该对象所对应的哈希码的十六进制
- */
-
- System.out.println(arr);
- //[I@119d7047
- System.out.println(arr.toString());
- //与上卖弄结果一样 默认调用该对象的toString方法
- System.out.println(arr.hashCode());
- //295530567 打印为哈希码的10进制数
-
- Student stu01 = new Student("张三", 18);
- System.out.println(stu01);
- //day01.Student@4eec7777 在没有重写toString的情况下,默认打印包名+类名+哈希码的十六进制数
- //Student{name='张三', age=18} 重写了toString方法
- Student stu02 = new Student("Andy",18);
- //比较两个学生对象的地址是否相同
- System.out.println(stu01.equals(stu02));//false
- //不想比较对象的地址,想比较对象的内容,去实体类中重写equals和hashCode方法,结果为true
- }
- }
3.1-包装类的概念
在某些场合中(集合)中要求所有数据内容都是对象,但是java中8中基本数据类型定义的变量并不是对象,为了满足该场合的需求就需要将基本数据类型进行包装,所以需要借助此包装类。
基本数据类型 | 包装类 |
---|---|
byte | java.lang.Byte类 |
short | java.lang.Short类 |
int | java.lang.Integer类 |
long | java.lang.Long类 |
float | java.lang.Float类 |
double | java.lang.Double类 |
boolean | java.lang.Boolean类 |
char | java.lang.Character类 |
基本概念
java.lang.Integer类被final关键字修饰表示该类不能被继承。
该类的内部包装了一个int类型的变量作为该类的成员变量,实现了int类型的包装。
装箱和拆箱的概念
装箱就是值从int类型向Integer类型的转换。
拆箱就是指冲Integer类型向int类型的转换。
从JDK1.5以后,编译器提供了自动装箱和拆箱的机制。
常用方法
方法名 | 说明 |
---|---|
int intValue() | 用于返回该调用对象的int类型数据(相当于拆箱) |
float floatValue() | 用于返回该调用对象的float类型数据(相当于拆箱) |
static int parseInt(String str) | 用于将参数指定的字符串转换成整数类型并返回 |
static String toBinaryString(int i) | 用于将参数指定的整数拆分成字符串形式的二进制并返回 |
static String toHexString(int i) | 用于将参数指定的整数拆分成字符串形式的十六进制并返回 |
static Integer valueOf(String str) | 用于将参数指定的字符串转换成Integer对象 |
- package day01;
- import java.util.ArrayList;
- /**
- * @author Mr.乐
- * @data 2022/8/1 22:03
- */
- public class Demo2 {
- public static void main(String[] args) {
- //定义集合泛型
- ArrayList<Integer> list = new ArrayList<>();//定义集合
- list.add(1);//自动拆箱装箱
- String str01 = "123";
- String str02 = "123abc";
- int num01 = 456;
- Integer num02 = 789;
- //intValue()
- System.out.println(num02.intValue());//789
- //floatValue()
- System.out.println(num02.floatValue());//789.0
- //parseInt()
- System.out.println(Integer.parseInt(str01));//123
- //System.out.println(Integer.parseInt(str02));//因为字符串中有字母,所以出现数字格式异常
- //toBinaryString()将整数类型转换成字符串形式的二进制
- System.out.println(Integer.toBinaryString(num01));//111001000
- //toHexString(num02)将整数类型转换成字符串形式的十六进制
- System.out.println(Integer.toHexString(num02));//自动拆箱 315
- //valueOf(str01)将字符串转换成Integer
- System.out.println(Integer.valueOf(str01));//123
- //查看引用的类型
- System.out.println(Integer.valueOf(str01).getClass()); //class java.lang.Integer
- }
- }
用于描述日期的信息,表示特定的瞬间可以精确到毫秒。
构造方法
方法名 | 说明 |
---|---|
Date() | 获取当前系统时间来初始化对象 |
Date(long date) | 根据参数指定的毫秒数来构造对象 |
常有方法
方法名 | 说明 |
---|---|
long getTime() | 获取当前对象距离1970.1.1 0:0:0之间的毫秒数(unix系统生日) |
void setTime(long time) | 用于根据参数指定的毫秒数来设置时间 |
- import java.util.Date;
- /**
- * @author Mr.乐
- * @data 2022/8/2 16:01
- */
- public class Demo02 {
- public static void main(String[] args) {
- Date date = new Date();//获取当前系统时间的Date类型对象
- System.out.println(date);//打印时间
-
- long time = date.getTime();//获取当前系统时间距离1970.....的毫秒数
- //获取前一年当前时间的毫秒数 31536000000L为一年的毫秒数
- time = time - 31536000000L;
- Date date1 = new Date(time);//构造方法根据参数指定毫秒数构造对象
- System.out.println(date1);//打印时间
- }
- }
java.text.SimpleDateFormat类用于格式化和解析日期,通俗来说就是调整日期格式。
常用方法
方法名 | 说明 |
---|---|
SimpleDateFormat(String pattern) | 根据参数指定格式来构造对象 |
参数格式 | y代表年份,M代表月份,d代表日期,H代表小时,m代表分钟,s代表秒 |
public final String format(Date date) | 用于将参数指定日期对象按照调用对象的格式转换成字符串 |
public Date parse(String source) | 用于将参数字符串按照调用对象转换成日期类型对象 |
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- /**
- * @author Mr.乐
- * @data 2022/8/2 16:32
- */
- public class Demo03 {
- public static void main(String[] args) throws ParseException {
- Date date = new Date();//创建当前系统时间的Date类型对象
- // System.out.println(date);
- //借助SimpleDateFormat类定义日期格式
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String time = sdf.format(date);//将参数按照指定格式进行格式化,并以字符串类型返回
- System.out.println(time);
-
- //按照sdf规定的格式解析时间
- Date parse = sdf.parse("2012-12-21 0:0:0");//时间格式必须要按照规定好的格式进行书写
- System.out.println(parse);
-
- //获取前一天的当前时间
- long time1 = date.getTime();//获取当前系统时间距离1970...的毫秒数
- long time01 = time1 - 86400000L;
- date.setTime(time01);//将参数指定的时间毫秒数设置到date对象中
- System.out.println(sdf.format(date));
- }
- }
基本概念
Java中的Math包含了用于执行基本数学运算的属性个方法
常有方法
方法名 | 说明 |
---|---|
static int abs(int a) | 返回参数的绝对值 |
static double ceil(double a) | 方法可对一个数进行上舍入,返回值大于或等于给定的参数 |
static double floor(double a) | 方法可对一个数进行下舍入,返回值小于或等于给定的参数 |
static int round(double a) | 返回一个四舍五入的值 |
static int min(int a, int b) | 返回两个参数的最小值 |
static int max(int a , int b) | 返回两个参数的最大值 |
- /**
- * @author Mr.乐
- * @data 2022/8/2 16:49
- */
- public class Demo04 {
- public static void main(String[] args) {
- int n01 = -18;
- int n02 = 21;
- double d01 = 3.4;
- double d02 = 3.5;
- //获取绝对值
- System.out.println(Math.abs(n01));//18
- System.out.println(Math.abs(n02));//21
- //四舍五入
- System.out.println(Math.round(d01));//3
- System.out.println(Math.round(d02));//4
- //上舍入
- System.out.println(Math.ceil(d01));//4.0
- System.out.println(Math.ceil(d02));//4.0
- //下舍入
- System.out.println(Math.floor(d01));//3.0
- System.out.println(Math.floor(d02));//3.0
- //返回两个参数的最小值
- System.out.println(Math.min(n01,n02));//-18
- //返回两个参数的最大值
- System.out.println(Math.max(n01,n02));//21
- }
- }
java的常用类总结到这了,关于String类的介绍在往日文章中。String详解(java)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。