-6

I have an Image class and I initialize an array as below. Here Java allow me to have only Image objects inside the images array. Why I am not able to assign string or any other values to the images array? In case of String [] names = new String[3] names array can hold string values. So, is there anything in the String class which will allow only string to assign to names array and we cannot assign any object to names array?

Image[] images = new Image[3];
public class Image {

  /** Instance variables **/
  private String imgLoc;
  private String imgDesc;
  private int dimensionX;
  private int dimensionY;
  private boolean defaultImg;

  /** Getter method for image location **/
  public String getImgLoc() {
    return imgLoc;
  }

  /** Setter method for image location **/
  public void setImgLoc(String imgLoc) {
    this.imgLoc = imgLoc;
  }

  public String getImgDesc() {
    return imgDesc;
  }

  public void setImgDesc(String imgDesc) {
    this.imgDesc = imgDesc;
  }

  public int getDimensionX() {
    return dimensionX;
  }

  public void setDimensionX(int dimensionX) {
    this.dimensionX = dimensionX;
  }

  public int getDimensionY() {
    return dimensionY;
  }

  public void setDimensionY(int dimensionY) {
    this.dimensionY = dimensionY;
  }

  public boolean isDefaultImg() {
    return defaultImg;
  }

  public void setDefaultImg(boolean defaultImg) {
    this.defaultImg = defaultImg;
  }
}
Michu93
  • 5,058
  • 7
  • 47
  • 80
user3742125
  • 617
  • 2
  • 9
  • 31
  • That's how arrays work. – SLaks Jul 05 '18 at 13:46
  • 1
    Possible duplicate of [What is the difference between statically typed and dynamically typed languages?](https://stackoverflow.com/questions/1517582/what-is-the-difference-between-statically-typed-and-dynamically-typed-languages) – Nambu14 Jul 05 '18 at 13:57

3 Answers3

3

Why I am not able to assign string or any other values to the images array?.

The Java type system prevents it. An array declared with type Image[] can only hold objects that are Image instances, or instances of a subtype of Image (or null values). The compiler will prevent you from doing that.

That is what is meant when we say that Java is a staticly typed language.

Furthermore, the runtime system is designed so to make it impossible to "break" the static typing rules for arrays. For example, if you try to use reflection to put a String into a an Image[], you will get an immediate exception.

That is what is meant when we say that Java is a strongly typed language.

(There are some issues to do with generic types and unsafe conversions, but even if you get that wrong the worst that can happen is that some of the hidden typecasts may result in exceptions in unexpected places.)

(The only way to actually break the type system is to step outside of the Java language; e.g. by using native code or the Unsafe class. But if you do that you are liable to make the JVM unstable, leading to JVM panics.)

In case of String [] names = new String[3], the names array can hold string values. So, is there anything in the String class which will allow only string to assign to names array and we cannot assign any object to names array?

Sure. The type system. The situation is the same as for your example with Image arrays.

But note that it is not the String class that is doing this ... except that String is a final class, so no subtypes of String are possible.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-1

The type of the array determines what you can put in it. Java is a statically typed language.

Per
  • 1,074
  • 6
  • 19
-1

There is nothing that has to be in the String class. This is how arrays works.

The Type[] array; will only accept Type instances or instances of a sub-class of Type

ex for subtype

Number[]array = new Number[5];
array[0] = 5;    //int
array[1] = 5.0f; //float
array[2] = 5.0d; // double

If you want to be able everything : Object[]

azro
  • 53,056
  • 7
  • 34
  • 70