`

Java关键字之native,strict fp,transient,volatile

    博客分类:
  • JAVA
阅读更多
abstract    default    if            private      this 
boolean     do         implements    protected    throw 
break       double     import        public       throws 
byte        else       instanceof    return       transient 
case        extends    int           short        try 
catch       final      interface     static       void 
char        finally    long          strictfp     volatile 
class       float      native        super        while 
const       for        new           switch 
continue    goto       package       synchronized


以上是java specifications中定义的keywords,一共48个,其中常见的三个看似是关键字的true, false, null,都不是关键字,而是作为一个单独标识类型。
其中,不常用到的关键字有:const,goto,native,strictfp,transient,volatile。
const和goto为java中的保留字。
1. native
native是方法修饰符。Native方法是由另外一种语言(如c/c++,FORTRAN,汇编)实现的本地方法。因为在外部实现了方法,所以在java代码中,就不需要声明了,有点类似于借口方法。Native可以和其他一些修饰符连用,但是abstract方法和Interface方法不能用native来修饰。
Example:
Java代码
public interface TestInterface {  
     void doMethod();  
}  
public class Test implements TestInterface {  
    public native void doMethod();  
    private native int doMethodB();  
  public native synchronized String doMethodC();  
  static native void doMethodD();  
}  


为什么需要使用native method?请参考:
http://www.iteye.com/topic/72543  java Native Method初涉
2. strictfp
修饰类和方法,意思是FP-strict,精确浮点,符合IEEE-754规范的。当一个class或interface用strictfp声明,内部所有的float和double表达式都会成为strictfp的。Interface method不能被声明为strictfp的,class的可以。
Example:
Java代码
strictfp interface FPTest {  
     void methodA();  
}  
class FPClass implements FPTest {  
    public void methodA() {  
    }  
    public void methodB() {  
  }  
  public strictfp void methodC() {  
  }  
}  
class FPClassB {  
    strictfp void methodA() {  
    }  
} 


3.transient
变量修饰符。标记为transient的变量,在对象存储时,这些变量状态不会被持久化。当对象序列化的保存在存储器上时,不希望有些字段数据被保存,为了保证安全性,可以把这些字段声明为transient。
4. volatile
volatile修饰变量。在每次被线程访问时,都强迫从共享内存中重读该成员变量的值。而且,当成员变量发生变化时,强迫线程将变化值回写到共享内存。这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。
看看Java Language Specification中的例子。
条件:一个线程不停的调用方法one(),一个线程不停的调用方法two()。我测试过多次,这种情况好像一直没有出现。
Java代码
class Test {  
    static int i = 0, j = 0;  
    static void one() { i++; j++; }  
    static void two() {  
        System.out.println("i=" + i + " j=" + j);  
    }  
} 


结果偶尔会出现j大于i的情况,因为方法没有同步,所以会出现i和j可能不是一次更新。一种防止这种情况发生的办法就是声明两个方法为synchronized 的。
Java代码
class Test {  
    static int i = 0, j = 0;  
    static synchronized void one() { i++; j++; }  
    static synchronized void two() {  
        System.out.println("i=" + i + " j=" + j);  
    }  
}  


这样可以防止两个方法同时被执行,还可以保证j和i被同时更新,这样一来i和j的值一直是一样的。
另外一种途径就是把i和j声明为volatile。
Java代码
class Test {  
    static volatile int i = 0, j = 0;  
    static void one() { i++; j++; }  
    static void two() {  
        System.out.println("i=" + i + " j=" + j);  
    }  
}  
分享到:
评论

相关推荐

    Referrer-Policy : strict-origin-when-cross-origin解决方案

    前后端联调跨域问题

    ( xhtml1-strict.dtd

    ( xhtml1-strict.dtd ( xhtml1-strict.dtd ( xhtml1-strict.dtd ( xhtml1-strict.dtd ( xhtml1-strict.dtd ( xhtml1-strict.dtd ( xhtml1-strict.dtd ( xhtml1-strict.dtd ( xhtml1-strict.dtd ( xhtml1-strict.dtd ...

    JavaScript的strict模式与with关键字介绍

    主要介绍了JavaScript的strict模式与with关键字,需要的朋友可以参考下

    JAVA白皮书(英文版)

    1. Introduction to Java 1.1Beginnings of the Java Language Project 1.2Design Goals of Java 1.2.1Simple, Object Oriented, and Familiar 1.2.2Robust and Secure 1.2.3Architecture Neutral and ...

    OOXML Strict Converter for Office 2010

    OOXML Strict Converter for Office 2010 allows you to open ISO strict documents that are created using Office 2013 in Office 2010. It will preserve the fidelity of the document. If you make any changes...

    Javascript 严格模式use strict详解

    在js文件的最前面添加 “use strict” 2.局部 在函数内部添加 “use strict”,如下 function fn() { use strict; //some code } 二、严格模式和非严格模式的对比 1.严格模式下,无法删除(delete)变量(delete是...

    前端开源库-strict-mode

    前端开源库-strict-mode严格模式,在包中启用严格模式

    前端开源库-use-strict

    前端开源库-use-strict使用strict,使节点中的所有模块都以strict模式加载。

    phpdocument Strict Standards 错误解决办法

    phpdocument 错误: ...Strict Standards:Only variables should be passed by reference in D:\xampp\php\pear\PhpDocumentor\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

    react-native-qrcode, 用于响应本机的极简qrcode组件.zip

    react-native-qrcode, 用于响应本机的极简qrcode组件 react-native-qrcode产生 QRcode的一个反应本机组件,不仅支持英语。安装npm install react-native-qrcode --save用法'use strict';impo

    开启PHP Static 关键字之旅模式

    用::方式调用一个非静态方法会导致一个E_STRICT级别的错误。 就像其它所有的PHP静态变量一样,静态属性只能被初始化为一个字符值或一个常量,不能使用表达式。 所以你可以把静态属性初始化为整型或数组,但不能指向...

    stylelint-declaration-strict-value:指定必须使用变量,函数,关键字或值的属性

    stylelint声明限制值 一个插件,可强制执行以下变量( $sass , namespace.$sass , @less , var(--cssnext) , css-loader @value ),函数或自定义CSS值,例如关键字( inherit , none , currentColor等) ,...

    Laravel开发-strict-transport-security

    Laravel开发-strict-transport-security 设置hsts头以启用HTTP严格传输安全性

    chart java applet

    THE FORM OF ACTION, WHETHER IN CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT PRODUCT LIABILITY OR OTHERWISE, EVEN IF ANY REPRESENTATIVE OF J4L COMPONENTS OR ITS SUPPLIERS HAS BEEN ADVISED OF THE ...

    jQuery过滤HTML标签并高亮显示关键字的方法

    jQuery实现网页关键字过滤效果,将需要过滤的文字定义在JavaScript中,可定义多个,不过要修改JS代码为数组,这样可过滤出多个不同的关键字,本例只是为你演示一个基本的功能,更多过滤功能请自己挖掘吧。...

    JAVA反编译软件

    反编译软件 ...IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    react-native-swipe手势:reactive-native的4向滑动手势

    'use strict' ; import React , { Component } from 'react' ; import { View , Text } from 'react-native' ; import GestureRecognizer , { swipeDirections } from 'react-native-swipe-gestures' ; class Some...

    用于圆形进度条的纯 React Native 组件

    React-Native-CircularProgress 一个纯 React Native 组件,用于 iOS 的循环进度条。 演示安装安装库 npm i --save react-native-progress-circular 包含在你的项目中 var CircularProgressDisplay = require('react...

    java web 编程技术

    java web 编程技术案例代码 Java Web 编程技术 沈泽刚 秦玉平 主编 清华大学出版社 20103 作者E-mail地址:shenzegang@tom.com shenzegang@126.com 电 话:13050451166 单 位:渤海大学信息科学与工程学院 ...

Global site tag (gtag.js) - Google Analytics