This question already has answers here:
what is the sense of final ArrayList?
(13 answers)
Closed 5 years ago.
I have a question about final variables in java and I have written a short code to demonstrate the issue. The Language Specification of java8 states that:
Once a final variable has been assigned, it always contains the same
value. If a final variable holds a reference to an object, then the
state of the object may be changed by operations on the object, but
the variable will always refer to the same object. Language
Specification
To investigate further I have tried out three things in a small example code. The first thing I tried out was to create an object of the type Integer and assign non final reference to it. Then I assigned a final reference to it as well. In the second experiment I did the same with primitive int. Both experiments lead to the same result of me not being allowed by the compiler to increment the final reference, but being allowed to increment the non final reference and in the Output only the non final variable got incremented.
In my third experiment I used a List and assigned, again, a non final and a final reference to the List. Here I was allowed to use the final and the non final reference to call add() and in both references the size was updated.
My test-code:
public void testFunction () {
Integer nonFinalInteger = 4;
final Integer finalInteger = nonFinalInteger;
nonFinalInteger++;
//Compiler shows error
//finalInteger++;
System.out.println("nonFinal Integer: " + nonFinalInteger);
System.out.println("final Integer: " + finalInteger + "\n");
int nonFinalInt = 4;
final int finalInt = nonFinalInt;
nonFinalInt++;
//Compiler shows error
//finalInt++;
System.out.println("nonFinal primitive int: " + nonFinalInt);
System.out.println("final primitive int: " + finalInt + "\n");
List<String> nonFinalVar = new ArrayList<String>();
final List<String> finalVar = nonFinalVar;
finalVar.add("Hello");
//Compiler does not show error
nonFinalVar.add("World");
System.out.println("nonFinal List Size: " + nonFinalVar.size());
System.out.println("final List Size: " + finalVar.size() + "\n");
}
Output:
nonFinal Integer: 5
final Integer: 4
nonFinal primitive int: 5
final primitive int: 4
nonFinal List Size: 2
final List Size: 2
My question is now: Is there a way to protect the state of the object without having to change the code of its class? Lets say we stay in the context that there is a List, where you should not be allowed to add or delete elements. Is it possible to mark the List in a way, so that the compiler would show an error (or at least some kind of warning)?
Edit:
It seems I have caused some confusion with using arrays in the example. My question should refer to any kind of objects, hence I did not put in List in the question or the tags. What I meant was not to tell the compiler that updating the reference is forbidden, I just wanted to know if there was a way of implicitly telling the compiler to reject the calling of functions that may change the state of the object.
Currently the idea with the wrapper class seems to be the most appealing one, even though it still lacks one thing for me: It would not stop me or other people to change the state of the object completely. Final variables always have this "do not touch" trait I am looking here for.
This is a confusing matter. I will try to explain it:
When you declare a variable as final, the result is that his value can be assigned in two different forms:
Assigned the value in the same line that you define the final variable.
If the final variable B is a property of a class A, then the B's value can be assigned in the constructor of the A class.
But the important thing is, when I'm saying assigned a value I refer to the assignment in the form:
final ArrayList<Integet> myArray = new ArrayList<Integer>(Arrays.asList(1,2,3));
Now the variable myArray is always pointing to that arraylist and can't be assigned in the same form again:
myArray = new ArrayList<Integer>(Arrays.asList(4,5,6)); will throw an error.
But that being said, you can still make operations in that variable. For example, like you see, you could add values doing: myArray.add(4);.
In a few words: when you mark a variable C as final you are doing just that, saying that C can't be assigned to another object, but that target object can change his state at any time.
Related
I know that writing a lambda in Java 8 to use a variable requires final type, but why can a variable of final array type be modified?
public static void main(String[] args) {
final String[] prefix = {"prefix_"};
String suffix = "_suffix";
List<Integer> list = Arrays.asList(1005, 1006, 1007, 1009);
List<String> flagList = list.stream().map(param -> {
prefix[0] = "NoPrefix_";
String flag = prefix[0] + param + suffix;
return flag;
}).collect(Collectors.toList());
System.out.println(flagList);
System.out.println(prefix[0]);
}
result:
[NoPrefix_1005_suffix, NoPrefix_1006_suffix, NoPrefix_1007_suffix, NoPrefix_1009_suffix]
NoPrefix_
So a final array means that the array variable which is actually a reference to an object, cannot be changed to refer to anything else, but the members of the array can be modified
refer below link for more information.
https://www.geeksforgeeks.org/final-arrays-in-java/
As per description
for example
final String[] arr = new String[10];
list.stream().map(ele -> {
arr[0] = ele.getName(); // this will work as you are updating member of array
arr = new String[5]; // this will not work as you are changing whole array object instead of changing member of array object.
}).collect(Collectors.toList());
the same thing happen when you use any final collection there.
why can a variable of final array type be modified?
It can't, even though it looks like it can.
The declaration is below, which defines "prefix" as an array:
final String[] prefix
First, edit the code to include two println() calls right after the declaration, like this:
final String[] prefix = {"prefix_"};
System.out.println("prefix: " + prefix);
System.out.println(prefix[0]);
And at the end, add a second println() next to the one you already had, like this:
System.out.println("prefix: " + prefix);
System.out.println(prefix[0]);
If you run that code, you'll see that the object hashCode when printing prefix will be the same object each time. The thing that changes then is not what "prefix" references – that remains the same, it's the same array as before. Instead, what you're doing is changing something inside the array, which is different from the array itself.
Here are the relevant lines from a local run showing that the object reference remains the same, but the value for "prefix[0]" changes:
prefix: [Ljava.lang.String;#5ca881b5
prefix_
prefix: [Ljava.lang.String;#5ca881b5
NoPrefix_
If we try to assign an entirely new array to "prefix", then the compiler will show an error – it knows "prefix" was defined as final.
prefix = new String[]{"new"};
cannot assign a value to final variable prefix
If you're looking for a way to prevent changes to the data, and if it's possible to use a List (instead of an array), you could use
Collections.unmodifiableList():
Returns an unmodifiable view of the specified list. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.
This question already has answers here:
What is the difference between constant variables and final variables in java?
(4 answers)
Closed 2 years ago.
I'm a newbie learning Java currently being introduces do the switch/case construct. The book I'm reading uses the following example:
int eingabe = 256;
final byte einKleinesByte = 2;
final char einKleinerCharacter = 'c';
final short einKleinesShort = 500;
switch(eingabe) {
case einKleinesByte:
case einKleinerCharacter:
case einKleinesShort:
The explanation is that these 3 constant are defined with the help of the keyword final, which is essential, because variables cannot be used for the cases.
My question is, what is the difference between final byte einKleinesByte = 2; and byte EIN_KLEINES_BYTE = 2; as they are both defined as constants?
Thanks for the help!
The main difference between final variable and a constant (static and final) is that if you create a final variable without static keyword, though its value is un-modifiable, a separate copy of the variable is created each time you create a new object. Where a constant is un-modifiable and have only one copy through out the program.
They are not both defined as constant. You can easily change the value of the byte EIN_KLEINES_BYTE. You are using the snake uppercase standard which is usually used to define constant but in reallity, there is nothing that stops you from changing the value of this variable. Only the keyworld final would prevent that.
// would work fine
int MY_CONSTANT = 2;
MY_CONSTANT = 3
// Will throw an error
final int myConstant = 2;
myConstant = 3
// recommended
static final int MY_CONSTANT = 2
byte EIN_KLEINES_BYTE is not a constant, it can be overwritten.
final byte EIN_KLEINES_BYTE can't be, that is a constant.
a final variable can not be reassigned. For primitive types, or for unchangeable types, this means that they are constant in value.
For most objects, it doesn't. You can change the state (value) of those objects, but you can't change the object itself for another.
Just by putting a variable name in upper case, does not make it a constant.
EDIT: if you want a final variable to be the same for the entire application, you'll have to declare it as static, as well.
final simply means that the value, once assigned, can not be changed or reassigned
concerning constants, i would refer you to this question
I find the defs circular, the subjects are defined by their verbs but the verbs are undefined! So how do you define them?
The Circular Definitions
initialization: to initialize a variable. It can be done at the time of
declaration.
assignment: to assign value to a variable. It can be done anywhere, only once with the final-identifier.
declaration: to declare value to a variable.
[update, trying to understand the topic with lambda calc]
D(x type) = (λx.x is declared with type)
A(y D(x type)) = (λy.y is assigned to D(x type))
%Then after some beta reductions we get initialization.
D(x type) me human // "me" declared with type "human"
A(y (D(x type) me human)) asking // "asking" assigned to the last declaration
%if the last two statemets are valid, an initialization exists. Right?
assignment: throwing away the old value of a variable and replacing it with a new one
initialization: it's a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.
declaration: a declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.
String declaration;
String initialization = "initialization";
declaration = "initialization"; //late initialization - will initialize the variable.
// Without this, for example, in java, you will get a compile-time error if you try
// to use this variable.
declaration = "assignment"; // Normal assignment.
// Can be done any number of times for a non-final variable
Declaration is not to declare "value" to a variable; it's to declare the type of the variable.
Assignment is simply the storing of a value to a variable.
Initialization is the assignment of a value to a variable at the time of declaration.
These definitions also applies to fields.
int i; // simple declaration
i = 42 // simple assignment
int[] arr = { 1, 2, 3 };
// declaration with initialization, allows special shorthand syntax for arrays
arr = { 4, 5, 6 }; // doesn't compile, special initializer syntax invalid here
arr = new int[] { 4, 5, 6 }; // simple assignment, compiles fine
However, it should be mentioned that "initialization" also has a more relaxed definition of "the first assignment to a variable", regardless of where it happens.
int i; // local variable declaration
if (something) i = 42;
System.out.println(i);
// compile time error: The local variable i may not have been initialized
This, however, compiles:
int i; // the following also compiles if i were declared final
if (something) i = 42;
else i = 666;
System.out.println(i);
Here i can be "initialized" from two possible locations, by simple assignments. Because of that, if i was an array, you can't use the special array initializer shorthand syntax with this construct.
So basically "initialization" has two possible definitions, depending on context:
In its narrowest form, it's when an assignment is comboed with declaration.
It allows, among other things, special array shorthand initializer syntax
More generally, it's when an assignment is first made to a variable.
It allows, among other things, assignments to a final variable at multiple places.
The compiler would do its best to ensure that exactly one of those assignments can happen, thus "initializing" the final variable
There's also JVM-context class and instance initialization, OOP-context object initialization, etc.
Here is a short explanation with some examples.
Declaration:
Declaration is when you declare a variable with a name, and a variable can be declared only once.
Example: int x;, String myName;, Boolean myCondition;
Initialization:
Initialization is when we put a value in a variable, this happens while we declare a variable.
Example: int x = 7;, String myName = "Emi";, Boolean myCondition = false;
Assignment:
Assignment is when we already declared or initialized a variable, and we are changing the value. You can change value of the variable as many time you want or you need.
Example:
int x = 7;
x = 12; .......We just changed the value.
String myName = "Emi";
myName = "John" .......We just changed the value.
Boolean myCondition = false;
myCondition = true; .......We just changed the value.
Note: In memory will be saved the last value that we put.
declaration: whenever you define a new variable with its type
assignment: whenever you change the value of a variable by giving it a new value
initialization: an assignment that is done together with the declaration, or in any case the first assignment that is done with a variable, usually it's a constructor call for an object or a plain assignment for a variable
I come from a C/C++ background, but the ideas should be the same.
Declaration - When a variable is declared, it is telling the compiler to set aside a piece of memory and associate a name (and a variable type) with it. In C/C++ it could look like this:
int x;
The compiler sees this and sets aside an address location for x and knows what methods it should use to perform operations on x (different variable types will use different access operations). This way, when the compiler runs into the line
x = 3 + 5;
It knows to put the integer value 8 (not the floating point value 8) into the memory location also known as 'x'.
Assignment - This is when you stuff a value into the previously declared variable. Assignment is associated with the 'equals sign'. In the previous example, the variable 'x' was assigned the value 8.
Initialization - This is when a variable is preset with a value. There is no guarantee that a variable will every be set to some default value during variable declaration (unless you explicitly make it so). It can be argued that initialization is the first assignment of a variable, but this isn't entirely true, as I will explain shortly. A typical initialization is a blend of the variable declaration with an assignment as follows:
int x = 6;
The distinction between initialization and assignment becomes more important when dealing with constants, such as this...
const int c = 15;
When dealing with constants, you only get to assign their value at the time of declaration/initialization. Otherwise, they can't be touched. This is because constants are often located in program memory vs data memory, and their actual assignment is occurring at compile time vs run time.
Step 1: Declaration : int a;
Step 2: Initialization : a = 5;
Step 3: Assignment: a = b; (ex: int b = 10 ; now a becomes 10)
Declaration
When we first create a variable of any Data Type is call Declaration. Example:
int obj;
String str;
Initialization
When (Declaration is completed) or variable is created the assigned any value to this variable is call Initialization. Example
int obj = 10;
String str = "Azam Khan";
Assignment
Reassign value to that variable that is already initialized is call Assignment. Example
int obj = 15;
obj = 20; // it print **20** value.
String str = "simple"
str = "Azam Khan" // it's result or print **Azam Khan** value...
Simple program
class ABC{
public static void main(String args[]){
int obj; // this is **Declaration:**
int testValue= 12; // After the **Declaration**,
testValue is **Initialize**.
testValue = 25;
System.out.println(textValue);
}
}
the output result is 25.
it saves the value of 25.
I was reading earlier that wrapper classes are all immutable. Immutable means that the value cannot be changed. Below I tried this simple example that can just be pasted in to any main method. first I create a Integer that wraps the value five. Immutable means that they cannot be changed so why can I set I to 89. I think that it is because it changes where (I) points to but I am not certain why this is the case.
In my next little example i create an Integer of x which will throw an error if I try and change it. The x seems to be immutable in this specific case but not in the case of the (i) variable.
It seems that I can change the value of (i) whenever I want to so in reality Integer without the final keyword is not immutable???? If i can be set to 89 then to me this seems that the variable can be changed.
I have read other post on this and I still am not certain why i can be changed to another variable. Also in writing code what is the best way to declare primitive types. Why not use the wrapper classes all of the time to create variables.
int y = 5;
Integer i = new Integer(y);
i = 89;
final Integer x = Integer.valueOf(5);
System.out.println("Integer:(i) " + i.intValue());
System.out.println("Integer:(i) " + i.byteValue());
System.out.println("Integer:(x) " + x.intValue());;
System.out.println("Integer:(x) " + x.byteValue());;
i = i - 5;
Using all wrapper classes to declare variables: (Would this be better than declaring these variable with the primitive variable types)
Integer a = new integer(MyNewValue);
Integer b = new integer(MyNewValue);
Integer c = new integer(MyNewValue);
Integer d = new integer(MyNewValue);
Float fa = new integer(MyNewValue);
You are conflating two things: changing the value of an "object" itself and changing the object a reference points to. Saying i = 89 just points the variable i to a new object; it doesn't change the Integer object which originally was pointed to by i.
Pre-pending variable declarations with final just ensures that reassigned is prohibited, it is in no way a declaration of the mutability/immutability of the object it points to. Maybe off-topic, but I personally think the article Java is Pass-by-Value, Dammit! is a good read.
When you call i = 89;, your not changing the value of the Integer object stored in memory. Instead, you're assigning a brand new int with value 89 to i. So the immutable rule isn't being broken.
Remember that i is simply a reference that points to the Integer, not the actual Integer itself.
Yes, it does look like the integer is changing, but all that is happening on line 3 is its being converted to i = new Integer(89) by the compiler. If you wanted to see, you could do
Integer i1 = i;
i = 83;
println(i); \\ prints out the original value 5
println(i1); \\ prints out a new value, 83
When you declare something as final, you cannot change the definition of the variable, though you can still mutate anything inside it. JavaRanch has a very nice analogy to help
You should not use wrapper objects when you can avoid it because they are a small amount less efficient to than primitives and take up a few extra bytes.
I find the defs circular, the subjects are defined by their verbs but the verbs are undefined! So how do you define them?
The Circular Definitions
initialization: to initialize a variable. It can be done at the time of
declaration.
assignment: to assign value to a variable. It can be done anywhere, only once with the final-identifier.
declaration: to declare value to a variable.
[update, trying to understand the topic with lambda calc]
D(x type) = (λx.x is declared with type)
A(y D(x type)) = (λy.y is assigned to D(x type))
%Then after some beta reductions we get initialization.
D(x type) me human // "me" declared with type "human"
A(y (D(x type) me human)) asking // "asking" assigned to the last declaration
%if the last two statemets are valid, an initialization exists. Right?
assignment: throwing away the old value of a variable and replacing it with a new one
initialization: it's a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.
declaration: a declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.
String declaration;
String initialization = "initialization";
declaration = "initialization"; //late initialization - will initialize the variable.
// Without this, for example, in java, you will get a compile-time error if you try
// to use this variable.
declaration = "assignment"; // Normal assignment.
// Can be done any number of times for a non-final variable
Declaration is not to declare "value" to a variable; it's to declare the type of the variable.
Assignment is simply the storing of a value to a variable.
Initialization is the assignment of a value to a variable at the time of declaration.
These definitions also applies to fields.
int i; // simple declaration
i = 42 // simple assignment
int[] arr = { 1, 2, 3 };
// declaration with initialization, allows special shorthand syntax for arrays
arr = { 4, 5, 6 }; // doesn't compile, special initializer syntax invalid here
arr = new int[] { 4, 5, 6 }; // simple assignment, compiles fine
However, it should be mentioned that "initialization" also has a more relaxed definition of "the first assignment to a variable", regardless of where it happens.
int i; // local variable declaration
if (something) i = 42;
System.out.println(i);
// compile time error: The local variable i may not have been initialized
This, however, compiles:
int i; // the following also compiles if i were declared final
if (something) i = 42;
else i = 666;
System.out.println(i);
Here i can be "initialized" from two possible locations, by simple assignments. Because of that, if i was an array, you can't use the special array initializer shorthand syntax with this construct.
So basically "initialization" has two possible definitions, depending on context:
In its narrowest form, it's when an assignment is comboed with declaration.
It allows, among other things, special array shorthand initializer syntax
More generally, it's when an assignment is first made to a variable.
It allows, among other things, assignments to a final variable at multiple places.
The compiler would do its best to ensure that exactly one of those assignments can happen, thus "initializing" the final variable
There's also JVM-context class and instance initialization, OOP-context object initialization, etc.
Here is a short explanation with some examples.
Declaration:
Declaration is when you declare a variable with a name, and a variable can be declared only once.
Example: int x;, String myName;, Boolean myCondition;
Initialization:
Initialization is when we put a value in a variable, this happens while we declare a variable.
Example: int x = 7;, String myName = "Emi";, Boolean myCondition = false;
Assignment:
Assignment is when we already declared or initialized a variable, and we are changing the value. You can change value of the variable as many time you want or you need.
Example:
int x = 7;
x = 12; .......We just changed the value.
String myName = "Emi";
myName = "John" .......We just changed the value.
Boolean myCondition = false;
myCondition = true; .......We just changed the value.
Note: In memory will be saved the last value that we put.
declaration: whenever you define a new variable with its type
assignment: whenever you change the value of a variable by giving it a new value
initialization: an assignment that is done together with the declaration, or in any case the first assignment that is done with a variable, usually it's a constructor call for an object or a plain assignment for a variable
I come from a C/C++ background, but the ideas should be the same.
Declaration - When a variable is declared, it is telling the compiler to set aside a piece of memory and associate a name (and a variable type) with it. In C/C++ it could look like this:
int x;
The compiler sees this and sets aside an address location for x and knows what methods it should use to perform operations on x (different variable types will use different access operations). This way, when the compiler runs into the line
x = 3 + 5;
It knows to put the integer value 8 (not the floating point value 8) into the memory location also known as 'x'.
Assignment - This is when you stuff a value into the previously declared variable. Assignment is associated with the 'equals sign'. In the previous example, the variable 'x' was assigned the value 8.
Initialization - This is when a variable is preset with a value. There is no guarantee that a variable will every be set to some default value during variable declaration (unless you explicitly make it so). It can be argued that initialization is the first assignment of a variable, but this isn't entirely true, as I will explain shortly. A typical initialization is a blend of the variable declaration with an assignment as follows:
int x = 6;
The distinction between initialization and assignment becomes more important when dealing with constants, such as this...
const int c = 15;
When dealing with constants, you only get to assign their value at the time of declaration/initialization. Otherwise, they can't be touched. This is because constants are often located in program memory vs data memory, and their actual assignment is occurring at compile time vs run time.
Step 1: Declaration : int a;
Step 2: Initialization : a = 5;
Step 3: Assignment: a = b; (ex: int b = 10 ; now a becomes 10)
Declaration
When we first create a variable of any Data Type is call Declaration. Example:
int obj;
String str;
Initialization
When (Declaration is completed) or variable is created the assigned any value to this variable is call Initialization. Example
int obj = 10;
String str = "Azam Khan";
Assignment
Reassign value to that variable that is already initialized is call Assignment. Example
int obj = 15;
obj = 20; // it print **20** value.
String str = "simple"
str = "Azam Khan" // it's result or print **Azam Khan** value...
Simple program
class ABC{
public static void main(String args[]){
int obj; // this is **Declaration:**
int testValue= 12; // After the **Declaration**,
testValue is **Initialize**.
testValue = 25;
System.out.println(textValue);
}
}
the output result is 25.
it saves the value of 25.