Home > Software design >  What is the return type of Java built-in array's access operator when contains primitive type?
What is the return type of Java built-in array's access operator when contains primitive type?

Time:10-07

When array xs has primitive char values and someone try to accessing that value using [], what is the return type?

Is it primitive char or wrapper Character?

I think it's always Character type, when array contains either char or Character.

Because when char minus int, the type of result is char.

char x = 'A' - 1;

When Character minus int, the type of result is int.

int x = new Character('A') - 1;

And result type of expression xs[0] - 1 is always int.

char[] xs = {'A'};
int x = xs[0] - 1;


char[] xs = {new Character('A')};
int x = xs[0] - 1;

Am I right? I cannot find document about result type and conversion of Java array's operator [].

CodePudding user response:

What is the return type of Java built-in array's access operator when contains primitive type?

It is the primitive type; e.g. for an int[] it is int, for a char[] it is char and so on.


However, I think you are getting yourself rather confused here.

All Java arithmetic operators applied to operands of type byte, char, short or int (or their wrapper types) always produce an int result. So, the expression types of 'A' - 1 and new Character('A') - 1 and xs[0] - 1 are all int.

That is why you get an errors here:

 char a = 'A';
 char[] a2 = {'A'};
 char b = a   1;                  // COMPILATION ERROR
 char c = a2[0]   1;              // COMPILATION ERROR
 char d = new Character('A')   1; // COMPILATION ERROR

You need type casts to make those assignments work.

So why don't you get a compilation error with the following one?

 char x = 'A' - 1;

In short, because 'A' - 1 is a (compile time) constant expression whose (int) value will fit into a char without any loss or information.

There is a special rule for the assignment operator that says:

  • IF the LHS type is byte, char or short, AND
  • IF the RHS expression type is int, AND
  • IF the RHS expression is a compile time constant expression, AND
  • IF the value of the RHS expression is in the range of the LHS type,
  • THEN there is an implicit conversion from int to the LHS type.

The above is a simplification of the JLS rules ... which include a precise specification of what a constant expression is.

Intuitively, the compiler allows you to leave out the type cast because it "knows" that a type cast would not lose any information.

CodePudding user response:

When array xs has primitive char values and someone try to accessing that value using [], what is the return type?

The type of the element always corresponds to the type of the reference (left hand side). Auto-boxing occurs if primitives assigned on the right where the type is Character[] or auto-unboxing if references assigned where type is char[]

You could check using instanceof Character - it will return true to verify it's a Character or compile error if it's a primitive.

char[] xs = {'A'};
System.out.println(xs[0]); 
System.out.println(xs[0] instanceof Character);   // COMPILE ERROR: unexpected type  required: reference  found: char

char[] ys = {new Character('A')};
System.out.println(ys[0]); 
System.out.println(ys[0] instanceof Character);   // COMPILE ERROR: unexpected type  required: reference  found: char
     
Character[] cs = {'A'};   
System.out.println(cs[0] instanceof Character);   // true
  • Related