How does Java calculate length of primitive array? [duplicate] - java

This question already has answers here:
Where is array's length property defined?
(7 answers)
Closed 8 years ago.
Code:
char[] chars = "abcd".toCharArray();
System.out.println(chars.length);
Question: How is length calculate by Java here? Since char is not a Class, I am not sure where length is stored. If it isn't stored, is it calculated every time you do chars.length? (I presume not)

The thing you wrote as char[] is an Object, an array, and has a public final field called length. It is calculated once when the array in created. Like all objects it also has a toString(), notify(), etc...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7
The public final field length, which contains the number of components
of the array. length may be positive or zero.

In this case chars.length is returning the number of elements in the array.
Or am I missing the question?

Even though char is not a Class/Object type in Java, an array of char actually is. And the length is a (final) field of that class. See the answer here for more.

Related

Character vs char with arrays and array lists? [duplicate]

This question already has answers here:
Why can Java Collections not directly store Primitives types?
(7 answers)
Closed 2 years ago.
Why are Character arrays not valid, but char arrays are, and why are Character lists valid, but char lists not? This probably has something to do with primitives vs objects, but can someone explain it more clearly?
both char[] and Character[] are perfectly valid. When it comes to generics, though, (like those in List<T>), the generic parameter must be a class and not a primitive, so you can only have List<Character>.

Why do we create object variables like this? [duplicate]

This question already has answers here:
Differences between new Integer(123), Integer.valueOf(123) and just 123
(6 answers)
create a new Integer object that holds the value 1?
(2 answers)
Advantage of using new Integer(a) over a
(3 answers)
using new(Integer) versus an int
(3 answers)
Closed 3 years ago.
I am new to learning Java, and I was told to create object variables like this:
Integer a = new Integer(2);
Instead of like this:
Integer a = 2;
Can someone explain why is creating object variables the 2nd way bad?
edit: I am adding this here cause I am getting mixed answers
Which one am I supposed to use and when?
The second approach is actually better, since it will implicitly call Integer.valueOf(). From the docs:
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.
(Emphasis mine.)
See also: Autoboxing

Printing Two Dimension Arrays [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
I know this isn't a difficult issue but I have come to a complete stand still, I want to print a two dimension array with values already assigned for example:
int array1[][] = new int[1][1];
array1[0][0] = 10;
array1[0][1] = 20;
array1[1][0] = 30;
array1[1][1] = 40;
I just want to simply print the values and I really can't remember how to do it, I keep on doing this
System.out.println(Arrays.toString(array1))`;
but I know this is wrong, can you help?
You may want to look at the Arrays.deepToString method, which is similar to Arrays.toString except that if the nested objects are arrays, it will recursively (and correctly) convert them into strings as well.
Also, note that there's a slight typo in your code - you need to size the array as
int[][] array1 = new int[2][2];
since you want a 2x2 array, not a 1x1 array.

Why the java array documentation doesn't show the field "length"? [duplicate]

This question already has answers here:
Where is array's length property defined?
(7 answers)
Closed 7 years ago.
I have seen some examples of getting the length of an array using the field length, for example: array.length. I have always used this field but checking the array documentation I did not see that variable. Why is it that the documentation doesn't show it? It only shows a bunch of methods but I can't see the variable length. Is it in another class or what? I have seen questions like this before but the answers are not well explained so I can't understand them.
Because length is not actually a field. The compiler recognizes the identifier specially and translates it to an arraylength instruction rather than a getfield instruction.

Multidimensional array in java [duplicate]

This question already has an answer here:
Arrays with trailing commas inside an array initializer in Java
(1 answer)
Closed 7 years ago.
int [][] a = {{1,2,},{3,4}};
int [][] a = {{1,2},{3,4}};
i have two Multidimensional arrays in java. I want to know what's the exact difference in both of them.just see there is a comma after 2 in first one.
JLS 10.6. Array Initializers: A trailing comma may appear after the last expression in an array initializer and is ignored.
There is no difference. The extra comma might be useful if you are formatting your elements one per line, so that every element appears the same.

Categories