首页 > 其他分享 >常用类

常用类

时间:2024-10-19 16:35:04浏览次数:1  
标签:常用 String int System println public out

第四章—常用类

1、API概述

API(Application Programming Interface) 
应用程序编程接口
编写一个机器人程序去控制机器人踢足球,程序就需要向机器人发出向前跑、向后跑、射门、抢球等各种命令,没有编过程序的人很难想象这样的程序如何编写。但是对于有经验的开发人员来说,知道机器人厂商一定会提供一些用于控制机器人的Java类,这些类中定义好了操作机器人各种动作的方法。其实,这些Java类就是机器人厂商提供给应用程序编程的接口,大家把这些类称为Xxx Robot API。本章涉及的Java API指的就是JDK中提供的各种功能的Java类。

2、Object类 标准类的4.0写法

Object类概述
类层次结构的根类
所有类都直接或者间接的继承自该类
构造方法
public Object()


/*
    java中所有的类默认都有一个共同的父类:Object

    == 比较:
        1、比较的是两个基本数据类型的话,比较两个数值是否相等
        2、比较的是两个引用数据类型的话,比较的是两个对象的地址值是否相等

    成员方法:
        int hashCode() 返回对象的哈希码值。 可以看作地址值的另外一种表现形式
        public final Class getClass() 返回此 Object的运行时类。 获取当前类的class文件对象, 一个类全局只有一个在方法区中
        public String toString()
            打印一个对象名,默认调用的是类中的toString()方法
            若我们自己没有编写该方法的话,默认使用父类Object类中的toString()
            而父类中默认的toString()打印是一个地址值
            我们今后打印一个对象名的时候,输出是地址值意义不大,我们今后更多的是想看一个对象中成员变量值的情况
            要想实现上面的输出成员变量值的话,重写toString()方法,这样调用的就是自己重写后的toString()
            自动生成即可


 */

class Student{
    String name;
    int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class ObjectDemo1 {
    public static void main(String[] args) {
//        Student s1 = new Student();
//        Student s2 = new Student();
//
//        System.out.println(s1.hashCode());
//        System.out.println(s2.hashCode());
//
//        System.out.println(s1==s2);
//
//        Class c1 = s1.getClass();
//        System.out.println(c1.getName());

        Student s1 = new Student("李刚", 18);
//        System.out.println(s1.hashCode());
//        System.out.println(s1.getClass().getName() );
//        System.out.println(s1.toString()); // com.shujia.day10.Student@4554617c
        //this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());

        System.out.println(s1.toString());
    }
}

============================================================
/*
    一个标准类的4.0版本写法:
        成员变量: 私有化
        构造方法: 有参,无参
        成员方法: getXxx()和setXxx()
        toString(): 重写父类中的toString()
 */
public class ObjectDemo2 {
    public static void main(String[] args) {

    }
}

============================================================

/*
    public boolean equals(Object obj)
    protected void finalize() 垃圾回收用的


 */

import java.util.Objects;

class Student2{
    String name;
    int age;

    public Student2() {
    }

    public Student2(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student2{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student2 student2 = (Student2) o;
        return age == student2.age && Objects.equals(name, student2.name);
    }

}

public class ObjectDemo3 {
    public static void main(String[] args) {
        Student2 s1 = new Student2("李刚", 21);
        Student2 s2 = new Student2("李刚", 21);

        //默认情况下,若对象类中没有自己编写equals方法,默认使用的是父类中Object类中的equals方法
        //而父类中Object类中的equals方法底层调用的是==比较,若比较的是引用数据类型,比较的是地址值
        //而我们这里s1和s2接受的是不同两个对象的地址值,所以默认比较的结果是false
//        System.out.println(s1.equals(s2));
        //需求:当对象的姓名和年龄一样的时候,表示是同一个学生
        //比较的对象类中自己重写一个equals方法,自动生成即可
        System.out.println(s1.equals(s2));
    }
}

============================================================

/*
    protected Object clone()

    java中并不是所有的类都可以被克隆,只有授权的类的对象才可以使用克隆方法
    我们通过阅读帮助文档后发现,若一个对象的类没有实现Cloneable接口的话,是不可以调用clone方法的
    然后,我们再去看Cloneable接口的时候,发现该接口中没有任何抽象方法,今后像这样的接口称之为标记接口

    克隆在IT行业中,称之为拷贝。
    拷贝分为两种:
        1、浅拷贝
        2、深拷贝

    面试题:Object类中的clone是浅拷贝还是深拷贝。答:是浅拷贝
 */
class Demo1 {
    int a = 10;


}

class Student3 implements Cloneable {
    String name;
    int age;
    Demo1 demo1;

