final

Java8 - “effectively final”

匿名 (未验证) 提交于 2019-12-03 01:22:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using RxVertx which is a sort of RxJava along with Java8 and I have a compilation error. Here is my code: public rx.Observable<Game> findGame(long templateId, GameModelType game_model, GameStateType state) { return context.findGame(templateId, state) .flatMap(new Func1<RxMessage<byte[]>, rx.Observable<Game>>() { @Override public Observable<Game> call(RxMessage<byte[]> gameRawReply) { Game game = null; switch(game_model) { case SINGLE: { ebs.subscribe(new Action1<RxMessage<byte[]>>() { @Override public void call(RxMessage<byte[]> t1) { if

Does it make sense to define a final String in Java? [duplicate]

偶尔善良 提交于 2019-12-03 00:57:33
Possible Duplicate: String and Final From http://docs.oracle.com/javase/6/docs/api/java/lang/String.html I can read that: Strings are constant; their values cannot be changed after they are created. Does this mean that a final String does not really make sense in Java, in the sense that the final attribute is somehow redundant? The String object is immutable but what it is is actually a reference to a String object which could be changed. For example: String someString = "Lala"; You can reassign the value held by this variable (to make it reference a different string): someString = "asdf";

string用final修饰的原因

匿名 (未验证) 提交于 2019-12-03 00:03:02
通过String源码可以看到,String类型的底层是由final修饰的char数组存储。 ? 1 2 3 4 5 6 7 public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[]; ........ } String能被设计成不可变类型的一个重要前是因为它是编程语言里面使用频率最高的一种类型。不可变类型带来的好处,体现在四个方面,分别是:缓存,安全,同步和性能。 (一)缓存 在JVM的运行时数据区域里面,有一个专门的字符串常量池用来存储字符串字面量,如下面一段代码: ? 1 2 3 4 String s1 = "Hello World" ; String s2 = "Hello World" ; assertThat(s1 == s2).isTrue(); s1和s2变量指针的内存地址其实是一样的,也就是说他们代表是同一个对象,这是jvm常量池做的优化,当第一个字面量声明的时候,它的值会被字符串常量池存储,当s2变量声明的时候,jvm发现常量池已经存在该对象,所以就不会再创建一次

Are final fields really useful regarding thread-safety?

戏子无情 提交于 2019-12-02 22:23:58
I have been working on a daily basis with the Java Memory Model for some years now. I think I have a good understanding about the concept of data races and the different ways to avoid them (e.g, synchronized blocks, volatile variables, etc). However, there's still something that I don't think I fully understand about the memory model, which is the way that final fields of classes are supposed to be thread safe without any further synchronization. So according to the specification, if an object is properly initialized (that is, no reference to the object escapes in its constructor in such a way

java中的final对重排序的限制与逸出

匿名 (未验证) 提交于 2019-12-02 21:53:52
在java中,为了保证final域的正确使用,对重排序进行了一些限制 1.在构造函数内对一个final域的写入,与随后把这个被构造对象的引用赋值给一个引用 变量,这两个操作之间不能重排序 2.初次读一个包含final域的对象的引用,与随后初次读这个final域,这两个操作之间不能 重排序 这个两句是可能被重排的,可能B执行reader并不能读到i的初始值 文章来源: java中的final对重排序的限制与逸出

Static Final Variable in Java [duplicate]

≯℡__Kan透↙ 提交于 2019-12-02 21:47:42
This question already has answers here : private final static attribute vs private final attribute (21 answers) Possible Duplicate: private final static attribute vs private final attribute What's the difference between declaring a variable as static final int x = 5; or final int x = 5; If I only want to the variable to be local, and constant (cannot be changed later)? Thanks Just having final will have the intended effect. final int x = 5; ... x = 10; // this will cause a compilation error because x is final Declaring static is making it a class variable, making it accessible using the class

How do I initialize a final class property in a constructor?

天大地大妈咪最大 提交于 2019-12-02 21:40:10
In Java you are allowed to do this: class A { private final int x; public A() { x = 5; } } In Dart, I tried: class A { final int x; A() { this.x = 5; } } I get two compilation errors: The final variable 'x' must be initialized. and 'x' can't be used as a setter because its final. Is there a way to set final properties in the constructor in Dart? w.brian You cannot instantiate final fields in the constructor body. There is a special syntax for that: class Point { final num x; final num y; final num distanceFromOrigin; // Old syntax // Point(x, y) : // x = x, // y = y, // distanceFromOrigin =

final关键字

牧云@^-^@ 提交于 2019-12-02 21:34:54
1、final的作用 ①修饰类,该类不能被继承。String、System都是用final修饰的类 ②修饰方法,该方法不能被重写。父类中方法只能被子类使用但不能重写时可以用final修饰 ③final和abstract不能共存 ④final修饰变量,即常量,只能赋值一次 package com.wang.duixiang; public class FinalDemo01 { public static void main(String[] args) { Employee employee=new Coder(); employee.showFinal(); employee.show(); System.out.println("============================================="); //final修饰的变量:基本类型的变量,值不能改变 final int NUM=20; System.out.println(NUM); //NUM=30;因为NUM是常量,所以值只能设置一次,不可更改 System.out.println("============================================="); //final修饰的变量:一个引用类型的变量,地址值不能改变,属性值可以发生变化 final Employee

Why does C# not allow const and static on the same line?

老子叫甜甜 提交于 2019-12-02 20:25:12
Why does C# not allow const and static on the same line? In Java, you must declare a field as 'static' and 'final' to act as a constant. Why does C# not let you declare const's as final? I make the further distinction that in Java, every interface is public and abstract, whether this is explicitly declared or not. Aren't const's effectively static in nature? WHy does C# balk at this? const and static really do mean different things, different storage mechanism, different initialisation. static is read/write, therefore must have memory allocated for storage and must be initialised at runtime. A