java小面

20 阅读3分钟

1.int和Integer有什么区别?

数据类型不同:int 是基础数据类型,而 Integer 是包装数据类型;
默认值不同:int 的默认值是 0,而 Integer 的默认值是 null;
内存中存储的方式不同:int 在内存中直接存储的是数据值,而 Integer 实际存储的是对象引用,
当 new 一个 Integer 时实际上是生成一个指针指向此对象;
实例化方式不同:Integer 必须实例化才可以使用,而 int 不需要;
变量的比较方式不同:int 可以使用 == 来对比两个变量是否相等,
而 Integer 一定要使用 equals 来比较两个变量是否相等;
泛型使用不同:Integer 能用于泛型定义,而 int 类型却不行。

2.什么时候用 int 和 Integer?

Integer 典型使用场景:在 Spring Boot 接收参数的时候,通常会使用 Integer
 而非 int,因为 Integer 的默认值是 null,而 int 的默认值是 0。如果接收参数使用
 int 的话,那么前端如果忘记传递此参数,程序就会报错(提示 500 内部错误)。
因为前端不传参是 nullnull 不能被强转为 0,所以使用 int 就会报错。
但如果使用的是 Integer 类型,则没有这个问题,程序也不会报错,
所以 Spring Boot 中 Controller 接收参数时,通常会使用 Integerint 典型使用场景:int 常用于定义类的属性类型,因为属性类型,
不会 int 不会被赋值为 null(编译器会报错),所以这种场景下,
使用占用资源更少的 int 类型,程序的执行效率会更高。

3.HashMap 底层实现?

HashMap的底层实现原理HashMap是Java集合框架中的一个重要类,底层是基于哈希表实现的。它可以存储键值对,
并允许存储null键和null值。HashMap的底层实现主要包括以下几个特性:数组:HashMap底层是一个数组,用于存储键值对节点的数据结构。链表:当碰撞(两个键的哈希值相同)发生时,HashMap使用链表来解决。红黑树:当链表长度超过阈值(8)时,链表会转换为红黑树来优化性能。加载因子:HashMap的加载因子决定了HashMap的数组何时应该扩容。哈希算法:HashMap使用哈希算法来计算键值对应该存储在数组的哪个位置。下面是一个简单的示例,展示了如何使用HashMap:
import java.util.HashMap; public class HashMapExample {    public static void main(String[] args) {        // 创建一个HashMap实例        HashMap<Integer, String> map = new HashMap<>();         // 添加键值对        map.put(1, "Apple");        map.put(2, "Banana");        map.put(3, "Cherry");         // 获取并打印值        System.out.println(map.get(1)); // 输出: Apple         // 遍历HashMap        for (Integer key : map.keySet()) {            System.out.println(key + ": " + map.get(key));        }    }}
在这个示例中,我们创建了一个HashMap实例,添加了三个键值对,并展示了如何获取和遍历HashMap中的元素。

4.SpringBoot 如何修改端口号?

参考答案:在 Spring Boot 中的配置文件中设置“server.port=xxx”就可以修改端口号了。

5.SpringBoot如何配置Redis?

SpringBoot如何配置Redis?在Spring Boot中配置Redis,你需要做以下几步:

1.添加依赖:在pom.xml中添加Spring Data RedisJedis的依赖。<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>

2.配置application.properties或application.yml:在配置文件中设置Redis服务器的连接信息。# application.properties
spring.redis.host=localhost
spring.redis.port=6379



或者使用YAML格式:# application.yml
spring:
  redis:
    host: localhost
    port: 6379

3.使用RedisTemplateStringRedisTemplate操作Redisimport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
 
@RestController
public class RedisController {
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
    @GetMapping("/set")
    public String setKey(@RequestParam String key, @RequestParam Object value) {
        redisTemplate.opsForValue().set(key, value);
        return "success";
    }
 
    @GetMapping("/get")
    public Object getKey(@RequestParam String key) {
        return redisTemplate.opsForValue().get(key);
    }
}以上步骤可以让你在Spring Boot项目中配置并使用Redis。根据实际需求,
你可能需要配置连接池、密码、使用Lettuce代替Jedis等高级设置。