isprimitive_Java类class isPrimitive()方法及示例

isprimitive

类class isPrimitive()方法 (Class class isPrimitive() method)

  • isPrimitive() method is available in java.lang package.

    isPrimitive()方法在java.lang包中可用。

  • isPrimitive() method is used to check whether this Class object denotes a primitive type or not.

    isPrimitive()方法用于检查此Class对象是否表示原始类型。

  • In Java, we have a predefined Class object to denote primitive and void but the important thing Class object has a similar name as primitives like byte, char, short, int, long, float and double.

    在Java中,我们有一个预定义的Class对象来表示基元和void,但重要的是Class对象的名称与诸如byte,char,short,int,long,float和double之类的基元相似。

  • isPrimitive() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    isPrimitive()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • isPrimitive() method does not throw an exception at the time of checking primitive.

    isPrimitive()方法在检查基元时不会引发异常。

Syntax:

句法:

    public boolean isPrimitive();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is boolean, it returns a boolean value based on the following cases,

此方法的返回类型为boolean ,它基于以下情况返回布尔值:

  • It returns true, when this Class object denotes primitive type.

    当此Class对象表示原始类型时,它返回true

  • It returns false, when this Class object does not denote a primitive type.

    当此Class对象不表示原始类型时,它返回false

Example:

例:

// Java program to demonstrate the example 
// of boolean isPrimitive() method of Class 

public class IsPrimitiveOfClass {
    public static void main(String[] args) {
        // Create and Return String class
        String str = new String();
        Class cl1 = str.getClass();

        // Create and Return short
        short sh = 10;
        Class cl2 = short.class;

        // We are checking the class denotes primitive type
        boolean b1 = cl1.isPrimitive();
        boolean b2 = cl2.isPrimitive();

        System.out.print("Is" + " " + cl1.getSimpleName() + " ");
        System.out.println("Primitive" + ": " + b1);

        System.out.print("Is" + " " + cl2.getSimpleName() + " ");
        System.out.println("Primitive" + ": " + b2);
    }
}

Output

输出量

Is String Primitive: false
Is short Primitive: true


翻译自: https://www.includehelp.com/java/class-class-isprimitive-method-with-example.aspx

isprimitive

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java中深拷贝对象可以使用Java序列化来实现。具体实现步骤如下: 1. 将待拷贝对象序列化为字节数组; 2. 将字节数组反序列化为新的对象。 以下是一个简单的深拷贝对象工具: ``` import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class DeepCopyUtil { public static <T> T deepCopy(T obj) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(obj); oos.flush(); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return (T) ois.readObject(); } } ``` 使用方法: ``` MyObject obj1 = new MyObject(); MyObject obj2 = DeepCopyUtil.deepCopy(obj1); ``` 需要注意的是,MyObject必须实现Serializable接口才能被序列化。 ### 回答2: Java中实现深拷贝对象的工具可以通过以下步骤: 首先,创建一个公共的工具,在该中定义一个静态方法,用于实现深拷贝操作。 其次,在该方法中,首先判断被拷贝对象是否为null,若为null,则直接返回null;否则,创建一个新的对象,用于存储拷贝后的内容。 接着,遍历原始对象的属性值,并逐个复制到新对象中。需要注意的是,若属性为基本数据型,则直接复制,若属性为引用型,则递归调用深拷贝方法进行拷贝。 同时,需要注意的是,对于可能会被修改的属性,如集合、数组等,也需要进行深拷贝,以保证复制后的对象与原始对象完全独立。 最后,返回拷贝后的对象。 简单示例代码如下: ```java import java.util.ArrayList; import java.util.List; public class DeepCopyUtil { public static <T> T deepCopy(T object) { if (object == null) { return null; } T copy = null; try { // 创建新对象 copy = (T) object.getClass().newInstance(); // 遍历属性并复制 for (Field field : object.getClass().getDeclaredFields()) { field.setAccessible(true); Object fieldValue = field.get(object); if (fieldValue != null) { // 判断属性型 if (field.getType().isPrimitive()) { field.set(copy, fieldValue); } else if (field.getType() == String.class) { field.set(copy, fieldValue); } else { // 递归调用深拷贝方法 field.set(copy, deepCopy(fieldValue)); } } } } catch (Exception e) { e.printStackTrace(); } return copy; } public static void main(String[] args) { List<String> list1 = new ArrayList<>(); list1.add("item1"); list1.add("item2"); List<String> list2 = deepCopy(list1); list2.add("item3"); System.out.println("Original list: " + list1); System.out.println("Copied list: " + list2); } } ``` 以上代码演示了一个简单的深拷贝工具,实现了对ArrayList对象的深拷贝。在实际使用中,可以根据需要进行修改和优化。 ### 回答3: Java中实现深拷贝对象的工具有多种方式,以下是一种常用的实现方法: 1. 首先,需要为对象实现Serializable接口,以便能够通过序列化和反序列化进行深拷贝。 ```java public class MyClass implements Serializable { // 对象的属性 } ``` 2. 创建一个深拷贝工具,该工具的内部使用序列化和反序列化来实现对象的深拷贝。 ```java import java.io.*; public class DeepCopyUtil { public static <T> T deepCopy(T object) { T copy = null; try { // 将对象写入字节流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(object); oos.close(); // 从字节流中读取对象 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); copy = (T) ois.readObject(); ois.close(); } catch (Exception e) { e.printStackTrace(); } return copy; } } ``` 3. 使用深拷贝工具进行对象的深拷贝。 ```java public class Main { public static void main(String[] args) { MyClass original = new MyClass(); MyClass copy = DeepCopyUtil.deepCopy(original); // 此时copy是original的深拷贝对象,对copy的修改不会影响original } } ``` 总结:以上代码中,通过实现Serializable接口,在深拷贝工具中使用序列化和反序列化进行对象的拷贝,确保了生成的新对象与原对象完全独立。这样实现的深拷贝工具可以适用于大多数的Java对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值