final data of final keyword

final data

Many programming languages ​​have their own way of telling the compiler that some data is "constant". Constants are mainly used in the following two aspects:

    (1) A compile-time constant, which never changes

    (2) A value initialized at runtime, we do not want it to change

    For compile-time constants, the compiler (program) can "encapsulate" the constant value into the required calculation process. That is, computations can be performed ahead of time during compilation, saving some overhead at runtime. In java, these forms of constants must belong to the basic data type (Primitives), and use the final keyword to express. When defining such a constant, a value must be given .

    Regardless of static or final fields, only one data can be stored and must not be changed.

    If final is used with object handles, rather than primitive data types, its meaning is slightly confusing. For primitive data types, final turns the value into a constant ; but for object handles, final turns the handle into a constant . When making a declaration, the handle must be initialized to a specific object (that is, only one person is loved, and the white head is not separated). And the handle can never be turned into another object (can't like other women). However, the objects themselves are modifiable (they can have other people change their hairstyles). Java does not provide any means for this to directly turn an object into a constant (however, we can write a class ourselves where objects have a "constant" effect). This restriction also applies to arrays , which are also objects.

class Value{
	int i = 1;
}

public class FinalDemo {
	//可以在编译时常量
	final int i1 = 9;
	static final int I2 = 99;
	
	//典型的公共常量:
	public static final int I3 = 39;
	
	//不能是编译时常量:
	final int i4 = (int)(Math.random()*20);
	static final int i5 = (int)(Math.random()*20); //注意i5 在 编译期间是未知的,所以它没有大写。
	
	Value v1 = new Value();
	final Value v2 = new Value();
	static final Value V3 = new Value();
	//! final Value v4; // Pre-Java 1.1 Error:
	// no initializer
	
	//Arrays;
	final int[] a = {1,2,3,4,5,6};
	
	public void print(String id){
		System.out.println(
		id+":"+"i4:"+i4+",i5="+i5);		
	}
	
	public static void main(String[] args) {
		FinalDemo fd1 = new FinalDemo();
		//!fd1.i1++;	//不能改成的值
		//fd1.v2.i++;		//Object isn't constant 不是常数!
		fd1.v1 = new Value(); //ok not final
		//!fd1.V3 = new Value();  fd1.v2 = new Value();  change handle
		fd1.V3.i++;
		//System.out.println(fd1.V3.i); //i可以改成
		
		//! fd1.a = new int[3];
		fd1.a[0]=5;//值可变,对象不可变
		System.out.print("int [] a=");
		for (int i = 0; i < fd1.a.length; i++) {
			if(fd1.a.length-i==1){
				System.out.println(fd1.a[i]+"]");
			}else if(i==0){
				System.out.print("["+fd1.a[i]+",");
			}else{
				System.out.print(fd1.a[i]+",");
			}
		}
		
		
		fd1.print("fd1");
		
		//创建新的FinalDemo对象
		FinalDemo fd2 = new FinalDemo();
		fd1.print("fd1"); //fd1:i4:16,i5=18 
		fd2.print("fd2"); //fd2:i4:3,i5=18 
		/**
		 * 注意对于fd1和 fd2来说,i4的值是唯一的,但 i5的值不会由于创建了另一个FinalData 对象而发生改
		变。那是因为它的属性是static,而且在载入时初始化,而非每创建一个对象时初始化。
		 * */
		
		System.out.println("i5:"+FinalDemo.i5);
	}
}

Output result:

int [] a=[5,2,3,4,5,6]
fd1:i4:18,i5=11
fd1:i4:18,i5=11
fd2:i4:17,i5=11
i5:11

Since i1 and I2 are both primitive data types with final attributes and contain compile-time values, they will not appear any different in any import method except that they can be used as compile-time constants. I3 is a more typical way we experience the definition of such constants:

    (1) public means that it can be used outside the package;   

    (2) static emphasizes that there is only one of them;

    (3) final indicates that it is a constant.

    Note: Final static primitive data types with fixed initialization values ​​(i.e. compile-time constants) must have their names in all uppercase according to the rules .

/**
 * 男
 */
public static final byte SEX_MALE = 1;

    Just because something is final doesn't mean its value is known at compile time. The i4 and i5 prove it to everyone. They use randomly generated numbers during runtime. This part of the example also shows you the difference between making a final value static and not static. This difference is only explained if the value is initialized at runtime. Because the value during compilation is considered the same by the compiler. This difference can be seen in the output:

fd1:i4:18,i5=11
fd2:i4:17,i5=11

    Note that the value of i4 is unique for fd1 and fd2, but the value of i5 does not change due to the creation of another FinalDemo object. That's because its properties are static and initialized at load time, not every time an object is created.

    The variables from v1 to v4 reveal to us the meaning of the final handle. As you can see in main(), it cannot be assumed that since v2 is final, it cannot be changed any more. However, we really can no longer bind v2 to a new object because its properties are final. The exact meaning of here and final for a handle. We'll find that the same meaning applies to arrays, which are just another type of handle. Making a handle final may seem less useful than making a primitive data type final.

    Excerpted from: "The Fourth Edition of Java Programming Ideas"

Java's problem with modifying the value of an Integer variable?

Excerpted from Zhihu:

java中Integer传参是无法改变原值的,如
Integer i = new Integer(-1);
void chang(Integer i){
  i = 1;
}
甚至在chang函数中new Integer(1)都无法改变 i 的值,如i = new Integer(1);
那么问题是:为什么可以改变Object中的Integer属性呢?如:
public class ObjectName{
  private Integer id;
  public ObjectName(Integer id){
    this.id = id;
  }
  public void setId(Integer id){
    this.id = id;
  }
}
ObjectName实例调用setId函数是可以改变Integer id的,这是为什么?

值传递与引用比较清楚。Integer不可变的,在java官方中明确指出了,所以有i = new Integer(1)都无法改变i原来的值。
我的问题就在这里:Integer变量既然是不可变的,那为什么用Object封装后就可变了呢?

Best answer:

这个可以看下Integer类的源码,因为Integer类中的value值是final的。
/**
 * The value of the {@code Integer}.
 *
 * @serial
 */
private final int value;
更改一个Integer对象的值,会新创建一个Integer对象的。

作者:向南
链接:https://www.zhihu.com/question/34904653/answer/129044196
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324968386&siteId=291194637