0

Assume the following scenario. i call a method like this

String[] arr = {"1","2","3"};
method(arr);

and the method signature is

public void method(Object o)
{
// how will i get back the String[] arr object now.. 
}

4 Answers 4

3

The casting would look like this:

final String[] array = (String[]) o;
Sign up to request clarification or add additional context in comments.

Comments

1

If you are writing

public void method(Object o) {
   String[] arr = (String[]) o;
}

this means the only valid parameter type is String[] and you are better off making this clear with

public void method(String[] arr) {

}

Comments

1

Just cast it back.

String[] o2 = (String[]) o;

Comments

1
public void method(Object o) {
   String[] arr = (String[]) o;
}

Here o is your passed String[] object. Just cast it to String[]. And if you are planning to pass an array every time then change your method signature to:

public void method(Object[] o)

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.