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

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>.

Related

Confused on when to use Character vs. char in Java? [duplicate]

This question already has answers here:
Why do people still use primitive types in Java?
(21 answers)
When to use wrapper class and primitive type
(11 answers)
Closed 2 years ago.
I know that char is a primitive type while Character is a class, but I'm confused on when to use which? Is there a good rule of thumb for this?

Is this valid Syntax to create character linklist? [duplicate]

This question already has answers here:
Why don't Java Generics support primitive types?
(5 answers)
Closed 5 years ago.
LinkedList<char> PC=new LinkedList<char>();
getting this error message when tried to run.
Solution.java:5: error: unexpected type
LinkedList<char> PC=new LinkedList<char>();
^
required: reference
found: char
Can anyone explain why we can create the link list of arrays, object, and string but not of character?
Array is an object, when char is a primitive type. As generics support only non-primitive types, you can create LinkedList<Character> and autoboxing will take care of packing chars into Character class.
you can create a linked list of any Object. just not primitives.
you can't create a linked list of char\int\long\etc....

What is the correct way to create a object array in java? [duplicate]

This question already has answers here:
Difference between int[] array and int array[]
(26 answers)
Closed 7 years ago.
I've seen examples such as:
Type arrayname[] = new Type[];
also as written as:
Type[] arrayname = new Type[]
I am quite confused about such expressions!
Where exactly should I put the []?
Any of the above are allowed. All produce the same bytecode. JLS-10.2 Array Variables says (in part)
The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

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.

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

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.

Categories