    public Student3() {
    }

    public Student3(String name, int age, Demo1 demo1) {
        this.name = name;
        this.age = age;
        this.demo1 = demo1;
    }

    @Override
    public String toString() {
        return "Student3{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", demo1=" + demo1 +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class ObjectDemo4 {
    public static void main(String[] args) throws Exception {
        Demo1 d1 = new Demo1();


        Student3 s1 = new Student3("刘亦菲", 36, d1);
        System.out.println("s1: " + s1);
        System.out.println(s1.hashCode());
//        Student3 s2 = new Student3("章若楠", 21);

        System.out.println("===========================================");
        //首先,如果此对象的类不实现接口Cloneable ,则抛出CloneNotSupportedException 。
//        Object o1 = s1.clone();
        Object o1 = s1.clone();
        System.out.println("o1: " + o1);
        System.out.println(o1.hashCode());
    }
}

3、Scanner类

import java.util.Scanner;

/*
    Scanner: 一个简单的文本扫描器,可以使用正则表达式解析原始类型和字符串。

    构造方法:
        Scanner(InputStream source) 构造一个新的 Scanner ,产生从指定输入流扫描的值。

 */
public class ScannerDemo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        //获取一个数字:
//        int i = sc.nextInt();
//        System.out.println(i);
//
//        String s1 = sc.next();
//        System.out.println(s1);

        //获取数字的时候,我们是不是只能够输入数字呢?
//        System.out.println("请输入一个数字:");
        //hasNextXxx()判断下一个输入的内容是否是指定类型
//        if(sc.hasNextInt()){
//            int i = sc.nextInt();
//            System.out.println(i);
//        }else {
//            System.out.println("您输入的内容不是一个int类型的值!");
//        }

//        int i = sc.nextInt();
//        System.out.println(i);
//        String s1 = sc.next(); //无法接收换行等空白特殊字符
//        System.out.println(s1);
        //输入字符串的另外一种方法
//        String s1 = sc.nextLine(); //可以接收换行等空白特殊字符
//        System.out.println(s1);


        System.out.println("hello world");
    }
}

4、String类

1、String类概述及其构造方法
String类概述
字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
构造方法
public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)
    
/*
    String类中的构造方法:
        public String()
        public String(byte[] bytes)
        public String(byte[] bytes,int offset,int length)
        public String(char[] value)
        public String(char[] value,int offset,int count)
        public String(String original)
 */

public class StringDemo2 {
    public static void main(String[] args) {
        //public String() 创建一个空字符串
        String s1 = new String(); // 堆内存
        System.out.println("s1: " + s1);

//        String s2 = ""; // 常量池
//        System.out.println(s1==s2);
        System.out.println("=============================");

        //public String(byte[] bytes) 将字节数组转成一个字符串
        byte[] bytes = {97,98,99,100,101,102};
        String s2 = new String(bytes);
        System.out.println("s2: "+s2);

        System.out.println("=============================");
        //public String(byte[] bytes,int index,int length) 从字节数组的某个位置开始,向后截取变成字符串
        String s3 = new String(bytes, 2, 3);
        System.out.println("s3: "+s3);
        System.out.println("=============================");
        //public String(char[] value)  //将一个字符数组转成一个字符串
        char[] chars = {'我','爱','中','华'};
        String s4 = new String(chars);
        System.out.println("s4: "+s4);
        System.out.println("=============================");
        //public String(char[] value,int index,int length) 将字符数组一部分转成字符串
        String s5 = new String(chars,1,2);
        System.out.println("s5: "+s5);
        System.out.println("=============================");
        //public String(String original) 将字符串封装成一个String对象在堆内存中
        String s6 = new String("你好");
        System.out.println("s6: "+s6);
    }
}
    
============================================================
2、字符串是常量,它的值在创建之后不能更改
String s = “hello”; s += “world”; 问s的结果是多少?
面试题
String s = new String(“hello”)和String s = “hello”;的区别?
字符串比较之看程序写结果
字符串拼接之看程序写结果
    
