lesson3

Hello everyone, welcome everyone to join me and continue to learn Java

 

As we learned in the last lesson, in Java, statements are executed sequentially, and each statement needs to be separated by ;

We can get the user's keyboard input through the following statement (new Scanner(System.in)).next()

When we need to store data in the program, we can declare a variable and assign a value to this variable through the = sign

 

In the last lesson, we knew the string type String, and the variables declared with it can be used to store textual information, such as names, etc.

 

In today's lesson, let's take a look at what other types of variables exist in Java.

Everyone knows that the reason why a computer is called a computer is mainly because it can handle numerical calculations. When it comes to mathematics, I believe everyone will have an inexplicable headache. Don’t worry, we can write a program to tell the computer how to calculate, so that it It can help us solve most of the math problems.

 

First, in Java, we are provided with rich data types for storing numbers,

There are byte, short, int, long for storing integers. With so many types of numbers, some students asked, what is the difference between them?

In fact, the only difference between them is that they can store different size ranges of numbers, let's take a look at the table below

show table

In our daily work, we most often use the int type, which is enough to store most of the integers in our daily life.

For students who want to know why their numerical ranges are different, please see the detailed explanation, and I will not explain too much here.

 

Details: How do computers store numbers?

The underlying hardware of the computer is actually very simple and not as complicated as we think. These hardware composed of semiconductors can only store two kinds of physical signals with electricity and without electricity, but we humans are very smart, using these two signals to express Then, we use binary and decimal conversion to express the numbers 0 to 9 with binary 0 and 1.

In a computer, each storage unit used to store 0 and 1 is called a bit, that is, a bit. Every eight bits, we are used to calling it a byte, that is, a byte.

A byte takes up one byte, a short takes two bytes, an int takes four bytes, and a long takes eight bytes.

Now you can understand why there are so many data types to store integers and decimals. This is actually to better utilize the storage space of the computer according to the size of the data.

 

If we want to store decimals, what data types does Java give us?

In Java, two data types float and double are provided to store decimals. The difference between them is the same as that of integers, that is, the size range of the stored numbers is different, let's take a look at the following table

show table

In daily work, the most commonly used decimal type is double.

 

We don't need to memorize the exact size range of these numbers, just refer to the table when unsure.

Just remember that the most commonly used integer is int, and the most commonly used decimal is double 

 

Now that we have learned about the number types in Java, let's take a look at what we can do with numbers in Java.

 

In fact, in Java, all four arithmetic operations are supported, and their symbols are +,-,*,/

Another is to find the remainder %.

 

Let's try to compute some simple operations

some simple operations

We found that the result of an integer operation must be an integer

The result of a decimal operation must be a decimal

The result of integer and decimal operations, also a decimal

 

But sometimes, we want the result of integer operation to be a decimal, such as the following example

I had a dinner with my friends and it cost 25 yuan. Please help us to calculate how much each person will pay for the 1 words of the AA system. We prefer to get 12.5 yuan per person instead of 12 yuan, right?

So, in order to get the result to be a decimal, our dividend needs to be declared as a decimal, such as

 

We know that many of the mathematical formulas we have learned are relatively complex. How should we express them in the computer? For example a=(b+c)*d

In fact, in Java, like the mathematics we know, operations have priority, and the priority of multiplication and division is higher than that of addition and subtraction.

Similarly, we can also use parentheses to express mandatory precedence in Java, such as

 

Alright, let's see what new skills we've learned in this lesson

First of all, we know that in Java, variables of numeric type can be stored. The most commonly used integer is int, and the most commonly used decimal is double

Secondly, we know that in Java, four arithmetic operations and remainder operations can be performed

And, we know that the result of integer operation must be an integer; as long as there is a decimal involved in the operation, the result must be a decimal

Finally, we can use parentheses to express forced precedence operations, just as we do in mathematics

 

So far, we have learned how to write a simple Java program, how to print and get user input,

How to store characters and numbers, and learn how to perform number operations. In fact, Java can do it, there are many more, let's see you in the next class, byebye

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326987023&siteId=291194637