I'm learning Java with Eclipse, but I have a question about data types.
Among the error messages of the byte type, there is an error saying 'Type mismatch: cannot convert from int to byte', and to assign an integer to the long type, you must add 'L' to the end of the number. If you want to use a float type rather than a double type even for real numbers, you must add 'F' to the end of the decimal.
Do integers and real numbers check the data type of a variable and keep it int or change to another integer data type such as long after it is assigned as int and double type regardless of the size of the value and the data type specified before the variable name?
If so, if you use byte or long instead of int when writing an integer variable, the wasteful action of converting the type once occurs. Is that a very small action that can be ignored when creating a program?
I wonder if I should always keep this in mind when creating integer and float variables.
Are you entering the values in out code like this?
byte bVal = 10;
short sVal = 10;
int iVal = 10;
long lVal = 10;
Then there is no "conversion loss", you are just telling the compiler how to treat the values you entered, and that you really know what integer size you are using (8, 16, 32 or 64 bits). The same applies to double and float.
The compiler generates the correct byte code during the compile. No conversion happens.
More information can be found in the Java Languange Specification.