String s = new String("hello"):
这行代码使用 new 关键字创建了一个新的 String 对象。
当你使用 new 关键字时,Java 虚拟机(JVM)会在堆内存中分配一个新的区域来存储字符串 "hello"。
这种方式创建的 String 对象是独立的,即使有其他 String 变量也是指向相同的字符串字面量("hello"),它们在内存中是分开的。
    
String s = "hello":
这行代码使用字符串字面量来创建一个 String 对象。
字符串字面量存储在 Java 中的字符串常量池(String pool)中。常量池是一个特殊的内存区域,用于存储所有字符串字面量。
当你使用字符串字面量时,如果常量池中已经存在这个字符串字面量的值(即 "hello"),则直接使用这个引用。如果不存在,JVM 会创建一个新的字符串对象并将其放入常量池中,然后返回这个引用。    
    
    
3、String类的判断功能:
    
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean startsWith(String str)
boolean endsWith(String str)
boolean isEmpty()
    
/*
    String类中的判断功能:
        boolean equals(Object obj)
        boolean equalsIgnoreCase(String str)
        boolean contains(String str)
        boolean startsWith(String str)
        boolean endsWith(String str)
        boolean isEmpty()

 */
public class StringDemo4 {
    public static void main(String[] args) {
        //boolean equals(Object obj) 比较两个字符串的内容值
        String s1 = "hello";
        String s2 = "HellO";
        System.out.println(s1.equals(s2));
        //boolean equalsIgnoreCase(String str) 忽略大小写比较字符串内容值
        System.out.println(s1.equalsIgnoreCase(s2));
        //boolean contains(String str)  判断大字符串中是否包含某一个小字符串
        String s3 = "今天的天气还可以李刚决定去洗个脚";
        System.out.println(s3.contains("李刚决"));
        //boolean startsWith(String str) 判断字符串是否以某个字符串开头
        System.out.println(s3.startsWith("今天的天气数可以"));
        // boolean endsWith(String str) 判断字符串是否以某个字符串结尾
        System.out.println(s3.endsWith("洗个脚"));
        //boolean isEmpty() 判断字符串是否为空字符串,指的是内容是否为空
        String s4 = "";
        System.out.println(s4.isEmpty());
        String s5 = null;
//        System.out.println(s5.isEmpty()); // NullPointerException


    }
}    
    
============================================================ 
4、String类中的获取功能:
 /*
String类中的获取功能:
    int length()
    char charAt(int index)
    int indexOf(int ch)
    int indexOf(String str)
    int indexOf(int ch,int fromIndex)
    int indexOf(String str,int fromIndex)
    String substring(int start)
    String substring(int start,int end)

 */
public class StringDemo5 {
    public static void main(String[] args) {
        String s = "李刚在数加学院中学习非常快乐!";
        //int length() 获取字符串中的字符个数,字符串长度
        System.out.println(s.length());

        //char charAt(int index) 根据索引获取字符串中某一个字符
        //字符串可以被看作成一个字符数组
//        System.out.println(s.charAt(15)); // StringIndexOutOfBoundsException

        //int indexOf(int ch) 根据ascii码值获取对应字符所在的索引位置,左边起第一个
        String s2 = "qweasdsafqe";
        System.out.println(s2.indexOf(104)); // -1
        //int indexOf(int ch,int fromIndex) 从某个索引开始向后寻找某个字符,返回找到字符在整个大字符串中的索引位置
        System.out.println(s2.indexOf(97,5)); // 7

        //int indexOf(String str) 获取大字符串中小字符串的位置,返回小字符串第一个字符的索引
        System.out.println(s2.indexOf("sdsa")); // 4
        //int indexOf(String str,int fromIndex) 从某个索引开始向后寻找某个字符,返回找到字符串第一个字符在整个大字符串中的索引位置
        System.out.println(s2.indexOf("saf",3)); // 6

        //String substring(int start) // 从指定位置向后截取,返回新的字符串
        String s3 = "李刚是真的帅!江川很不服,钱志强觉得自己是最帅的!";
        String res1 = s3.substring(3);
        System.out.println(res1);

        //String substring(int start,int end) 截取字符串中的一部分 [start, end)
        String res2 = s3.substring(7, 12);
        System.out.println(res2);
    }
}
  
    
============================================================ 5、String转换功能:   
/*
String转换功能:
    byte[] getBytes()
    char[] toCharArray()
    static String valueOf(char[] chs)
    static String valueOf(int i)
    String toLowerCase()
    String toUpperCase()
    String concat(String str)

 */
