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

The solution of variables

Fortunately, programming gives us a way of storing and retrieving data; this is called the variable. To declare a variable in Java, we first have to specify what kind of variable we're going to be using. Variables come in a number of different types. In this instance, we're content with using whole numbers, that is, numbers that do not have a specified decimal place and aren't fractions. Also, in this case, it's appropriate to use one of Java's primitive types. These are essentially as base level as we can get with information in the Java programming language; just about everything else we work with in Java is built of the primitive types.

To declare a variable of the integer primitive type, that is, whole numbers, we use the int keyword, all lowercase. Once we do this, we need to give our variable a name. This is a unique identifier that we'll use to access this piece of information in future. Each variable in our local program should have its own name. Let's call our first variable x and our second variable y:

package variables; 
 
public class Variables { 
 
    public static void main(String[] args) { 
        int x; 
        int y; 
         
        System.out.println(2+3); 
        System.out.println(2-3); 
        System.out.println(2*3); 
        System.out.println(2/3); 
    } 
} 

We've just written two perfectly legitimate lines of Java code. If we run our program now, we'll see the same output we did before:

However, behind the scenes, Java will also be setting aside memory space for our x and y variables. This allocation doesn't affect our println commands because the variables are not referenced in them yet.

So let's store some information in our variables. We can reference a variable once we've created it simply by the variable's name. It's important that we do not reference our variable by typing int x again because this is the command for Java to create a brand new variable x, not access the existing variable x:

Once we've referenced our variable, we can change its value using the equal sign. So let's set x to 4 and y to 3. Our println commands currently operate with two explicitly declared integers: the numbers 2 and 3. Since x and y are also integers, it stands to reason that we can simply replace the existing numbers with the variables x and y:

package variables; 
 
public class Variables { 
 
    public static void main(String[] args) { 
        int x; 
        int y; 
        x = 4; 
        y = 3; 
        System.out.println(x+y); 
        System.out.println(x-y); 
        System.out.println(x*y); 
        System.out.println(x/y); 
    } 
} 

The following is the output of the preceding code:

When our Java code comes to the variables x and y, it will look to see what integer value they have currently been given. It will find the numbers 4 and 3. So if we run our program, we should expect the first println statement, x+y, to evaluate to 4+3, which then evaluates to 7. This is exactly what occurs.

So here's something interesting. The last line of our program, in which we divide x by y, isn't evaluating as we might mathematically expect it to. In this line of code, x has the value 4, and y has the value 3, Now 4 divided by 3 equals 1.3, but our program is simply outputting 1. That's because 1.3 is not a valid integer value. Integers are only whole numbers and never fractions or decimal numbers. So, to keep us working with integers, Java simply rounds down any calculations that have fractional portions to their nearest whole number. If we want to work in an environment where we could have fractional results, we would need to use a primitive type other than an integer.

Anyway, now that we've set up our println commands to take integer variable input instead of explicit numbers, we can modify the behavior of all four lines of the calculation by simply changing the values of these integer variables. For example, if we wanted our program to run on the input values -10 and 5 (integers can be negative; they just can't have fractional components), all we would need to do is change the values we give to our variables x and y:

package variables; 
 
public class Variables { 
 
    public static void main(String[] args) { 
        int x; 
        int y; 
         
        x = -10; 
        y = 5; 
         
        System.out.println(x+y); 
        System.out.println(x-y); 
        System.out.println(x*y); 
        System.out.println(x/y); 
    } 
} 

If we run the preceding code quickly, we will see the expected results:

Awesome! You've just learned the very basics of working with both integers and variables in Java.

主站蜘蛛池模板: 绍兴市| 宽甸| 汽车| 双流县| 华安县| 承德市| 黑山县| 友谊县| 天等县| 绥滨县| 鹿邑县| 敖汉旗| 博客| 宣威市| 扶余县| 化州市| 东辽县| 东兰县| 桓台县| 玉龙| 芜湖县| 远安县| 黄龙县| 柳林县| 旺苍县| 乌鲁木齐市| 耒阳市| 万州区| 南华县| 顺昌县| 霍邱县| 什邡市| 五河县| 阳新县| 福清市| 即墨市| 青河县| 宁晋县| 塔城市| 义马市| 宣汉县|