What is the difference between float and float[]?

Asked

Viewed 431 times

1

I’m seeing some codes that some exercises here on the site and whenever I see something I don’t understand, I research.

In the case now it was regarding the float[] ,

What is the real difference between this type of determination and the simple float?

  • 1

    This is an array of floats, i.e., as a variable with several values of the same type, float.

  • 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.

  • 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 value float y = 2.5;

  • So instead of creating 5 variables, I create only one?

  • 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..

  • 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

Show 1 more comment

3 answers

4

The guy float is a type of numerical data that holds values of various magnitudes and accuracy in the well-rounded decimal part, so it can hold very large and quite fractionated values. Contrary to what many think, they lack accuracy, it cannot represent all numbers, it depends on approximations, but for certain problem this is no problem, it is when it comes to monetary value. Although it has a decimal part, we call it binary floating point value since its representation is binary, so it has no accuracy.

The float[] is indicating that you have a data vector floats. Then there will be a variable which will be composed of several numerically indexed variables to access each element. And each of these elements is a data of the type float. This is the syntax to declare a given as a array of floats. See also What are the applications of day-to-day arrays? (examples of their usability).

You declare a variable like this:

float notas[] = new float[5];

Java accepts it as well and some prefer it because it’s more symmetrical, which is what you might have seen:

float[] notas = new float[5];

Note that you use the brackets without a value only in the variable, when we use with the type we are initiating the array and so we need to put a value there to say the size of the array. Without a value gives error and does not compile. This value can be fixed or variable (only known at the time of creating the array), but after the array be created it can no longer change size, so in many cases people prefer to create a ArrayList that allows it, then changes everything:

List<float> notas = new ArrayList<float>();

I put in the Github for future reference.

I won’t give any more details because the links already respond with much more property and it makes no sense to write here again.

  • Transcribing the answer above. "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."

1

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]);

            }
        }
    }
}

0

The float is a primitive type of a number with decimals, for example: 1.6 (1.6 in BR). The float[] is a set of variables (one-dimensional array) of float type, ie several numbers with decimals in a "box" [1.6, 1.0, 3.2]. These numbers within a "box" we also conceptually call vector.

Note: It is also possible to have boxes inside boxes float[][], in this case we call conceptually two-dimensional arrays, or matrices.

Remembering that java has a class called Arrays (java.utils.) that has several methods that help in the manipulation of arrays.

  • In Java, vector is the same as array? And a array of array is the same as matrix? Are you sure of that?

  • Oh, sorry, I explained incorrectly, I will edit. Thank you for informing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.