public class StringDemo6 {
    public static void main(String[] args) {
        String s1 = "abcdefg";
        //byte[] getBytes() 将字符串转字节数组
        byte[] bytes = s1.getBytes();
        for(int i=0;i<bytes.length;i++){
            System.out.println(bytes[i]);
        }
        System.out.println("----------------");
        //后面集合的时候讲解
//        for(byte b : bytes){
//            System.out.println(b);
//        }
//        System.out.println("----------------");
//        //明天讲解
//        System.out.println(Arrays.toString(bytes));
//        System.out.println("----------------");
        //char[] toCharArray()  将字符串转字符数组
        char[] chars = s1.toCharArray();
        for(int i=0;i<chars.length;i++){
            System.out.println(chars[i]);
        }
        System.out.println("----------------");
        String s3 = new String(chars);
        System.out.println(s3);

        //static String valueOf(char[] chs) 将字符数组转字符串
        String s2 = String.valueOf(chars);
        System.out.println(s2);


        //static String valueOf(int i)
        System.out.println(String.valueOf(100)); // 100 -> "100"

        //String toLowerCase() 转小写
        String s4 = "HellOWOrlD";
        String res1 = s4.toLowerCase();
        System.out.println(res1);

        //String toUpperCase()
        String res2 = s4.toUpperCase();
        System.out.println(res2);

        //String concat(String str) 字符串拼接操作
        String res3 = "李刚".concat("真帅!");
        System.out.println(res3);


    }
}
============================================================
/*
    String其它功能:
        替换功能
            String replace(char old,char new)
            String replace(String old,String new)
        去除字符串两空格
            String trim()
        按字典顺序比较两个字符串
            int compareTo(String str)
            int compareToIgnoreCase(String str)

 */
public class StringDemo7 {
    public static void main(String[] args) {
        String s1 = "dasqwajavadwqrajjavaiojadjavafq-aedjavaqajiqa";
        //String replace(char old,char new) 由新字符替换字符串中所有旧字符
        String res1 = s1.replace('a', '$');
        System.out.println(s1);
        System.out.println(res1);
        //String replace(String old,String new) 由新的字符串替换旧的字符串
        String res2 = s1.replace("java", "李刚");
        System.out.println(res2);
        //String trim() 去除字符串两边的空格
        String s2 = "    hello  world    ";
        System.out.println("s2: "+s2);
        String res3 = s2.trim();
        System.out.println("res3: "+res3);
        //int compareTo(String str) 按字典顺序比较两个字符串是否相同 当结果是0的时候表示两个字符串内容相同
        String s3 = "hello";
        String s4 = "world";
        String s5 = "hel";
        String s6 = "hello";
        System.out.println(s3.compareTo(s4)); // -15
        System.out.println(s3.compareTo(s5)); // 2
        System.out.println(s3.compareTo(s6)); // 0


    }
}    
    

5、StringBuffer类

StringBuffer类概述
我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
线程安全的可变字符序列
StringBuffer和String的区别?
构造方法
public StringBuffer() 
public StringBuffer(int capacity)
public StringBuffer(String str)
    
/*
    StringBuffer: 可变的字符序列,可以看作一个存储字符的一个容器
    构造方法:
        public StringBuffer()
        public StringBuffer(int capacity)
        public StringBuffer(String str)

 */
public class StringBufferDemo1 {
    public static void main(String[] args) {
        //public StringBuffer() 创建默认大小的StringBuffer容器
        StringBuffer sb1 = new StringBuffer();
        //获取默认StringBuffer容器的大小
        System.out.println("StringBuffer默认的容量大小为:" + sb1.capacity()); // 16
//        System.out.println("sb1: " + sb1);
//        System.out.println("StringBuffer实际存储字符数量:"+ sb1.length()); // 0

        //public StringBuffer(int capacity) 创建指定大小容量的StringBuffer
//        StringBuffer sb2 = new StringBuffer(50);
//        System.out.println("StringBuffer默认的容量大小为:" + sb2.capacity()); // 50
//        System.out.println("sb1: " + sb2);
//        System.out.println("StringBuffer实际存储字符数量:" + sb2.length()); // 0

        //public StringBuffer(String str) 创建默认大小的StringBuffer容器,其中存储了一个字符串
//        StringBuffer sb3 = new StringBuffer("hello");
//        System.out.println("StringBuffer默认的容量大小为:" + sb3.capacity()); // 21
//        System.out.println("sb1: " + sb3);
//        System.out.println("StringBuffer实际存储字符数量:" + sb3.length()); // 5
    }
}

