0

There is this following code that I am having a problem wrapping my head around what (String) does. I am assuming that it is taking IR which is attached to a java panel(setvalue), casts memoryArray into a string, and sets the value of IR as that string but I am not sure. If yes, how does this differ from doing toString() as it is also used to cast array to string.

Also, I was wondering if this is possible to do in python simply.

 public static final int IR = 1;   
 int address2 = 3;
          
 Object[][] memoryArray = new Object[256][17];
 int memR2 = address2/16;
 int memC2 = address2 % 16 + 1;
 IR.setValue((" "+(String) memoryArray[memoryRow][memoryCol]).trim()); 
 System.out.println(IR);

1 Answer 1

1

Casting (String) memoryArray[memoryRow][memoryCol] would work only if the array contains a String object at [memoryRow][memoryCol], otherwise a runtime ClassCastException is thrown.

It is not the same as calling toString() method of Object which converts the object of any class to String. If object at [memoryRow][memoryCol] happens to be null, call to the toString method will result in NullPointerException.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.