Navigator


Archive

201139
201230
201312
20151
201633
201755
201865
201955
20231

Creation date:

Implicit typecasting

Java makes some impicit typecasting when we assign one primitive type to another broader promitive type. But we can't do it with wrappers. Because all numeric wrappers are extend Numeric, but not each other. You can write

byte b = 5;
int i = b;

But you can not write

Long l = new Integer(6);

Now let's look at arrays

class A{}
class B extends A{}

public class Test {
  int[] intArr;
  byte[] byteArr = {2, 5, 10};
  A[] aArr;
  B[] bArr = new B[5];

  public static void main(String[] args) {
    Test test = new Test();
    test.intArr = test.byteArr; //will not compile
    test.aArr = test.bArr; //correct and will compile
  }
}

Although Java casts byte to int implicitly, it can't cast byte[] to int[] neither implicitly nor explicitly. But in case of inherited object it compiles fine.

Author: Jafar N.Aliyev (Jsoft)

Read also

Static methods can not be overriden

Here I explain, why static methods can not be overriden

Use instanceof carefully

Usage of 'instanceof' method in some situations will give you a compilation error

Java access and nonaccess modifiers

Look at this cheat sheet for Java access and nonaccess modifiers

© Copyright

All articles in this site are written by Jafar N.Aliyev. Reproducing of any article must be followed by the author name and link to the site of origin(this site). This site also keeps the same rules relative to the articles of other authors.