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

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:

主站蜘蛛池模板: 九龙城区| 九龙县| 罗田县| 五寨县| 西宁市| 丰宁| 平南县| 瑞金市| 常州市| 武安市| 大姚县| 靖远县| 沙田区| 武隆县| 汶川县| 饶河县| 达州市| 清徐县| 红安县| 株洲县| 高尔夫| 黔东| 元阳县| 宁陕县| 九台市| 射洪县| 明溪县| 浦东新区| 双江| 凉城县| 五台县| 嫩江县| 汝州市| 资兴市| 隆回县| 城固县| 乐昌市| 星子县| 安泽县| 托克托县| 临武县|