============================================================
    
/*
    StringBuffer中的成员方法:
        添加功能
            public StringBuffer append(String str)
            public StringBuffer insert(int offset,String str)
        删除功能
            public StringBuffer deleteCharAt(int index)
            public StringBuffer delete(int start,int end)
        替换功能
            public StringBuffer replace(int start,int end,String str)
        反转功能
            public StringBuffer reverse()

 */
public class StringBufferDemo2 {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        System.out.println("sb: " + sb);
        System.out.println("------------------------");
        //public StringBuffer append(String str) 在StringBuffer末尾处添加新的字符串
//        StringBuffer sb2 = sb.append(100);
//        System.out.println("sb: "+sb);
//        System.out.println("sb2: "+sb2);
//        System.out.println(sb==sb2);
        sb.append(100);
        sb.append(12.343);
        sb.append(true);
        System.out.println("sb: " + sb);
        System.out.println("------------------------");

        //public StringBuffer insert(int offset,String str) 在StringBuffer指定位置中添加字符串
        //10012.343true
        sb.insert(10,"java");
        System.out.println("sb: " + sb);
        System.out.println("------------------------");

        //public StringBuffer deleteCharAt(int index) 指定索引删除StringBuffer某一个字符
        //10012.343tjavarue
        sb.deleteCharAt(5);
        System.out.println("sb: " + sb);
        System.out.println("------------------------");

        //public StringBuffer delete(int start,int end) 指定开始和结束索引,删除StringBuffer一部分字符
        //10012343tjavarue
        sb.delete(9,13); //[start, end)
        System.out.println("sb: " + sb);
        System.out.println("------------------------");

        //public StringBuffer replace(int start,int end,String str) 使用字符串替换StringBuffer一部分字符
        //10012343true
        sb.replace(6,9,"李刚");
        System.out.println("sb: " + sb);
        System.out.println("------------------------");

        //public StringBuffer reverse()
        //100123李刚rue
        sb.reverse();
        System.out.println("sb: " + sb);
        System.out.println("------------------------");
    }
}    

============================================================

/*
    public String substring(int start)
    public String substring(int start,int end)

 */
public class StringBufferDemo3 {
    public static void main(String[] args) {
        StringBuffer sb1 = new StringBuffer("hello world java hadoop");

        //public String substring(int start)
        String s1 = sb1.substring(4);
        System.out.println("sb1: "+sb1);
        System.out.println("s1: "+s1);

        //public String substring(int start,int end)
        String s2 = sb1.substring(6, 11);
        System.out.println("sb1: "+sb1);
        System.out.println("s2: "+s2);

    }
}

============================================================
/*
    String和StringBuffer的相互转换

    数据类型之间相互转换的场景:
    1、方法传参所需的类型与我自己值的类型不一样
    2、需要借助其它类型中的方法完成某功能

 */
public class StringBufferDemo4 {
    public static void main(String[] args) {
        String s1 = "hello";
        //String -> StringBuffer
        StringBuffer sb1 = new StringBuffer(s1);


        //StringBuffer->String
        String s2 = sb1.substring(0);
        String s3 = sb1.toString();
    }
}

6、Arrays类

Arrays类概述:
针对数组进行操作的工具类。
提供了排序,查找等功能。
成员方法
public static String toString(int[] a)
public static void sort(int[] a)
public static int binarySearch(int[] a,int key)

/*
    Arrays: java提供了一个类专门针对数组一系列操作的工具类

    public static String toString(int[] a)
    public static void sort(int[] a)
    public static int binarySearch(int[] a,int key)


 */
public class ArraysDemo1 {
    public static void main(String[] args) {
        //传入任意类型元素的一维数组,将其变成一个字符串形式返回
        int[] arr = {11,22,33,44,55};
//        String s1 = Arrays.toString(arr);
        System.out.println(Arrays.toString(arr));


        //public static void sort(int[] a)
        //对除了boolean类型以外的一维数组做排序
        int[] arr2 = {21,31,6,23,78,12,47};
        Arrays.sort(arr2); // 底层是快速排序
        System.out.println(Arrays.toString(arr2));


        //public static int binarySearch(int[] a,int key) 二分查找,前提是被查找的序列是有序的!
        //查找元素key在数组a中的位置
        //[6, 12, 21, 23, 31, 47, 78]
        int index = Arrays.binarySearch(arr2, 4);
        System.out.println(index); // -8  -1
    }
}
    

