The float
is a primitive type of decimal numbers in Java, example: 1.234
,5.7
,19.3456999
. Note that the decimal numbers in Java and in other languages have no comma (,) but a point. In the world of programming we call these types of value numbers Floating Point, and from that came the name float.
The float[]
is a array (set of variables) one-dimensional float
. In Java you can only store values that are the same type as the array, in this case you can only store values of the type float.
Initializing an array:
There are several ways to initialize an array in Java, but the most basic form is using new tipo[tamanho]
. Note that you can initialize arrays of several types of values because the brackets indicate that your variable will save an array. The correct thing in this case is to say that the reference variable save an array. See this example:
String myArray[] = new String[5]; // O array guardará 5 valores no máximo do tipo String.
And if I just write float[] myArray;
in my code ?
In this case you will only be creating a reference but not the array itself.
Multidimensional array:
Sometime you can also find the following code:
float[][] myArray = new float[tamanho][tamanho];
Wait a minute... two brackets ? Whaaat ?!
To your surprise, there may be one, two or more square brackets. More than one square bracket means that the array is no longer one-dimensional but multidimensional. This means that within an array there are other arrays. See the code below:
public class MinhaClasse {
public static void main(String[] args) {
String[][] nomes = new String[2][2];
nomes[0][0] = "Maria";
nomes[0][1] = "João";
nomes[1][0] = "Lucas";
nomes[1][1] = "Larissa";
for (int y = 0; y <2;y++) {
System.out.println("Este é o id da posição "+y+" do array: "+nomes[y]);
for (int x = 0; x<2;x++) {
System.out.println(nomes[y][x]);
}
}
}
}
This is an array of floats, i.e., as a variable with several values of the same type, float.
– Guilherme Nascimento
Through your comment I didn’t quite understand what an array is, but at least I already know what it is, after that I will study more on such a topic. Thank you very much.
– Gusttavo Santtos
That is, a "variable" that accepts several values of the same type, in the case float[] will accept something like
float[] x = { 1.1, 2.2, 3.4, 1.2 };
, already float only one valuefloat y = 2.5;
– Guilherme Nascimento
So instead of creating 5 variables, I create only one?
– Gusttavo Santtos
If it is YOUR intention yes, type has amount of undetermined values or even treat/compare various values of the same type to know which one is larger, assuming it receives several floats, you want to know which one is bigger, which one is smaller, which one is on average. One of the uses of a vector instead of several variables is to be able to have an amount of "indeterminate" values. Of course, there will be times that you might want to use multidimensional, something like
float[][]
,float[][][]
, etc..– Guilherme Nascimento
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero