前端开发入门到精通的在线学习网站

网站首页 > 资源文章 正文

十五、Java运算符-赋值运算符与instanceof运算符

qiguaw 2024-10-08 06:50:41 资源文章 12 ℃ 0 评论

赋值运算符

符号

说明

描述

=

赋值

将右操作数的值赋给左侧操作数

+=

加和赋值

将左操作数和右操作数相加赋值给左操作数

-=

减和赋值

将左操作数和右操作数相减赋值给左操作数

/=

除和赋值

将左操作数和右操作数相除赋值给左操作数

*=

乘和赋值

将左操作数和右操作数相乘赋值给左操作数

(%)=

取余和赋值

将左操作数和右操作数取余后赋值给左操作数

<<=

左移位赋值


>>=

右移位赋值


>>>=

无符号右移赋值


&=

按位与赋值


^=

按位异或赋值


|=

按位或赋值


案例:

public class AssignmentOperatorsExample {
    public static void main(String[] args) {
        // 基础赋值运算符 =
        int baseValue = 10;
        System.out.println("Original baseValue: " + baseValue);

        // 复合赋值运算符
        // 1. 加法赋值运算符 +=
        baseValue += 5;
        System.out.println("baseValue after adding 5: " + baseValue);

        // 2. 减法赋值运算符 -=
        baseValue -= 3;
        System.out.println("baseValue after subtracting 3: " + baseValue);

        // 3. 乘法赋值运算符 *=
        baseValue *= 2;
        System.out.println("baseValue after multiplying by 2: " + baseValue);

        // 4. 除法赋值运算符 /=
        baseValue /= 2;
        System.out.println("baseValue after dividing by 2: " + baseValue);

        // 5. 模(取余)赋值运算符 %=
        baseValue %= 3;
        System.out.println("baseValue after taking modulo with 3: " + baseValue);

        // 6. 左移赋值运算符 <<=
        baseValue <<= 1;
        System.out.println("baseValue after shifting left by 1 bit: " + baseValue);

        // 7. 右移赋值运算符 >>=
        baseValue >>= 1;
        System.out.println("baseValue after shifting right by 1 bit: " + baseValue);

        // 8. 无符号右移赋值运算符 >>>=
        baseValue = 10;
        baseValue >>>= 1;
        System.out.println("baseValue after unsigned right shift by 1 bit: " + baseValue);

        // 9. 按位与赋值运算符 &=
        int anotherValue = 5;
        anotherValue &= 3; // 只保留同时存在于anotherValue和3中的二进制位
        System.out.println("anotherValue after bitwise AND assignment: " + anotherValue);

        // 10. 按位或赋值运算符 |=
        anotherValue |= 2; // 添加另一个Value中不存在但在2中存在的二进制位
        System.out.println("anotherValue after bitwise OR assignment: " + anotherValue);

        // 11. 按位异或赋值运算符 ^=
        anotherValue ^= 3; // 对anotherValue和3的相同位进行翻转
        System.out.println("anotherValue after bitwise XOR assignment: " + anotherValue);
    }
}

结果:

Original baseValue: 10
baseValue after adding 5: 15
baseValue after subtracting 3: 12
baseValue after multiplying by 2: 24
baseValue after dividing by 2: 12
baseValue after taking modulo with 3: 0
baseValue after shifting left by 1 bit: 0
baseValue after shifting right by 1 bit: 0
baseValue after unsigned right shift by 1 bit: 5
anotherValue after bitwise AND assignment: 1
anotherValue after bitwise OR assignment: 3
anotherValue after bitwise XOR assignment: 0

instanceof运算符

在Java编程语言中,"instanceof" 是一个二元运算符,检测对象是否属于某个类或者实现了某个接口。

语法格式:

object instanceof ClassName

参数

说明

object

要检查的对象引用

ClassName

类的名字,也可以是接口的名字

当“object”引用的对象是由“ClassName”类或其任何子类实例化时,“instanceof”运算符返回“true”;如果对象不是“ClassName”类或其任何子类的实例,则返回“false”。

例如:

public class InstanceOfExample {
    public static void main(String[] args) {
        // 创建一个对象
        Animal animal = new Dog();

        // 使用 instanceof 运算符进行类型检查和转换
        checkType(animal);
    }

    // 定义一个通用方法检查对象类型并打印结果
    public static void checkType(Object obj) {
        if (obj instanceof Animal) {
            System.out.println(obj + " 是 Animal 类型或其子类的实例.");
        }

        if (obj instanceof Dog) {
            System.out.println(obj + " 是 Dog 类型的实例.");
            // 如果确定是 Dog 类型,还可以安全地向下转型
            Dog dog = (Dog) obj;
            dog.bark();
        }

        if (obj instanceof Cat) {
            System.out.println(obj + " 是 Cat 类型的实例.");
        }
    }
}

// 基类
class Animal {
    // ...
}

// 子类
class Dog extends Animal {
    public void bark() {
        System.out.println("Woof!");
    }
}

class Cat extends Animal {
    // ...
}

结果:

org.chewing.exercise.Dog@682a0b20 是 Animal 类型或其子类的实例.
org.chewing.exercise.Dog@682a0b20 是 Dog 类型的实例.
Woof!

“instanceof“运算符经常用于类型安全的向下转型前的检查,确保对象可以被安全地转换为其父类或接口的子类型。

例如:

if (myAnimal instanceof Dog) {
    Dog myDog = (Dog) myAnimal; // 在确定是Dog类型之后进行安全的类型转换
    // ... 进一步操作 myDog ...
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表