📌  相关文章
📜  Java中的修饰符 isVolatile(mod) 方法及示例

📅  最后修改于: 2022-05-13 01:55:38.987000             🧑  作者: Mango

Java中的修饰符 isVolatile(mod) 方法及示例

Java.lang.reflect.ModifierisVolatile(mod)方法用于检查整数参数是否包含 volatile 修饰符。如果此整数参数表示 volatile 类型修饰符,则方法返回 true,否则返回 false。

句法:

public static boolean isVolatile(int mod)

参数:此方法接受整数 mod 作为参数,表示一组修饰符。

Return :如果 mod 包含 volatile 修饰符,此方法返回 true;否则为假。

下面的程序说明了 isVolatile() 方法:
方案一:

// Java program to illustrate isVolatile() method
  
import java.lang.reflect.*;
  
public class GFG {
  
    volatile static String field1 = "GeeksForGeeks";
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // get Field class object
        Field field
            = GFG
                  .class
                  .getDeclaredField("field1");
  
        // get Modifier Integer value
        int mod = field.getModifiers();
  
        // check Modifier is volatile or not
        boolean result
            = Modifier.isVolatile(mod);
  
        System.out.println("Mod integer value "
                           + mod
                           + " is volatile : "
                           + result);
    }
}
输出:
Mod integer value 72 is volatile : true

方案二:

// Java program to illustrate isVolatile()
  
import java.lang.reflect.*;
  
public class GFG {
  
    volatile Long number
        = 139892824314253L;
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // get Field class object
        Field field
            = GFG
                  .class
                  .getDeclaredField("number");
  
        // get Modifier Integer value
        int mod = field.getModifiers();
  
        // check Modifier is volatile or not
        boolean result
            = Modifier.isVolatile(mod);
  
        System.out.println("Mod integer value "
                           + mod
                           + " is volatile : "
                           + result);
    }
}
输出:
Mod integer value 64 is volatile : true

参考资料: https: Java/lang/reflect/Modifier.html#isVolatile(int)