7、Integer类

Integer类概述
Integer 类在对象中包装了一个基本类型 int 的值
该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法
构造方法
public Integer(int value)
public Integer(String s)

/*
    包装类:
        java针对每一个基本数据类型都提供了与之对应的引用数据类型
            byte         Byte
            short        Short
            int          Integer
            long         Long
            float        Float
            double       Double
            boolean      Boolean
            char         Character


 */
public class IntegerDemo1 {
    public static void main(String[] args) {
        int a = 10;
        //获取int类型的最大值
        System.out.println(Integer.MAX_VALUE);

//        Integer i = new Integer(100);
//        Integer i = new Integer("100"); // "100" -> 100
//        System.out.println(i);

        Integer i = 100; // 自动装箱
        //当包装类做运算的时候,会自动将其中包装的值拆出来进行运算
        System.out.println(i+100);  // 自动拆箱
        System.out.println("--------------------------------");
        //将字符串转int类型
        int i1 = Integer.parseInt("100"); // String -> int
        System.out.println(i1);
        Integer i2 = Integer.valueOf("100"); // String -> Integer
        System.out.println(i2);
        Integer i3 = Integer.valueOf(100); // int -> Integer
        Integer i4 = 200;
        String s1 = i4.toString(); // Integer -> String
        String s2 = String.valueOf(100); // int -> String

        System.out.println("--------------------------------");
        //Character是char的包装类
        Character c1 = 'a'; // 自动装箱
        Character c2 = new Character('b');
        Character c3 = new Character('8');

        /*
            public static boolean isUpperCase(char ch)
            public static boolean isLowerCase(char ch)
            public static boolean isDigit(char ch)
            public static char toUpperCase(char ch)
            public static char toLowerCase(char ch)

         */
        System.out.println(Character.isUpperCase('b'));
        System.out.println(Character.isLowerCase('b'));
        System.out.println(Character.isDigit('e')); // 判断字符是否是数字
        System.out.println(Character.isDigit('9'));
        System.out.println(Character.toUpperCase('m'));
        System.out.println(Character.toLowerCase('P'));

    }
}    
    

8、Random类

Random类概述
此类用于产生随机数
如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
构造方法
public Random()
public Random(long seed)
    
import java.util.Random;

public class RandomDemo1 {
    public static void main(String[] args) {
//        Math.random() [0.0, 1.0)

        Random random = new Random();

//        System.out.println(random.nextInt());

        //1-100
        int i = random.nextInt(100) + 1; // [1,101)
        System.out.println(i);
    }
}

9、System类 Date类 DateFormat类

System类概述
System 类包含一些有用的类字段和方法。它不能被实例化。 
成员方法
public static void gc()
public static void exit(int status)
public static long currentTimeMillis()

/*
    System: 是和系统操作相关的类
        public static void gc() 垃圾回收
        public static void exit(int status) 强制退出程序
        public static long currentTimeMillis()

 */

public class SystemDemo1 {
    public static void main(String[] args) {
//        for(int i=0;i<=10;i++){
//            if(i==5){
//                System.exit(1);
//            }
//            System.out.println(i);
//        }
//
//        System.out.println("hello world");

        //public static long currentTimeMillis() 获取当前的时间戳
        //从1970年开始,1月1日0点0分0秒
//        System.out.println(System.currentTimeMillis());
        //....
    }
}    
    
============================================================import java.text.SimpleDateFormat;
import java.util.Date;

/*
    //1727510083386
    //需求:将时间戳转成指定的格式输出

    Date: java为了描述日期,提供了一个Date类
    构造方法:
        Date() 分配一个 Date对象,并初始化它,以便它代表它被分配的时间,测量到最近的毫秒。
        Date(long date) 分配一个 Date对象,并将其初始化为表示自称为“时代”的标准基准时间以后的指定毫秒数,即1970年1月1日00:00:00 GMT。

    SimpleDateFormat: java为了格式化日期提供的一个类
    构造方法:
        SimpleDateFormat(String pattern) 使用给定模式 SimpleDateFormat并使用默认的 FORMAT语言环境的默认日期格式符号。

 */
