栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java里的好东西

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java里的好东西

java静态内部类

1,静态(static)内部类不需要与外部类对象之间有联系,通常称为嵌套类(nested class).

2,通常的内部类需要在外部类实例化之后才能实例化,而静态内部类不依赖于外部类实例而实例化

3,普通的内部类对象隐含的保存了一个引用,指向创建它的外部类对象

4,静态内部类中可以定义静态或者非静态的成员

5,静态内部类内不能访问外部类的非静态成员,


 * Java的值传递和所谓的引用传递
 * 本质上Java只有值传递,所有的赋值传参都是一次值的拷贝。
 * 引用数据类型拷贝的就是引用地址,基本数据类型拷贝的是值,不会传入实例对象本身。

 *  常用api
 *  API(Application Programming Interface)应用程序接口。
 *  JDK给我们提供的一些已经写好的类,我们可以直接调方法来解决问题。
 *  我们类的方法,在宏观上都可以称为接口。
 *  api文档,介绍api


 * 时间相关api
 * 时间:
 * 格林尼治天文台,伦敦
 * 时区:东八区
 * 北京时间,+8。
 * 时间戳:格林尼治时间1970.1.1 00:00:00 到今天2022.7.28 9:29:30的毫秒数
 * 1s=1000ms,1min=60s,1h=60min,1day=24h。时间戳在全世界都是固定的。
 

// 获取时间戳
可以通过时间戳转换成我们当前所在地的具体时间和日期
System.out.println(System.currentTimeMillis());

获取系统当前日期时间
        Date date = new Date();
        System.out.println(date);

 public static void main(String[] args) {
        Date date1 = new Date(1658974415756L); // 10:13:35
        Date date2 = new Date();

        System.out.println(date2.after(date1));
        
        System.out.println(date1.compareTo(date2));
        System.out.println(date1.getYear());
    }
}

 * 日历类:日期时间
 * Calendar是一个抽象类。
 * 初始化:
 * 提供了一组对“年月日时分秒星期”等信息的操作函数。操作不同时区的信息。
 * JDK1.1版本开始,在处理时间和日期时,系统推荐使用Calendar类。
 *  Calendar要比Date强大的多

  Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
        System.out.println(calendar.get(Calendar.DATE));
        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.HOUR));
        System.out.println(calendar.get(Calendar.MILLISECOND));

 * ZoneId

时区:
  System.out.println(TimeZone.getDefault());
        System.out.println(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT-08:00"));
        System.out.println(calendar.get(Calendar.HOUR));
  ZoneId zoneId = ZoneId.of("America/New_York");
        System.out.println(TimeZone.getTimeZone(zoneId));

 * 日期格式化
 * SimpleDateFormat
 * format:格式化Date类型,把Date类型转成String类型
 *  我们要展示数据到客户端。
 * parse:把String类型的时间,转成Date类型
 *  从客户端传过来的时间,一般都是String类型,存入数据库。

  yyyy:年
            yy:年的后两位
            MM:月
            dd:日
            HH:小时 24小时制
            hh:小时 12小时制
            mm:分
            ss:秒
            SSS:毫秒
 

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        String s = sdf.format(date);
        System.out.println(s);


String字符串翻转的三种方式:

一,

public class Demo01 {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String str = "abcdefg";
        char[] array = str.toCharArray();
        String str2 = "";
        for (int i = array.length- 1;i >= 0; i--) {
            str2 += array[i];
        }
        str = str2;
        System.out.println(str);
        long end = System.currentTimeMillis();
        System.out.println(end - start); // 23
    }
}

二,

public class Demo02 {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String str = "abcdefg";
        String str2 = "";
        for (int i = str.length() - 1; i >= 0; i--) {
            str2 += str.charAt(i);
        }
        str = str2;
        System.out.println(str);
        long end = System.currentTimeMillis();
        System.out.println(end - start); // 25
    }
}

三,

