What is the FLOAT command used for in java? They left me an exercise where I have to capture enrollments, names and the 3 grades of students and then give the user the option to consult them when entering the student's enrollment and asks to use the float command to achieve this. My question is what is it for and how is it used? The teacher wrote this as an example:
float c1[]=new float[20]
float c2[]=new float[20]
float c3[]=new float[20]
float
is one of the Java primitive types. It is used to store floating point numbers and has a precision of 32 bits. To define and initialize a type variablefloat
you do as follows:What you have in your teacher's example is an array of
float
. Consider that each element of your array must have elements of typefloat
. To continue her example:For more information, you can review the official Java guide: Primitive Data Types
Grades are usually numbers with integer part and decimal part, for example 3.5 or 5.0 or 4.8 and can be stored in a float type variable in java, this is one way:
Your code has brackets [] or square brackets [] indicating that more than one value is stored on those variables, in this case, 20 values of type float.
This means that 20 float numbers are stored in variable c1, 20 float numbers in variable c2, and 20 float numbers in variable c3. Placing each grade in one of the 20 positions with the [] differentiates them from each other and can logically indicate that these grades belong to a certain student. For example, the following code displays all three grades for the first student: