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.
Related
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.
This question already has answers here:
Is an array a primitive type or an object (or something else entirely)?
(7 answers)
Closed 4 years ago.
When I typed:
int[] i = new int[3];
in my IntelliJ and I can see that "i" has "length" property and "clone" method. So I really wonder, is java's "Array" a raw type or not? I suppose only "objects" should have property of methods right?
Or there's something special done by java compiler or jvm, that makes raw type of Array "just look like objects"?
Please kindly help to explain. Thanks!
Is java's array “primitive type”?
No. Java's array is a reference type. You can see the java.lang.reflect.Array class for insight into how it works internally. For example,
int[] i = { 1, 2, 3 };
Object v = i;
System.out.println(v.getClass());
System.out.println(Array.get(v, 1));
Outputs
class [I
2
This question already has answers here:
Empty an ArrayList or just create a new one and let the old one be garbage collected? [duplicate]
(4 answers)
Closed 5 years ago.
Which is the best practice between
ArrayList.clear();
new ArrayList<Object>();
I have a list which gets data, I use them and then I have to recycle it.
Which is the best way?
I think if u just clear it its 1 less object for GC to handle, so its better.
I dont see any actual advantage on creating a new array list object if not changing the type.
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
This question already has answers here:
What is the difference between a variable, object, and reference? [duplicate]
(5 answers)
Closed 7 years ago.
new ArrayList<Integer>().addAll(Arrays.asList(4,5,6,7));
I could write:
ArrayList<Integer> my_array = new ArrayList<Integer>().addAll(Arrays.asList(4,5,6,7));
How can i access the first array later in my code without creating a reference to it?
In your first code, you are just creating an object. And in the second piece of code, you are creating an object and putting it into a variable.
Let's have an analogy here, imagine that objects are like balloons, and the variables are like kids holding the balloons. In your first code, you create a balloon and "Let it go". And it flies away. In the second code though, you create a kid and a balloon and you told the kid to hold it. Now you can ask the kid for the balloon and you can access it.
So long story short, You cannot access the List in the first code.
If you don't create a reference, you won't be able to access it. No exceptions that I am aware of.
I suppose you could push that new object into another ArrayList and then use .get() to access it again. No point to that unless you want a list of lists.