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

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:

主站蜘蛛池模板: 宿松县| 常山县| 沂南县| 尖扎县| 民权县| 合川市| 凌云县| 都江堰市| 定日县| 博客| 宝应县| 马尔康县| 阜平县| 高邑县| 司法| 金湖县| 日照市| 扬中市| 博野县| 靖宇县| 韶山市| 阿鲁科尔沁旗| 佳木斯市| 珠海市| 滨海县| 秦皇岛市| 科尔| 美姑县| 株洲县| 延寿县| 牟定县| 霍林郭勒市| 永平县| 高淳县| 桂阳县| 彰化县| 皮山县| 汶上县| 合作市| 衡南县| 攀枝花市|