秋招Java后端开发冲刺——基础篇2

Java语言基础

一、基本数据类型(8个)

  1. 整型:byte(1字节)、 short(2字节)、int(4字节)、 long(8字节,使用时一定要在数值后面加上 L)
    :整型类型的最高位表示数据符号位,因此实际上数据的表示范围为( − 2 字节数 , 2 字节数 - 2^{字节数},2^{字节数} 2字节数2字节数
  2. 浮点型:float(4字节),double(8字节)
  3. 字符型:char(2字节)
  4. 布尔型: boolean(1bit)

二、包装类型(Java中的类)

  1. 整型:Byte、Short、Integer、Long
  2. 浮点型:Float、Double
  3. 字符型:Character
  4. 布尔型:Boolean
    :String不是包装类型,是Java中的一个类

三、基本数据类型和包装类型间区别与联系

类型泛型存储位置占用空间默认值比较方式
基本数据类型×栈、堆、方法区(元空间)不同类型不同默认值==
包装类型nullequals()

类型间转换:

  • 自动装箱:直接将基本数据类型的变量赋值给包装类对象,这是Java编译器自动处理的,从Java 5开始引入的特性。例如,Integer boxedIntValue = intValue。
  • 显式装箱:使用包装类的静态方法valueOf进行转换,例如,Integer explicitlyBoxedIntValue = Integer.valueOf(intValue)。
  • 自动拆箱:直接将包装类型的对象赋值给基本数据类型变量
  • 显式拆箱:使用包装类的静态方法xxxValue()进行转换,例如,int baseValue = boxedIntValue.intValue()。
  1. 基本数据类型 -> 包装类型
// 基本数据类型
        int intValue = 5;
        char charValue = 'a';
        boolean booleanValue = true;
        byte byteValue = 1;
        short shortValue = 2;
        long longValue = 100L;
        float floatValue = 3.14f;
        double doubleValue = 3.14159;

        // 自动装箱:将基本数据类型转换为包装类型
        Integer boxedIntValue = intValue;
        Character boxedCharValue = charValue;
        Boolean boxedBooleanValue = booleanValue;
        Byte boxedByteValue = byteValue;
        Short boxedShortValue = shortValue;
        Long boxedLongValue = longValue;
        Float boxedFloatValue = floatValue;
        Double boxedDoubleValue = doubleValue;

        // 显示装箱:使用包装类的构造函数
        Integer explicitlyBoxedIntValue = Integer.valueOf(intValue);
        Character explicitlyBoxedCharValue = Character.valueOf(charValue);
        Boolean explicitlyBoxedBooleanValue = Boolean.valueOf(booleanValue);
        Byte explicitlyBoxedByteValue = Byte.valueOf(byteValue);
        Short explicitlyBoxedShortValue = Short.valueOf(shortValue);
        Long explicitlyBoxedLongValue = Long.valueOf(longValue);
        Float explicitlyBoxedFloatValue = Float.valueOf(floatValue);
        Double explicitlyBoxedDoubleValue = Double.valueOf(doubleValue);
  1. 包装类型 -> 基本数据类型
Integer boxedIntValue = Integer.valueOf(5);
        Character boxedCharValue = Character.valueOf('a');
        Boolean boxedBooleanValue = Boolean.valueOf(true);
        Byte boxedByteValue = Byte.valueOf((byte) 1);
        Short boxedShortValue = Short.valueOf((short) 2);
        Long boxedLongValue = Long.valueOf(100L);
        Float boxedFloatValue = Float.valueOf(3.14f);
        Double boxedDoubleValue = Double.valueOf(3.14159);

        // 自动拆箱:将包装类型转换为基本数据类型
        int intValue = boxedIntValue;
        char charValue = boxedCharValue;
        boolean booleanValue = boxedBooleanValue;
        byte byteValue = boxedByteValue;
        short shortValue = boxedShortValue;
        long longValue = boxedLongValue;
        float floatValue = boxedFloatValue;
        double doubleValue = boxedDoubleValue;

        // 显示拆箱:使用包装类的对应方法
        int explicitlyUnboxedIntValue = boxedIntValue.intValue();
        char explicitlyUnboxedCharValue = boxedCharValue.charValue();
        boolean explicitlyUnboxedBooleanValue = boxedBooleanValue.booleanValue();
        byte explicitlyUnboxedByteValue = boxedByteValue.byteValue();
        short explicitlyUnboxedShortValue = boxedShortValue.shortValue();
        long explicitlyUnboxedLongValue = boxedLongValue.longValue();
        float explicitlyUnboxedFloatValue = boxedFloatValue.floatValue();
        double explicitlyUnboxedDoubleValue = boxedDoubleValue.doubleValue();

四、方法

  1. 静态方法和普通方法的区别
类型调用方式访问限制
静态方法对象名.方法名()/ 类名.方法名()只能访问类的静态变量或静态方法
普通方法对象名.方法名()无限制
:静态方法是在类加载的时候就分配了内存空间,而类的非静态成员是在创建对象时才分配内存空间,因此静态方法访问非静态成员是非法的。
  1. 重载
  • 方法重载:指的是在同一个类中定义两个方法名完全相同的方法,但两个方法的形式参数不同(类型、个数和顺序),在调用的时候程序会根据实际参数与形式参数的匹配自动调用正确的方法。
    :形参名称不同是错误的
  • 重载方法一般用于实现类似的功能,比如不同类型数据的加减等运算
  • 构造方法可以重载
public class methodTest {
    //整数+整数
    public int add(int a,int b){
        return a+b;
    }
    //整数+浮点数
    public double add(double a,int b){
        return a+b;
    }
    //浮点数+浮点数
    public double add(double a,double b){
        return a+b;
    }
    public static void main(String[] args){
        methodTest mt=new methodTest();
        int result1=mt.add(1,2);
        double result2 = mt.add(1.2,5);
        double result3=mt.add(2.3,5.2);
    }
}
  1. 重写
  • 方法重写:指的是在子类重写父类中的同一个方法,实现不同的逻辑
  • 方法名、参数列表必须相同,子类方法返回值类型应比父类方法返回值类型更小或相等
    :private/final/static 修饰的方法不能被重写,构造方法不能被重写
// 父类
class Animal {
    // 父类中的方法
    public void makeSound() {
        System.out.println("The animal makes a sound");
    }

    // 父类中的另一方法
    public void sleep() {
        System.out.println("The animal is sleeping");
    }
}

// 子类
class Dog extends Animal {
    // 子类重写父类的方法
    @Override
    public void makeSound() {
        System.out.println("The dog barks");
    }
}
  • 35
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值