public class DateDemo1 {
    public static void main(String[] args) throws Exception{
//        Date d1 = new Date(); // 获取当前时间日期
//        System.out.println(d1); // Sat Sep 28 16:01:22 CST 2024

        // Date(long date) 将毫秒级别的时间戳转成Date类型对象
        Date d2 = new Date(1727510083386L);
        System.out.println(d2); // Sat Sep 28 15:54:43 CST 2024

        //xxxx年xx月xx日 xx时xx分xx秒
        //xxxx-xx-xx xx:xx:xx
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒 a");
        String time = sdf.format(d2); // Date -> String
        System.out.println(time);

        Date d3 = sdf.parse("2024年09月28日 03时54分43秒 下午");// String -> date
        System.out.println(d3);
    }
}    
   

标签:常用,String,int,System,println,public,out
From: https://www.cnblogs.com/snzjz/p/18475578

相关文章

  • WPF中Grid、StackPanel、Canvas、WrapPanel常用属性
    Grid常用属性Grid控件在WPF中非常强大,它提供了多种属性来定义行和列的布局。以下是一些常用的Grid属性:RowDefinitions和ColumnDefinitions:Grid 控件使用 RowDefinitions 和 ColumnDefinitions 来定义行和列的集合。每个 RowDefinition 和 ColumnDefinition......
  • 常用类:包装类,System类,Random类,Arrays
    包装类--integer相关包装inti1=Integer.parseInt("100");//String->intSystem.out.println(i1);Integeri2=Integer.valueOf("100");//String->IntegerSystem.out.println(i2);Integeri3=In......
  • 常用APIStringBuilder类
    StringBuilder代表可变字符串对象,相当于是一个容器,它里面的字符串是可以改变的,就是用来操作字符串的。好处:StringBuilder比String更合适做字符串的修改操作,效率更高,代码也更加简洁。1StringBuilder方法演示1.1字符串拼接接问题:builder.append();可以拼接int、long、d......
  • Golang 常用的五种创建型设计模式
    Golang常用的五种创建型设计模式原创GoOfficialBlogGoOfficialBlog 2024年10月18日19:10中国香港听全文在Go中,创建设计模式有助于管理对象的创建,并控制对象的实例化方式。这些模式在对象创建过程复杂或需要特殊处理时特别有用。以下是Go中常用的主要创建模式: ......
  • oracle 11g常用运维命令总结
    一、日常巡检命令1、检查Oracle实例状态SQL>setpages600lines600SQL>selectinstance_name,host_name,startup_time,status,database_statusfromv$instance;说明:“STATUS”表示Oracle当前的实例状态,必须为“OPEN”;“DATABASE_STATUS”表示Oracle当前数据库的状......
  • 解决下包慢的问题(常用命令)
    解决下包慢的问题(常用命令)1.切换npm的下包镜像源1.查看当前的下包镜像源​npmconfiggetregistry2.将下包的镜像源切换为淘宝镜像源​npmconfigsetregistry=https://registry.npmmirror.com/3.检查镜像源是否下载成功​npmconfiggetregistry2.......
  • 使用常用组件构建页面
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/......
  • Linux 常用指令全解析
    文章目录一、文件和目录操作指令1.`ls`2.`cd`3.`pwd`4.`mkdir`5.`rm`二、文件查看和编辑指令1.`cat`2.`more`和`less`3.`vi`或`vim`三、文件复制和移动指令1.`cp`2.`mv`四、系统管理指令1.`ps`2.`top`3.`kill`五、网络相关指令1.`ping`2.`ifconfig`或......
  • 网络常用工具
    软件工具puttySecureCRTCMD华为模拟器-enspwindwos常用命令pingtracertnslookupipconfigtelnet硬件工具Console线网络钳寻线仪红光笔光功率计故障处理常用方法对比分析互换分析仪表测试分段处理常见故障私接路由排查通过arp-a获取mac地址,再通过交......
  • 容器运维必备-Docker 常用命令
    前言:在Kubernetes的日常运维中,虽然我们主要依赖kubectl命令来管理容器和集群,但有时候,Docker的一些命令因其直观和便捷性,能够为我们提供极大的帮助。以下是一些Docker的常用命令,它们可以在Kubernetes环境中作为辅助工具使用,以提高我们的工作效率和操作的灵活性以下是Doc......