- Java Programming for Beginners
- Mark Lassoff
- 747字
- 2021-07-02 15:22:45
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.
- Expert C++
- 零基礎PHP學習筆記
- C# Programming Cookbook
- Hands-On Data Structures and Algorithms with JavaScript
- Vue.js快跑:構建觸手可及的高性能Web應用
- Java高手真經(高級編程卷):Java Web高級開發技術
- 程序設計基礎教程:C語言
- RocketMQ實戰與原理解析
- Android移動應用開發項目教程
- 百萬在線:大型游戲服務端開發
- AutoCAD基礎教程
- Android嵌入式系統程序開發(基于Cortex-A8)
- Getting Started with JUCE
- Beginning PHP
- Android 5從入門到精通