![]() Java has a class Long (note the upper case L in Long), which defines two constants to represent maximum and minimum values of long data type, Long.MAX_VALUE and Long.MIN_VALUE. If num2 has a value that is outside the range of the int data type, you would not get the same value in num1 and it will result in data loss. At runtime, Java will use only the 32 least significant bits of num2, and assign the value stored in those 32 bits to num1. You do this using “cast” in Java, like so: long num1 = (int) num2 // Now it is fine because of the "(int)" castīy writing (int)num2, you are instructing Java to treat the value stored in num2 as an int. If you want to assign the value of a long variable to an int variable, you have to explicitly mention this fact in your code, so that Java makes sure you are aware that there may be data overflow. ![]() Even if num2's value 25 which is within the range of int. long num1 = 0L Įven if the value stored in a long variable is well within the range of the int data type, the assignment from long to int is not allowed without explicit type casting, as shown in the following example: int num1 = 5
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |