java 面试 —— java 基础

1. char => int

  • char 类型转换为 int 类型时,是转换为其 ascii 码或 unicode 码(比如中文)

    char ch = 'A';
    int i = ch; // (int)ch
        // i = 65;
    int ch = '香';
    int i = ch; // (int)ch
        // i = 39321;   \u9999;

2. float

  • Infinity 与 NaN

    public final class Float extends Number implements Comparable<Float> {
        public static final float POSITIVE_INFINITY = 1.0f / 0.0f;  
        public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
        public static final float NaN = 0.0f / 0.0f;
    
        public static boolean isNaN(float v) {
            return (v != v);
        }
    }
    1/0
        java.lang.ArithmeticException thrown: / by zero
        // 除 0 异常,只有在除数为 0 时才会发生;
    1.0f/0.0f == 1.0f/0.0f;
        // true;
    1.0f/0.0f == 2.0f/0.0f;
        // true;
    0.0f/0.0f == 0.0f/0.0f;
        // false;

3. String

jshell> new String() == new String()
$7 ==> false

jshell> String s = "abc";
s ==> "abc"

jshell> String s2 = new String("abc")
s2 ==> "abc"

jshell> s2 == s
$10 ==> false

jshell> s2.intern() == s
$11 ==> true
  • java String pool:字符串常量池(在堆空间)

    String str1 = “ABC”; 可能创建一个对象或者不创建对象。
    如果”ABC” 这个字符串在java String池中不存在,会在java String池中创建一个String str1= “ABC”的对象。然后把str1指向这个内存地址。之后用这种方式创建多少个值为”ABC”的字符串对象。始终只有一个内存地址被分配,之后都是String的copy。这种被称为‘字符串驻留’,所有的字符串都会在编译之后自动驻留。


    这里写图片描述

  • 源码

    @Stable
    private final byte[] value;
    
    /** Cache the hash code for the string */
    // Java的字符串的hash做了缓存,第一次才会真正算,以后都是取缓存值。
    private int hash; // Default to 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值