public class Demo3 {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String str = "abcdefg";
        StringBuffer stringBuffer = new StringBuffer(str);
        stringBuffer.reverse();
        str = stringBuffer.toString();
        System.out.println(str);

        long end = System.currentTimeMillis();
        System.out.println(end - start); // 1
    }
}

 * JDK8的日期时间类
 * 在JDK8之前,处理日期和时间,基本上就是用我们上面的几个类。
 * Date和Calendar,获取到的月份都是0-11,而不是我们生活中的1-12。
 *
 * 阿里巴巴规约明确要求:
 * 如果是jdk8的应用,可以使用
 * Instant代替Date
 * LocalDateTime代替Calendar
 * DateTimeFormatter代替SimpleDateFormat
 *
 * 新的实践类:
 * Instant:
 * LocalDate
 * LocalTime
 * LocalDateTime
 * DateTimeFormatter

public class Ch01 {
    public static void main(String[] args) {
        Instant instant = Instant.now();
        System.out.println(instant.atZone(ZoneId.systemDefault()));
        // 获取秒数
        System.out.println(instant.getEpochSecond());
        // 获取毫秒数
        System.out.println(instant.toEpochMilli());
        long millis = System.currentTimeMillis();
        System.out.println(Instant.ofEpochMilli(millis));

        System.out.println("---------------------------------------------");
        String text = "2020-07-28T14:06:50.147Z";
        Instant parse = Instant.parse(text);
        System.out.println(parse);

    }
}

* 持续时间Duration

public class Ch02 {

    public static void main(String[] args) {
        Instant instant = Instant.now();
        Instant plus = instant.plus(Duration.ofDays(5));
        System.out.println(plus);
    }
}

 * LocalDate:获取当前日期

public class Ch03 {

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println(now);
        System.out.println(now.getMonth());
        System.out.println(now.getDayOfWeek());
        System.out.println(now.getDayOfMonth());
        LocalDate of = LocalDate.of(2024, 9, 10);
        System.out.println(of);
        // 判断是否为闰年
        // 能被4整除不被100,被400
        System.out.println(of.isLeapYear());
    }
}

 * LocalTime:本地时间

public class Ch04 {

    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        System.out.println(now);

    }
}

 * LocalDateTime:获取日期和时间

public class Ch05 {

    public static void main(String[] args) {
        System.out.println(LocalDateTime.now());
    }
}

 * DateTimeFormatter
 * 处理日期的格式化问题

public class Ch06 {

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");

        System.out.println(now.format(dateTimeFormatter));
    }
}

 * Instant和Date的转换
 * Instant和LocalDate转换
 * Date和LocalDateTime转换

    public static void main(String[] args) {
        Instant instant = Instant.now();
        // 把Instant转成Date
        Date date = Date.from(instant);
        // 把Date转成Instant
        Instant toInstant = date.toInstant();
        // 把Instant转成LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        // 把LocalDateTime转成Instant
        Instant instant1 = LocalDateTime.now().toInstant(ZoneOffset.UTC);

        // 把date转成LocalDateTime
        Date date1 = new Date();
        Instant instant2 = date.toInstant();
        LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault());

        // 把LocalDateTime转成Date
        LocalDateTime now = LocalDateTime.now();
        Instant instant3 = now.atZone(ZoneId.systemDefault()).toInstant();
        Date date2 = Date.from(instant3);

 * 数学类Math
 * 随机数 random()  double
 * 向上取整 ceil()  double
 * 向下取整 floor() double
 * 四舍五入 round() long
 *
 * Math.E
 * Math.PI

       // 生成0-1的随机数
        // 生成1-100的随机整数
        // 生成13-26的随机整数
        // (int)(Math.random()*(max-min)+min)
        System.out.println(Math.random());
        System.out.println((int)(Math.random() * (26 - 13) + 13));
    }
}

 * BigDecimal统计类

 public static void main(String[] args) {
        double d1 = 0.7;
        double d2 = 0.1;
        BigDecimal bigDecimal1 = new BigDecimal(d1);
        BigDecimal bigDecimal2 = new BigDecimal(d2);

        // 使用BigDecimal的构造器,开发中,传入的参数必须是字符串
        BigDecimal bigDecimal3 = new BigDecimal("0.7");
        BigDecimal bigDecimal4 = new BigDecimal("0.1");

        System.out.println(bigDecimal3.add(bigDecimal4));
    }

 * 随机
 * Random

