Object类
- 超类、基类,所有类的直接或间接父类,位于继承树的最顶层
- 任何类,如果没有书写extends显示继承某个类,都默认直接继承Object类,否则为间接继承
- Object类中所定义的方法,是所有对象都具备的方法
- Object类型可以存放任何对象。
- 作为参数,可接受任何对象
- 作为返回值,可返回任何对象
getClass()方法
- public final Class<?> getClass(){}
- 返回引用中存储的实际对象类型
- 应用:通常用于判断两个引用中实际储存对象类型是否一致
| package com.standardClass.demo05; |
| |
| public 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; |
| } |
| } |
| package com.standardClass.demo05; |
| |
| public class TestStudent { |
| public static void main(String[] args) { |
| Student s1 = new Student("aaa",20); |
| Student s2 = new Student("bbb",20); |
| |
| Class class1 = s1.getClass(); |
| Class class2 = s2.getClass(); |
| if (class1==class2){ |
| System.out.println("s1和s2属于同一个类型"); |
| }else { |
| System.out.println("s1和s2不属于同一个类型"); |
| } |
| } |
| } |
| |
hashCode()方法
- public int hashCode(){}
- 返回该对象的哈希码值
- 哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值
- 一般情况下相同对象返回相同哈希码
| package com.standardClass.demo05; |
| |
| public 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; |
| } |
| } |
| package com.standardClass.demo05; |
| |
| public class TestStudent { |
| public static void main(String[] args) { |
| |
| Student s1 = new Student("aaa",20); |
| Student s2 = new Student("bbb",20); |
| |
| Class class1 = s1.getClass(); |
| Class class2 = s2.getClass(); |
| if (class1==class2){ |
| System.out.println("s1和s2属于同一个类型"); |
| }else { |
| System.out.println("s1和s2不属于同一个类型"); |
| } |
| |
| System.out.println(s1.hashCode()); |
| |
| System.out.println(s2.hashCode()); |
| Student s3 = s1; |
| System.out.println(s3.hashCode()); |
| } |
| |
| |
| |
| |
| |
| } |
toString()方法
- public String toString(){}
- 返回该对象的字符串表示(表现形式)
- 可以根据程序需求覆盖该方法,如:展示对象各个属性值
| package com.standardClass.demo05; |
| |
| public 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; |
| } |
| } |
| package com.standardClass.demo05; |
| |
| public class TestStudent { |
| public static void main(String[] args) { |
| System.out.println("----------------------getClass------------------"); |
| |
| Student s1 = new Student("aaa",20); |
| Student s2 = new Student("bbb",20); |
| |
| Class class1 = s1.getClass(); |
| Class class2 = s2.getClass(); |
| if (class1==class2){ |
| System.out.println("s1和s2属于同一个类型"); |
| }else { |
| System.out.println("s1和s2不属于同一个类型"); |
| } |
| System.out.println("----------------------hashCode------------------"); |
| |
| System.out.println(s1.hashCode()); |
| |
| System.out.println(s2.hashCode()); |
| Student s3 = s1; |
| System.out.println(s3.hashCode()); |
| |
| System.out.println("----------------------toString------------------"); |
| |
| System.out.println(s1.toString()); |
| System.out.println(s2.toString()); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| package com.standardClass.demo05; |
| |
| public 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; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| '}'; |
| } |
| } |
| package com.standardClass.demo05; |
| |
| public class TestStudent { |
| public static void main(String[] args) { |
| System.out.println("----------------------getClass------------------"); |
| |
| Student s1 = new Student("aaa",20); |
| Student s2 = new Student("bbb",20); |
| |
| Class class1 = s1.getClass(); |
| Class class2 = s2.getClass(); |
| if (class1==class2){ |
| System.out.println("s1和s2属于同一个类型"); |
| }else { |
| System.out.println("s1和s2不属于同一个类型"); |
| } |
| System.out.println("----------------------hashCode------------------"); |
| |
| System.out.println(s1.hashCode()); |
| |
| System.out.println(s2.hashCode()); |
| Student s3 = s1; |
| System.out.println(s3.hashCode()); |
| |
| System.out.println("----------------------toString------------------"); |
| |
| System.out.println(s1.toString()); |
| System.out.println(s2.toString()); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
equals()方法
- public boolean equals(Object obj){}
- 默认实现为(this == obj),比较两个对象地址是否相同(this表示当前调用equals()方法的对象,obj表示你传递进去进行比较的对象)
- 可进行覆盖,比较两个对象的内容是否相同
| package com.standardClass.demo05; |
| |
| public 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; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| '}'; |
| } |
| } |
| package com.standardClass.demo05; |
| |
| public class TestStudent { |
| public static void main(String[] args) { |
| System.out.println("----------------------getClass------------------"); |
| |
| Student s1 = new Student("aaa",20); |
| Student s2 = new Student("bbb",20); |
| |
| Class class1 = s1.getClass(); |
| Class class2 = s2.getClass(); |
| if (class1==class2){ |
| System.out.println("s1和s2属于同一个类型"); |
| }else { |
| System.out.println("s1和s2不属于同一个类型"); |
| } |
| System.out.println("----------------------hashCode------------------"); |
| |
| System.out.println(s1.hashCode()); |
| |
| System.out.println(s2.hashCode()); |
| Student s3 = s1; |
| System.out.println(s3.hashCode()); |
| |
| System.out.println("----------------------toString------------------"); |
| |
| System.out.println(s1.toString()); |
| System.out.println(s2.toString()); |
| |
| System.out.println("----------------------equals------------------"); |
| |
| System.out.println(s1.equals(s2)); |
| |
| Student s4 = new Student("小明",17); |
| Student s5 = new Student("小明",17); |
| System.out.println(s4.equals(s5)); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
equals()方法覆盖步骤
- 比较两个引用是否指向同一个对象
- 判断obj是否为null
- 判断两个引用指向的实际对象类型是否一致
- 强制类型转换
- 依次比较各个属性值是否相同
| package com.standardClass.demo05; |
| |
| import java.util.Objects; |
| |
| public 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; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| '}'; |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| |
| if (this==obj){ |
| return true; |
| } |
| |
| if (obj==null){ |
| return true; |
| } |
| |
| |
| |
| |
| |
| if (obj instanceof Student){ |
| |
| Student s = (Student) obj; |
| |
| if (this.name.equals(s.getName())&&this.age==s.getAge()){ |
| return true; |
| } |
| } |
| return false; |
| } |
| } |
| package com.standardClass.demo05; |
| |
| public class TestStudent { |
| public static void main(String[] args) { |
| System.out.println("----------------------getClass------------------"); |
| |
| Student s1 = new Student("aaa",20); |
| Student s2 = new Student("bbb",20); |
| |
| Class class1 = s1.getClass(); |
| Class class2 = s2.getClass(); |
| if (class1==class2){ |
| System.out.println("s1和s2属于同一个类型"); |
| }else { |
| System.out.println("s1和s2不属于同一个类型"); |
| } |
| System.out.println("----------------------hashCode------------------"); |
| |
| System.out.println(s1.hashCode()); |
| |
| System.out.println(s2.hashCode()); |
| Student s3 = s1; |
| System.out.println(s3.hashCode()); |
| |
| System.out.println("----------------------toString------------------"); |
| |
| System.out.println(s1.toString()); |
| System.out.println(s2.toString()); |
| |
| System.out.println("----------------------equals------------------"); |
| |
| System.out.println(s1.equals(s2)); |
| |
| Student s4 = new Student("小明",17); |
| Student s5 = new Student("小明",17); |
| System.out.println(s4.equals(s5)); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
finalize()方法
- 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列
- 垃圾对象:没有有效引用指向此对象时,为垃圾对象
- 垃圾回收:由GC销毁垃圾对象,释放数据存储空间
- 自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象
- 手动回收机制:使用System.gc();通知JVM执行垃圾回收
| package com.standardClass.demo05; |
| |
| import java.util.Objects; |
| |
| public 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; |
| } |
| |
| @Override |
| protected void finalize() throws Throwable { |
| System.out.println(this.name+"对象被回收了"); |
| } |
| } |
| package com.standardClass.demo05; |
| |
| public class TestStudent2 { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| |
| new Student("aaa", 20); |
| new Student("bbb", 20); |
| new Student("ccc", 20); |
| new Student("ddd", 20); |
| new Student("eee", 20); |
| |
| System.gc(); |
| System.out.println("回收垃圾"); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
包装类
什么是包装类?
- 基本数据类型所对应的引用数据类型
- Object可统一所有数据,包装类的默认值是null
包装类对应
-
基本数据类型---------包装类型
-
byte---------------------Byte
-
short--------------------Short
-
int------------------------Integer
-
long----------------------Long
-
float----------------------Float
-
double------------------Double
-
boolean-----------------Boolean
-
char-----------------------Character
类型转换与装箱、拆箱
装箱:把基本类型转换为引用类型的过程
拆箱:把引用类型转换为基本类型的过程
- 8种包装类提供不同类型间的转换方式:
- Number父类中提供6个共性方法。
- parseXXX()静态方法
- valueOf()静态方法
- 注意:需保证类型兼容,否则抛出NumberFormatException异常
| package com.standardClass.demo06; |
| |
| public class Demo01 { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| |
| int num1 = 18; |
| |
| Integer integer1 = new Integer(num1); |
| Integer integer2 = Integer.valueOf(num1); |
| System.out.println("装箱"); |
| System.out.println(integer1); |
| System.out.println(integer2); |
| |
| |
| Integer integer3 = new Integer(100); |
| int num2 = integer3.intValue(); |
| System.out.println("拆箱"); |
| System.out.println(num2); |
| |
| |
| |
| int age = 30; |
| |
| Integer integer4 = age; |
| System.out.println("自动装箱"); |
| System.out.println(integer4); |
| |
| int age2 = integer4; |
| System.out.println("自动拆箱"); |
| System.out.println(age2); |
| |
| System.out.println("------------基本类型和字符串之间转换----------"); |
| |
| |
| int n1 = 255; |
| |
| String s1 = n1+""; |
| |
| |
| String s2 = Integer.toString(n1,16); |
| System.out.println(s1); |
| System.out.println(s2); |
| |
| |
| |
| String str = "150"; |
| |
| int n2 = Integer.parseInt(str); |
| System.out.println(n2); |
| |
| |
| String str2 = "truerr"; |
| boolean b1 = Boolean.parseBoolean(str2); |
| System.out.println(b1); |
| |
| |
| } |
| } |
整数缓冲区(Integer缓冲区)
- Java预先创建了256个常用的整数包装类型对象
- 在实际应用当中,对已创建的对象进行复用
| package com.standardClass.demo06; |
| |
| public class Demo02 { |
| public static void main(String[] args) { |
| |
| Integer integer1 = new Integer(100); |
| Integer integer2 = new Integer(100); |
| System.out.println(integer1==integer2); |
| |
| Integer integer3 =Integer.valueOf(100); |
| Integer integer4 = 100; |
| System.out.println(integer3==integer4); |
| |
| Integer integer5 = 200; |
| Integer integer6 = 200; |
| System.out.println(integer5==integer6); |
| } |
| } |
| |
| public static Integer valueOf(int i) { |
| if (i >= IntegerCache.low && i <= IntegerCache.high) |
| return IntegerCache.cache[i + (-IntegerCache.low)]; |
| return new Integer(i); |
| } |
String类
- 字符串是常量,创建之后不可改变。
- 字符串字面值存储在字符串池中,可以共享。
- String s = "Hello";//产生一个对象,字符串池中存储。
- String s = new String("Hello");//产生两个对象,堆,池各存储一个。
| package com.standardClass.demo06; |
| |
| public class Demo03 { |
| public static void main(String[] args) { |
| String name = "hello"; |
| name = "zhangsan"; |
| String name2 = "zhangsan"; |
| |
| |
| String str = new String("java"); |
| String str2 = new String("java"); |
| System.out.println(str==str2); |
| System.out.println(str.equals(str2)); |
| } |
| } |
常用方法
- 1、public int length();返回字符串的长度。
- 2、public char charAt(int index);根据下标获取字符。
- 3、public boolean contains(String str);判断当前字符串中是否包含str。
- 4、public char[] toCharArray();将字符串转换成数组。
- 5、public int indexOf(String str);查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1.
- 6、public int lastIndexOf(String str);查找字符串在当前字符串中最后一次出现的下标索引。
- 7、public String trim();去掉字符串前后的空格。
- 8、public String toUpperCase();将小写转成大写。
- 9、public boolean endsWith(String str);判断字符串是否以str结尾。
- 10、public String replace(char oldchar,char newchar);将旧字符串替换成新字符串。
- 11、public String[] split(String str);根据str做拆分。
| System.out.println("------------------字符串方法的使用1--------------------"); |
| |
| |
| |
| |
| |
| String content = "java是世界上最好的java编程语言,java真香"; |
| System.out.println(content.length()); |
| System.out.println(content.charAt(content.length()-1)); |
| System.out.println(content.contains("java")); |
| System.out.println(content.contains("php")); |
| |
| System.out.println("------------------字符串方法的使用2--------------------"); |
| |
| |
| |
| |
| System.out.println(Arrays.toString(content.toCharArray())); |
| System.out.println(content.indexOf("java")); |
| System.out.println(content.indexOf("java",4)); |
| System.out.println(content.lastIndexOf("java")); |
| |
| System.out.println("------------------字符串方法的使用3--------------------"); |
| |
| |
| |
| |
| |
| String content2 = " hello World "; |
| System.out.println(content2.trim()); |
| System.out.println(content2.toUpperCase()); |
| System.out.println(content2.toLowerCase()); |
| String filename = "hello.java"; |
| System.out.println(filename.endsWith(".java")); |
| System.out.println(filename.startsWith("hello")); |
| |
| System.out.println("------------------字符串方法的使用4--------------------"); |
| |
| |
| |
| |
| System.out.println(content.replace("java","php")); |
| |
| String say = "java is the best programing language,java xiang"; |
| String[] arr = say.split("[ ,]+"); |
| System.out.println(arr.length); |
| for (String String : arr){ |
| System.out.println(String); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| System.out.println("------------------补充--------------------"); |
| |
| String s1 = "hello"; |
| String s2 = "HELLO"; |
| System.out.println(s1.equalsIgnoreCase(s2)); |
| |
| String s3 = "abc"; |
| String s4 = "xyz"; |
| System.out.println(s3.compareTo(s4)); |
| |
| String s5 = "abc"; |
| String s6 = "abcxyz"; |
| System.out.println(s5.compareTo(s6)); |
案例演示
- 需求:
- 已知String str = "this is a text";
- 1、将str中的单词单独获取出来
- 2、将str中的text替换为practice
- 3、在text前面插入一个easy
- 4、将每个单词的首字母改为大写
| package com.standardClass.demo06; |
| |
| |
| |
| |
| |
| |
| |
| |
| public class Demo04 { |
| public static void main(String[] args) { |
| String str = "this is a text"; |
| System.out.println("-------------1、将str中的单词单独获取出来-----------------"); |
| |
| String[] arr = str.split(" "); |
| for (String s : arr){ |
| System.out.println(s); |
| } |
| |
| System.out.println("-------------2、将str中的text替换为practice-----------------"); |
| |
| String str2 = str.replace("text","practice"); |
| System.out.println(str2); |
| |
| System.out.println("-------------3、在text前面插入一个easy-----------------"); |
| |
| String str3 = str.replace("text","easy text"); |
| System.out.println(str3); |
| |
| System.out.println("-------------4、将每个单词的首字母改为大写-----------------"); |
| |
| for (int i=0;i< arr.length;i++){ |
| char first = arr[i].charAt(0); |
| |
| char upperfirst = Character.toUpperCase(first); |
| |
| String news = upperfirst + arr[i].substring(1); |
| System.out.println(news); |
| } |
| |
| } |
| } |
可变字符串(String的进阶)
- StringBuffer:可变长字符串,JDK1.0提供,运行效率慢、线程安全。
- StringBuilder:可变长字符串,JDK5.0提供,运行效率快、线程不安全。
| package com.standardClass.demo06; |
| |
| |
| |
| |
| |
| public class Demo05 { |
| public static void main(String[] args) { |
| |
| StringBuilder sb = new StringBuilder(); |
| |
| sb.append("java世界第一"); |
| System.out.println(sb.toString()); |
| sb.append("java真香"); |
| System.out.println(sb.toString()); |
| sb.append("java不错"); |
| System.out.println(sb.toString()); |
| |
| sb.insert(0,"我在最前面"); |
| System.out.println(sb.toString()); |
| |
| sb.replace(0,5,"hello"); |
| System.out.println(sb.toString()); |
| |
| sb.delete(0,5); |
| System.out.println(sb.toString()); |
| |
| sb.delete(0,sb.length()); |
| System.out.println(sb.toString()); |
| System.out.println(sb.length()); |
| } |
| } |
| package com.standardClass.demo06; |
| |
| |
| |
| |
| public class Demo06 { |
| public static void main(String[] args) { |
| |
| long start = System.currentTimeMillis(); |
| |
| |
| |
| |
| |
| StringBuilder sb = new StringBuilder(); |
| for (int i=0;i<99999;i++){ |
| sb.append(i); |
| } |
| System.out.println(sb.toString()); |
| |
| long end = System.currentTimeMillis(); |
| System.out.println("用时:"+(end-start)); |
| } |
| } |
BigDecimal类
| public class Demo07 { |
| public static void main(String[] args) { |
| double d1 = 1.0; |
| double d2 = 0.9; |
| System.out.println(d1-d2); |
| } |
| } |
很多实际应用中需要精确运算,而double是近似值存储,不在符合要求,需要借助BigDecimal。
- 位置:java.math包中。
- 作用:精确计算浮点数。
- 创建方式:BigDecimal bd = new BigDecimal("1.0");
- 方法:
- BigDecimal add(BigDecimal bd) 加
- BigDecimal subtract(BigDecimal bd) 减
- BigDecimal multipiy(BigDecimal bd) 乘
- BigDecimal divide(BigDecimal bd) 除
- 利用BigDecimal可以进行数值计算
- 除法:divide(BigDecimal bd,int scale,RoundingMode mode)
- 参数scale:指定精确到小数点后几位。
- 参数mode:
- 指定小数部分的取舍模式,通常采用四舍五入的模式,
- 取值为BigDecimal.ROUND_HALF_UP。
| package com.standardClass.demo06; |
| |
| import java.math.BigDecimal; |
| import java.math.RoundingMode; |
| |
| public class Demo07 { |
| public static void main(String[] args) { |
| double d1 = 1.0; |
| double d2 = 0.9; |
| System.out.println(d1-d2); |
| |
| |
| double result = (1.4-0.5)/0.9; |
| System.out.println(result); |
| |
| |
| BigDecimal bd1 = new BigDecimal("1.0"); |
| BigDecimal bd2 = new BigDecimal("0.9"); |
| |
| BigDecimal r1 = bd1.subtract(bd2); |
| System.out.println(r1); |
| |
| |
| BigDecimal r2 = bd1.add(bd2); |
| System.out.println(r2); |
| |
| |
| BigDecimal r3 = bd1.multiply(bd2); |
| System.out.println(r3); |
| |
| |
| BigDecimal r4 = new BigDecimal("1.4") |
| .subtract(new BigDecimal("0.5")) |
| .divide(new BigDecimal("0.9")); |
| System.out.println(r4); |
| |
| BigDecimal r5 = new BigDecimal("20") |
| .divide(new BigDecimal("3"),2,BigDecimal.ROUND_HALF_UP); |
| System.out.println(r5); |
| } |
| } |
Date类
- Date表示特定的瞬间,精确到毫秒。Date类中的大部分方法都已经被Calender类中的方法所取代。
- 时间单位
- 1秒=1000毫秒
- 1毫秒=1000微秒
- 1微秒=1000纳秒
| package com.standardClass.demo07; |
| |
| import java.util.Date; |
| |
| public class Demo01 { |
| public static void main(String[] args) { |
| |
| |
| Date date1 = new Date(); |
| System.out.println(date1.toString()); |
| System.out.println(date1.toLocaleString()); |
| |
| Date date2 = new Date(date1.getTime()-(60*60*24*1000)); |
| System.out.println(date2.toLocaleString()); |
| |
| boolean b1 = date1.after(date2); |
| System.out.println(b1); |
| boolean b2 = date1.before(date2); |
| System.out.println(b2); |
| |
| |
| int d1 = date1.compareTo(date2); |
| int d2 = date2.compareTo(date1); |
| int d3 = date1.compareTo(date1); |
| System.out.println(d1); |
| System.out.println(d2); |
| System.out.println(d3); |
| |
| boolean b3 = date1.equals(date2); |
| System.out.println(b3); |
| } |
| } |
Calendar类
| package com.standardClass.demo07; |
| |
| import java.util.Calendar; |
| |
| public class Demo02 { |
| public static void main(String[] args) { |
| |
| Calendar calendar = Calendar.getInstance(); |
| System.out.println(calendar.toString()); |
| System.out.println(calendar.getTime().toLocaleString()); |
| System.out.println(calendar.getTimeInMillis()); |
| |
| |
| int year = calendar.get(Calendar.YEAR); |
| |
| int month = calendar.get(Calendar.MONTH); |
| |
| int day = calendar.get(Calendar.DAY_OF_MONTH); |
| |
| int hour = calendar.get(Calendar.HOUR_OF_DAY); |
| |
| int minute = calendar.get(Calendar.MINUTE); |
| |
| int second = calendar.get(Calendar.SECOND); |
| System.out.println(year+"年"+(month+1)+"月"+day+"日"+hour+":"+minute+":"+second); |
| |
| Calendar calendar2 = Calendar.getInstance(); |
| calendar2.set(Calendar.DAY_OF_MONTH,25); |
| System.out.println(calendar2.getTime().toLocaleString()); |
| |
| |
| calendar2.add(Calendar.HOUR,-1); |
| System.out.println(calendar2.getTime().toLocaleString()); |
| |
| |
| calendar2.add(Calendar.MONTH,1); |
| int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH); |
| int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH); |
| System.out.println(max); |
| System.out.println(min); |
| } |
| } |
| package com.standardClass.demo07; |
| |
| import java.text.SimpleDateFormat; |
| import java.util.Date; |
| |
| public class Demo03 { |
| public static void main(String[] args) throws Exception{ |
| |
| |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); |
| |
| Date date = new Date(); |
| |
| String str = sdf.format(date); |
| System.out.println(str); |
| |
| Date date2=sdf.parse("1990/05/01"); |
| System.out.println(date2); |
| } |
| } |
System类
- System系统类,主要用于获取系统的属性数据和其他操作,构造方法私有。
方法名 |
说明 |
static void arraycopy(...) |
复制数组 |
static long currentTimeMillis(); |
获取当前系统时间,返回的是毫秒值 |
static void gc(); |
建议JVM赶快启动垃圾回收器回收垃圾 |
static void exit(int status); |
退出jvm,如果参数是0表示正常退出jvm,非0表示异常退出jvm |
| package com.standardClass.demo07; |
| |
| public class Student { |
| private String name; |
| private int age; |
| |
| public Student(String name, int age) { |
| this.name = name; |
| this.age = age; |
| } |
| |
| public Student() { |
| } |
| |
| 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; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| '}'; |
| } |
| |
| @Override |
| protected void finalize() throws Throwable { |
| System.out.println("回收了"+name+""+" "+age); |
| } |
| } |
| package com.standardClass.demo07; |
| |
| import java.util.Arrays; |
| |
| public class Demo04 { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| |
| |
| int[] arr = {20,18,15,8,35,26,45,90}; |
| int[] dest = new int[8]; |
| System.arraycopy(arr,4,dest,4,4); |
| |
| for (int i=0;i< dest.length;i++){ |
| System.out.println(dest[i]); |
| } |
| |
| |
| |
| System.out.println(System.currentTimeMillis()); |
| |
| long start=System.currentTimeMillis(); |
| for (int i=-999999999;i<99999999;i++){ |
| for (int j=-999999999;j<999999999;j++) { |
| int result=i+j; |
| } |
| } |
| long end=System.currentTimeMillis(); |
| System.out.println("用时:"+(end-start)); |
| |
| |
| Student s1 = new Student("aaa",19); |
| new Student("bbb",19); |
| new Student("ccc",19); |
| |
| System.gc(); |
| |
| |
| |
| |
| |
| System.exit(0); |
| |
| System.out.println("程序结束了。。。"); |
| |
| } |
| } |
总结
- 内部类:
- 在一个类的内部再定义一个完整的类。
- 成员内部类、静态内部类、局部内部类、匿名内部类。
- Object类:
- 包装类:
- 基本数据类型所对应的引用数据类型,可以使Object统一所有数据。
- String类:
- 字符串是常量,创建之后不可改变,字面值保存在字符串池中,可以共享。
- BigDecimal类:
- Date类:
- Calendar类:
- SimpleDateFormat类:
- System类:
笔记出处
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?