java包装类型

Java包装类型(也称为装箱类型)是为了解决基本数据类型不能直接参与面向对象的特性,例如使用泛型、集合等。然而,由于装箱类型在底层是通过对象包装基本数据类型,因此在某些情况下可能会导致性能问题,尤其是在频繁的装箱和拆箱操作中。 如果在性能敏感的代码中使用包装类型,可以考虑以下几种方法来提高性能: 使用基本数据类型:优先使用基本数据类型(如int、double等)而不是装箱类型(如Integer、Double等),这样可以避免装箱和拆箱操作的开销。 使用valueOf()方法:对于装箱操作,可以使用valueOf()方法来获取包装对象,而不是使用构造函数。valueOf()方法内部可能会使用缓存,从而避免创建不必要的新对象。 例如: Integer num = Integer.valueOf(42); // 使用valueOf()方法 使用缓存:对于小范围的整数或常用的浮点数,Java在装箱操作时会进行缓存,因此在这些值的装箱和拆箱操作上会更快。 例如: Integer num1 = 42; // 自动装箱,使用缓存 int num2 = num1; // 自动拆箱 避免频繁装箱和拆箱:在循环或高频操作中,尽量避免频繁地进行装箱和拆箱操作,可以将基本数据类型放入集合或数组中,而不是使用装箱类型的集合。 使用Java 8的Optional类:对于可能为null的值,可以使用Java 8中的Optional类来处理,而不是使用可能会引起装箱的装箱类型。 例如: Optional<Integer> optionalNum = Optional.ofNullable(someValue); // 使用Optional类 使用第三方库:有些第三方库专门针对性能进行了优化,可以考虑使用这些库来处理特定的性能问题。 总的来说,在性能敏感的代码中,尽量减少装箱和拆箱的次数,优先使用基本数据类型,并合理使用缓存和第三方库来提高性能。在其他情况下,使用装箱类型可以带来更好的可读性和灵活性。优化性能时应先进行性能测试,确保优化方案真正带来了实质的性能提升。

阅读全文

设计原则

  • 多用组合少用继承。
  • 找出应用中可能需要变化之处,把他们独立出来,不要和那些不需要变化的代码混合在一起。
  • 针对接口编程,而不是针对实现编程。
  • 为了交互对象之间的松耦合设计而努力。
  • 类应该对扩展开放,对修改关闭。
  • 要依赖抽象,不要依赖具体类。
  • 最少知识原则:只和你的密友谈话。(相当于类间交互适当封装,减少耦合,只让必要部分交互)

阅读全文

Timeout using @Transactional annotation

To set request timeout on database queries or calls by using Spring’s @Transactional annotation. We may set the timeout attribute that it has. It has a default value of -1, which is the same as having no timeout at all.

Let’s say we have set this timeout to 50 seconds. An exception will be raised if the annotated method takes longer than this amount of time to execute. This approach is quite handy for time-consuming database searches or long-running SQL queries.

阅读全文

Java 面试题汇编 (七)

1. Multi threading 1.1 How do you create a thread In Java, there are two main ways to create a thread: by extending the Thread class, or by implementing the Runnable interface. Extending the Thread class: To create a thread by extending the Thread class, you need to create a new class that extends Thread and overrides its run() method. The run() method contains the code that will be executed in the new thread.

阅读全文

Java 面试题汇编 (六)

1. Collections 1.1 Why do we need collections in Java Collections in Java are a way to store and manipulate groups of objects. They are essential because they provide a way to manage data in a flexible and dynamic manner. Here are some of the key reasons why we need collections in Java: Dynamic storage: Collections allow us to store and manipulate a large number of objects dynamically, without requiring us to know the exact number of objects in advance.

阅读全文

Java 面试题汇编 (五)

1. Advanced object oriented concepts 1.1 What is polymorphism Polymorphism is a fundamental concept in object-oriented programming (OOP) and refers to the ability of an object to take on multiple forms or behaviors. In Java, polymorphism allows an object to be treated as if it is of any type in its inheritance hierarchy. Java supports two types of polymorphism: static polymorphism (also known as method overloading) and dynamic polymorphism (also known as method overriding).

阅读全文

Java 面试题汇编 (四)

1. Wrapper Classes

1.1 What are Wrapper classes

Wrapper classes are classes in object-oriented programming that are used to wrap or encapsulate primitive data types so that they can be treated as objects. In Java, for example, there are several wrapper classes such as Integer, Double, and Boolean that correspond to the primitive data types int, double, and boolean, respectively.

Wrapper classes provide a way to represent primitive data types as objects, which allows them to be used in situations where objects are required, such as in collections or as method arguments. They also provide additional functionality, such as methods for converting between the wrapped value and other data types.

阅读全文

作者的图片

闲散工程师笔记

一名不断探索、坚持学习的闲散工程师

云原生.盐酒猿

CHINA🇨🇳