public class Ch10 {

    public static void main(String[] args) {
        Random random = new Random();
        System.out.println(random.nextDouble());

    }
}

 * Arrays数组的工具类

public class Ch11 {
    public static void main(String[] args) {
        int [] arr = new int[]{1,2,3,4,5};
        System.out.println(Arrays.toString(arr));
        Arrays.sort(arr);
        System.out.println(Arrays.binarySearch(arr, 4));
        int[] ints = Arrays.copyOf(arr, 2);
        System.out.println(Arrays.toString(ints));
        System.out.println(Arrays.equals(arr, ints));
    }
}

 * System类
 * 系统类

public class Ch12 {
    public static void main(String[] args) {
//        System.exit(-1);
//        System.out.println();
//        System.err.println("错误信息");
//        System.currentTimeMillis();
        Properties properties = System.getProperties();
        System.out.println(properties);
    }
}

 * Objects

 System.out.println(Objects.isNull(""));

* StringBuffer和StringBuilder
 * 可变的字符序列,和String是有本质区别的
 *
 *  String怎么反转???
 *
 *  StringBuffer是同步的。安全,效率低
 *  StringBuilder是异步的。不安全。效率高

 public static void main(String[] args) {
     StringBuffer stringBuffer = new StringBuffer("你好");
       System.out.println(stringBuffer);
     stringBuffer.append(",我好").append(",大家好");
       System.out.println(stringBuffer);
       stringBuffer.delete(4,7);
       stringBuffer.deleteCharAt(0);
       stringBuffer.insert(1,"哈哈");
     stringBuffer.reverse();
     System.out.println(stringBuffer);
       String str = "hello";
       String s = str.concat(",world!!");
       System.out.println("str = " + str);
       System.out.println("s = " + s);
 }

 *  面试题:
 *  三个字符串的区别?

区别一:

String:适用于少量的字符串操作。

StringBuilder:适用于单线程下在字符串缓冲区进行大量操作。

StringBuffer:适用于多线程下在字符串缓冲区进行大量操作

区别二:

String是final类不能被继承且为字符串常量,而StringBuilder和StringBuffer均为字符串变量。“字符串常量一旦声明则不可改变,而字符串对象可以改变,但是改变的是其内存地址的指向。”所以String类不适合于频繁修改的字符串操作上,所以在这种情况下,往往可以使用StringBuffer类,即StringBuffer类方便用户进行内容修改。String类型是字符串常量,当进行字符串操作时,地址即发生改变。StringBuffer和StringBuilder类型是字符串变量,当使用apend()等操作时,内存地址不发生变化

区别三:

在String类中使用“+”作为数据的连接操作,而在StringBuffer类中使用append()方法(方法定义:public StringBuffer append(数据类型 变量))进行数据连接。String、StringBuffer和StringBuilder类都是CharSequence接口的子类,也就证明String、StringBuffer和StringBuilder类的对象都可以利用自动向上转型的操作为CharSequence接口实例化。

区别四:线程安全性方面

1.StringBuilder(非线程安全)

该类提供一个与StringBuffer兼容的 API,但不能保证同步,所以在性能上较高。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。两者的方法基本相同。

2.StringBuffer(线程安全的)

StringBuffer上的主要操作是 append 和 insert 方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串缓冲区中。append 方法始终将这些字符添加到缓冲区的末端;而 insert 方法则在指定的点添加字符。
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1031325.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号