官术网_书友最值得收藏!

Memory allocation for integer variables

Let's go over an edge case and learn a little bit more about how Java thinks. You might remember from earlier, I spoke about how Java sets aside memory when we declare new variables. This is one of the huge advantages of working in a high-level programming language, such as Java. Java abstracts away or automatically takes care of most of the memory management for us. Quite often, this makes writing programs simpler, and we can write shorter, cleaner, and more easily readable code. Of course, it is important that we appreciate what's happening behind the scenes, lest we run into issues.

For example, whenever Java sets aside memory for an integer variable, it also sets aside the same amount of memory for all integer variables. This means there's a maximum and minimum value that Java could ever conceivably store in an integer variable. The maximum integer value is 2147483647 and the minimum integer value is 2147483648.

So let's do an experiment. What happens if we attempt to store and print out an integer variable that is one larger than the maximum value? To start with, let's simplify our program. We're simply going to assign a value, one higher than possible, to the variable x:

When we attempt to do this, NetBeans yells at us. It's got some logic built in that attempts to stop us from making this very basic and common mistake. If we were to attempt to compile this program, we would also get an error.

However, we want to make this mistake in the name of science, so we're going to trick NetBeans. We're going to set the value of our variable x to the largest possible integer value, and then in the next line of our code, we're going to set the value of x to be one higher than what x is currently, that is, x=x+1. Actually, there's a nifty little shorthand we can use for this: x=x+1 is equivalent to x++. OK, so when we run this program, which will sneak by the compiler and NetBeans, and do our addition at runtime, we attempt to print out an integer value that is one plus the highest integer value that Java can store in a memory location:

package variables; 
 
public class Variables { 
 
    public static void main(String[] args) { 
        int x; 
         
         
        x = 2147483647; 
         
        x++; 
        System.out.println(x); 
         
    } 
} 

When we run the preceding program, we get the following negative number:

This number happens to be the smallest number that we could ever store in an integer. This makes some sort of visual sense. We've gone so far positive, or to the right, on our integer number line, that we've arrived at the leftmost or the most negative point. Of course, in a mathematical sense, this could get pretty confusing pretty quickly.

It's unlikely that we're ever going to write programs that will need integer numbers higher than this value. However, if we do, we certainly need to be aware of this issue and circumvent it, using a variable type that can handle larger values. The long variable type is just like an integer but we need to allocate more memory for it:

package variables; 
 
public class Variables { 
 
    public static void main(String[] args) { 
        long x; 
         
         
        x = 2147483647; 
         
        x++; 
        System.out.println(x); 
         
    } 
} 

When we run the preceding program, we will get a mathematically accurate result:

主站蜘蛛池模板: 宽甸| 辛集市| 威宁| 麻城市| 凤山县| 永川市| 遂平县| 绥中县| 库车县| 甘谷县| 罗定市| 平和县| 五寨县| 清镇市| 奈曼旗| 巴中市| 勃利县| 昭通市| 满城县| 喀什市| 抚顺市| 内黄县| 台江县| 沈阳市| 奉化市| 石门县| 称多县| 明星| 宜兴市| 福清市| 简阳市| 八宿县| 松桃| 临潭县| 崇礼县| 金平| 京山县| 博爱县| 绥棱县| 永清县| 呈贡县|