Difference between "final" and "const"? [duplicate] - java

This question already has answers here:
Java's final vs. C++'s const
(11 answers)
Closed 6 years ago.
I know that Java uses "final" to declare a constant and that c uses "const". Just wondering what the differences are between the two.

In java, making something final means that it can't be reasigned to another reference to another instance, but if it's a reference to a mutable class, the mutable values inside the class can still be modified.
For example, a final String is a constant because Strings are immutable in Java, but a final ArrayList means that you cannot assign it to another ArrayList, but you can still add and remove elements to that ArrayList

Related

Java is updating a variable when I don't want it to [duplicate]

This question already has answers here:
Problem with assigning an array to other array in Java
(4 answers)
Array assignment and reference in Java
(5 answers)
Setting equal in Java: by value or reference?
(4 answers)
Closed 2 years ago.
Hi so I'm relatively new to programming so this might be a silly question.
So I have these lines of code in a run function:
int[] bstate = this.state;
System.out.println(Arrays.toString(bstate));
nextMove();
int[] astate = this.state;
System.out.println(Arrays.toString(astate));
System.out.println(Arrays.toString(bstate));
to check whether or not nextMove() had changed the state of the object, but I think when state was updated, bstate was updated too because after the method call, bstate was the same as astate where it wasn't before.
So I'm wondering if as my methods update state, they implicitly update bstate too and if so, how do I prevent this from happening?
= just assigns a reference to arrays. It doesn't copy them.
The fix is to call clone() on the array. It's a shallow copy, so would have strange behaviour for, say, int[][] or StringBuilder[].
int[] bstate = this.state.clone();
Anything other than primitive types assigns a reference. int[] is actually an object. Another poster recommended cloning, ditto.

Convenience method to initialize mutable Set in Java [duplicate]

This question already has answers here:
How to initialize HashSet values by construction?
(24 answers)
Closed 7 years ago.
Is there a convenience method to initialize a Set equivalent to Collections.singleton, which returns a mutable Set instead of an immutable one?
Guava is defintely a good solution.
Alternatively, you can do:
Set<T> mySet = new HashSet<>(Arrays.asList(t1, t2, t3));
Guava's Sets includes:
public static <E> HashSet<E> newHashSet(E... elements)
which:
Creates a mutable HashSet instance containing the given elements in unspecified order.
You can call it with a single item as:
Sets.newHashSet(item);

why need Array when already having ArrayList? [duplicate]

This question already has answers here:
Benefits of arrays
(9 answers)
Closed 8 years ago.
I see besides the ability to increase the capacity, the other difference between an array and arraylist is that the latter does not allow the base type as primitive types. But since we have unboxing, which converts, saying, an Int type to Integer type, now arraylist can store any type. So why do we still need arrays?
Arrays are more efficient, because they have fixed size.

How to get new String[0].class without creating an array? [duplicate]

This question already has answers here:
What's the best way to get a Class object for an array type?
(2 answers)
Closed 4 years ago.
Is there a way to write the Java class for String arrays (or arrays for another class, for that matter) as a literal without creating an array object? The only Java term I can think of that gives the value is new String[0].getClass() which is creating a pointless array of length 0.
(BTW: I know that's cheap and I could put that in a static final, but I am curious whether there is another way.)
Try like this:
Class<String[]> cls = String[].class;
String[].class should do for you.

Ordering Array of Objects by an attribute [duplicate]

This question already has answers here:
Sort ArrayList of custom Objects by property
(29 answers)
Closed 7 years ago.
Is There a java util method, or a short way to sort an Array of some type by an attribute of that type. I currently have an array of Choice type objects where each have a getText() method that returns the visual representation of the choice. I can make a long method that creates an array of the choices texts, sort them, get their ordered index and then order the choices by that index, but I surely think there is some kind of a shortcut.
Any Suggestions?
Collections.sort(list, new Comparator<Choice>(){
public int compare (Choise c1, Choice c2) {
return c1.getText().compareTo(c2.getText());
}
});
add check for null if necessary
you can move comparator to external class and use reflection to read custom field from any object. but this will make code less understandable

Categories