This question already has answers here:
final variable in methods in Java [duplicate]
(5 answers)
Closed 7 years ago.
I've used final keyword with class, method, fieldsbut this the first i'm seeing something like this
final Customer c=new Customer();
could anyone help me to get what is the use of this?
it is constant reference - you cannot change its value it can be assigned only once when being defined
due to Wikipedia
an example:
final String string = "initial value";
string += " some new content"; //here compiler will raise an error due to you cannot change final value
Related
This question already has answers here:
How does the "final" keyword in Java work? (I can still modify an object.)
(20 answers)
What is the point of "final class" in Java?
(24 answers)
Closed 3 years ago.
Even though String is a final class we can change the value of it like:
String A = "hello";
And in next step:
A = "World";
Here A will be changed.
Whereas in case of a final variable we can't do it like:
final int a =10;
a = 13; //This Will Give Error
This would be a contradiction.
This is because when the class is final it means the methods of the class cannot be changed or overridden. If a field is final, then the value cannot be changed after the initial value has been assigned.
This question already has answers here:
Variable used in lambda expression should be final or effectively final
(9 answers)
Closed 4 years ago.
This question is already asked. But today I found something odd. For the following code:-
public static List<EsbBucketInstanceDefinition> convertBucketDefinitionList(List<BucketInstanceDefinitionV1> bucketInstanceDefinitionV1List) {
List<EsbBucketInstanceDefinition> response = new ArrayList<>();
List<EsbBucketInstanceDefinition> finalResponse = new ArrayList<>();
bucketInstanceDefinitionV1List.stream().forEach(e -> {
EsbBucketInstanceDefinition esbBucketInstanceDefinition = new EsbBucketInstanceDefinition();
esbBucketInstanceDefinition.setInstanceType(e.getInstanceType());
esbBucketInstanceDefinition.setReportingGroup(e.getReportingGroup());
esbBucketInstanceDefinition.setSliceVolume(e.getSliceVolume());
esbBucketInstanceDefinition.setCounterName(e.getCounterName());
esbBucketInstanceDefinition.setSubscriberGroupId(e.getSubscriberGroupId());
// response.add(esbBucketInstanceDefinition); compiler error variable used in lambda should be final or effective final
finalResponse.add(esbBucketInstanceDefinition);
});
return finalResponse;
}
For this works fine. Looks like only variable name finalResponse is working. How and why? Is it valid to do?
References may only be made to (effectively) final variables from within a lambda.
The reference held by finalResponse in effectively final, because it never changes. Note that changing the reference means assigning a new value to it, eg
finalResponse = someOtherList;
Changing the state of the object referred to (eg adding items to the list referred to by finalResponse) is irrelevant to what the value held by the variable finalResponse, ie
finalResponse.add(something);
Does not change the variable finalResponse; it only changes the object to which finalResponse refers.
This question already has answers here:
How many Java Strings Created?
(2 answers)
Questions about Java's String pool [duplicate]
(7 answers)
Closed 7 years ago.
The result of this code:
public class Test {
public static void main(String[] args) {
String s = "Java";
s.concat(" SE 6");
s.replace('6', '7');
System.out.print(s);
}
}
will be "Java"
Who can tell me how many instances of String will be created during execution?
String is immutable in Java. Though you are invoking methods on it, they returns a new string each time.
There are 4 instance created here in this case
Please follow the comments:
String s = "Java"; // 1
s.concat(" SE 6"); // 2 & 3 for concat method returns a new string and another literal created " SE 6"
s.replace('6', '7'); // 4 returns a new string instance which you are not receiving
System.out.print(s);
This question already has answers here:
Immutability of Strings in Java
(26 answers)
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
In java String is a class and it is imutable so we can not change its value.In following code it will concate other string without any error.So I want to ask that if it is immutable then why in this following code value of String is changed??
import java.util.*;
public class conc
{
public static void main(String args[])
{
String a="Sheetal";
a=a+"Ga";
System.out.println("Result:"+a);
}
}
In the code that you have shown, you have not changed the original String object.
Instead, you have created a new String object, which represents a + "Ga", and then re-assigned it to the reference variable a.
Note that all variables in Java other than primitive types are references.
You are creating a new object by concatenating two strings, that is: You are not changing the object referenced by a but assigning to that reference the value referencing to a new String object.
String a="Sheetal";
a=a+"Ga"; // now this is not the same object you are referring early
When you alter your a will create a new String.
Your original String is not change that's why we call String are immutable and new String will create in the heap.
In this moment there are 2 object in your heap. now a is referring new Object.
This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 8 years ago.
I am trying to create a method that contains a for loop that creates new objects, but I am stuck at how to assign a predetermined name to an object based on the loop's count. For instance it would be something like this:
private void createPictureObject(int count){
for(int a = 1; a <= count; a++){
Picture picture*a* = new Picture(arguments);
}
The result of this loop would be that I have objects named something like picture1, picture2, picture3, picture 4 etc. Is this even possible?
No You cannot do that! Dynamic variable naming is not allowed in Java. Use an array instead.
Picture[] pictures = new Picture[10];