What is the difference between a long and an int
The integer data types are used to store numeric values. In this article, we will discuss the difference between these four Integer data-types. JAVA does not support an unsigned version of these integer data types. The main basis of difference is size and range.
Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. It is also an integer. It is an integer. It will iterate 5 times. Typecasting can be performed on data types. It is the process of converting a one data type to another data type.
When assigning a smaller data type to a larger data type, there is no casting required. The widening happens in a byte, short, int, long, float, double.
When assigning a larger data type to a small data type, it is necessary to do the casting. In above program, the num1 variable has value The variable num2 has value The total is an int. As int is a larger data type than a byte, it is necessary to typecast into byte in order to store into a byte variable.
If there is no typecasting, it means the integer value is assigned to the byte variable so there will be a compile-time error. The long is a predefined data type provided by languages such as Java. It is 64 bits in width. The number of bytes for a long is 8 bytes. One byte is equivalent to 8 bits. Refer the below program. According to the above program, width and length are long variables. The resulting value is assigned to a long variable. The long is the largest data type.
Other data types are smaller than long. So other data types can be assigned to long without typecasting. When assigning a long value to int, it is required to typecast. I know how to tick the V I know how to tick the V 1, 2 2 gold badges 10 10 silver badges 4 4 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Basil Bourque k 75 75 gold badges silver badges bronze badges.
Borealid Borealid Borealid, this is so wrong, Java is always pass by value, there is nothing such as pass by reference in Java. Uh huh? Okay, let's explore the idea that Java never passes a reference. If I call a function giving it an int[2], and then inside the function I change the value held in the first element of that array, will the function's caller see that change?
Well that's because what was passed to the function wasn't the values in the array, but rather a reference to those values. Whomever taught you that Java was pass-by-value did you a disservice. Borealid you are incorrect, the value is changed because a new copy of the reference to the array was created, that is why whoever is calling the function will see the change, this is still pass by value.
I can't believe you say that there is pass by reference in Java. You never pass the original reference to the function.
In a theoretical world where Java behaved completely differently from how it actually does, and passing an object or array to a function resulted in a totally separate copy where modifications inside the function weren't reflected to callers at all , how would you then describe the language? Keeping in mind that what we're talking about is completely not what Java actually does. DiegoRamos is right.
Java is always pass by value, the confusion is created because the value when non primitive is a memory reference, static swapping does not work in java, more details here: journaldev.
Show 5 more comments. There are a couple of things you can't do with a primitive type: Have a null value synchronize on them Use them as type parameter for a generic class, and related to that: Pass them to an API that works with Object s Unless you need any of those, you should prefer primitive types, since they require less memory.
Michael Borgwardt Michael Borgwardt k 75 75 gold badges silver badges bronze badges. By default use an int , when holding numbers. If the range of int is too small, use a long If the range of long is too small, use BigInteger If you need to handle your numbers as object for example when putting them into a Collection , handling null , Joachim Sauer Joachim Sauer k 55 55 gold badges silver badges bronze badges. Jesper Jesper k 44 44 gold badges silver badges bronze badges.
Lucky Houcem Berrayana Houcem Berrayana 3, 22 22 silver badges 39 39 bronze badges.
0コメント