0

I can never find what that code perform:

functionName(new float[]{factor});

is it array declaration? I mean:

new float[]{factor}

it is equal to

float[] arrayName = new float[factor];

?

4 Answers 4

2
new float[]{factor} 

is just one type of array declarations in Java. It creates a new float array with factor value in it.


Another ways how we can declare arrays:

For primitive types:

int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};

For classes, for example String, it's the same:

String[] myStringArray = new String[3];
String[] myStringArray = {"a","b","c"};
String[] myStringArray = new String[]{"a","b","c"};

Source from How do I declare and initialize an array in Java?

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

Comments

1
new float[]{factor}

This piece of code creates a float array with a single element and that element is factor.


functionName(new float[]{factor});

And this means that you're calling the method functionName() and passing a single-element float array to it, where that single element is factor.

Comments

0

It means you are passing an anonymous array to the function as an argument and No, it doesn't mean that factor is the length of array.
It means:

float factor = 0.5f;
float[] array = {factor};
function(array);

Comments

0
functionName(new float[]{factor});

Calls a function named: functionName passing a new array (anonymous) with one element as an argument, which is the contents of factor.

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.