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

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:

主站蜘蛛池模板: 寻乌县| 奉新县| 四子王旗| 利川市| 隆子县| 西畴县| 浦城县| 乡城县| 库车县| 潜江市| 防城港市| 久治县| 安顺市| 米泉市| 沁水县| 石景山区| 顺平县| 余姚市| 庆城县| 贵南县| 子长县| 大化| 沙坪坝区| 阳信县| 永嘉县| 南郑县| 青阳县| 白朗县| 阳朔县| 纳雍县| 中西区| 锦州市| 白银市| 苏尼特右旗| 乡宁县| 基隆市| 阜南县| 云霄县| 温州市| 东海县| 南投市|