class MyData{
volatile int number = 0;
public void addTo60(){
this.number = 60;
}
}
class Person{
String name = "";
String [] measurement = new String [] {"A","B","C"};
public String getPersonName() {
return name;
}
}
class Student extends Person{
int studentId = 0;
public void showStudentId() {
System.out.println("StudentId is " + studentId);
}
}
public class VolatileDemo {
public static void main(String[] args) {
Student xiaoMing = (Student) new Person();
if (xiaoMing instanceof Person) {
System.out.println(xiaoMing.getPersonName());
}else
{
System.out.println("error in classCastException.");
}
xiaoMing.getPersonName();
int stepInt = 3;
stepInt = stepInt << 4;
System.out.println(">> " + stepInt);
stepInt = stepInt >> 2;
System.out.println(">> " + stepInt);
}
}
本文深入探讨了Java内存模型中的Volatile关键字使用,通过具体代码示例展示了Volatile如何确保变量的可见性和一致性。同时,文章还涉及了类继承、位运算等Java核心概念。
2万+

被折叠的 条评论
为什么被折叠?



