Aqua
Aqua

Reputation: 39

How to return a float array in java

This is my code:

public  float[] DataSet() {
  float[] Data = {1.51,2.35,3.36};
  return  Data;}

Why do I get the error message:

This method must return a result of type float[]

Upvotes: 1

Views: 1483

Answers (2)

Wickramaranga
Wickramaranga

Reputation: 1181

The literals, 1.51 etc. are implicitly double. That's how Java interprets them.

See Primitive data types in Java.

If you want a float array, try postfixing them with f.

float[] Data = {1.51f, 2.35f, 3.36f};

Upvotes: 2

Bobas_Pett
Bobas_Pett

Reputation: 591

The values inside array are doubles. You could for example do this:

public float[] DataSet() {
    float[] Data = {1.51f,2.35f,3.36f};
    return  Data;
}

Upvotes: 0

Related Questions