Thinking logic of computer programs (2), thinking Logic

Source: Internet
Author: User

Thinking logic of computer programs (2), thinking Logic

Assignment

In the previous section, we talked about data types and variables. By declaring variables, each variable is given a data type and a meaningful name, and we tell the computer the data we want to operate on.

With data, we can do a lot of operations. However, this article only describes the first operation on data: assigning values. After the variable is declared, a location is allocated in the memory, but the content of this location is unknown. The value assignment is to set the content of this location to a definite value.

In Java, the assignment of basic types, arrays, and objects is significantly different. This article introduces the assignment of Basic Types and arrays. Subsequent articles on objects will be detailed.

Let's first assign values to the basic type, and then assign values to the array.

Assignment of Basic Types

Integer type

The integer types include byte, short, int, and long, respectively occupying 1/2/4/8 bytes. The values are as follows:

Type name Value Range
Byte -2 ^ 7 ~ 2 ^ 7-1
Short -2 ^ 15 ~ 2 ^ 15-1
Int -2 ^ 31 ~ 2 ^ 31-1
Long -2 ^ 63 ~ 2 ^ 63-1

We use ^ to represent the exponent. 2 ^ 7 is the 7th Power of 2. We don't need to remember this scope clearly. We can get a general understanding of the scope. In most daily applications, we can use int. In subsequent articles, we will further analyze the reason why the range is like this from the binary perspective.

The assignment method is simple. You just need to assign a value to a variable in the form of a familiar numeric constant. The value of the corresponding memory space has never been changed to a definite constant. However, constants cannot exceed the representation range of the corresponding type. For example:

byte b = 23;short s = 3333;int i = 9999;long l = 32323;

However, when assigning values to the long type, if the constant exceeds the int expression range, you need to increase the write or lower-case L after the constant, that is, L or l. For example:

long a = 3232343433L;

This is because the numeric constant is of the int type by default.

Decimal type

Float and double decimal types occupy 4 and 8 bytes of memory respectively, with different value ranges and precision. double indicates a larger range and higher accuracy. Specifically:

Type name Value Range
Float

1.4E-45 ~ 3.4E + 38

-3.4E + 38 ~ -1.4E-45

Double

4.9E-324 ~ 1.7E + 308

-1.7E + 308 ~ -4.9E-324

The value range looks strange. Generally, we don't need to remember it. We can just make a rough impression. E indicates the base-10 index, and the plus (+) and minus (-) values after E represent the positive and negative indexes. For example, 1.4E-45 indicates that the power of 1.4 multiplied by 10-45. Subsequent articles will further analyze the binary representation of decimal places.

For double, you can directly assign the familiar decimal representation to the variable. For example:

double d = 333.33;

However, for float, F or lower-case f needs to be added after the number, for example:

float f = 333.33f;

This is because the decimal constant is of the double type by default.

In addition to decimals, you can also assign Integers to float or double. For example:

float f = 33;double d = 3333333333333L;

Boolean Type

This is simple. Values are directly assigned with true or false values, indicating true and false, for example:

boolean b = true;b = false;

Character Type

The character type char is used to indicate a character. This character can be a Chinese character or an English character. In memory, Java uses two bytes to represent a character. Constant characters are enclosed in single quotes when values are assigned. Do not use double quotation marks. For example:

Char c = 'a'; char z = '中 ';

There are some details about character types, which will be further parsed in subsequent articles.

Notes

The assignment above is to directly set a constant value for the variable. However, you can also assign a variable to a variable, for example:

int a = 100;int b = a;

You can perform various operations on variables (as described in subsequent articles) or assign the calculation results of variables to variables, for example:

Int a = 1; int B = 2; int c = 2 * a + B; // 2 times the value of a and then the value of B is assigned to c.

The assignment values described above are all assigned values when the variable is declared, but this is not necessary. You can declare the variable first and then assign values.

Array type

Assignment syntax

An array of basic types has three assignment modes:

1. int[] arr = {1,2,3};2. int[] arr = new int[]{1,2,3};3. int[] arr = new int[3];    arr[0]=1; arr[1]=2; arr[2]=3;

Both the first and second types know the content of the array in advance, while the third type allocates the length first and then assigns a value to each element.

In the third form, each element has a default value even if no value is assigned to each element. This default value is related to the array type. Values of the numeric type are 0, boolean is false, and char is null.

The array length can be dynamically determined as follows:

Int length =...; // calculate int arr = new int [length] dynamically based on some conditions;

Although it can be determined dynamically, it cannot be changed after it is set. The array has a length attribute, but it can only be read and cannot be changed.

A small detail cannot specify the length of an initial value, that is, the following format is not allowed:

int[] arr = new int[3]{1,2,3}

This is understandable, because the initial value has determined the length and the length is given again. If they are not consistent, the computer will be at a loss.

Differences between arrays and Basic Types

For a basic type variable, only one corresponding memory space exists in the memory. However, there are two arrays, one for storing the content of the array and the other for storing the content location.

An example is used to illustrate that there is an int variable a and an int array variable arr. Its code, memory address and memory content corresponding to the variable are as follows:

Code Memory Address Memory Data
Int a = 100; 1000 100
Int arr = {1, 2, 3 }; 2000 3000
  3000 1
  3004 2
  3008 3

The memory address of basic type a is 1000, which stores the value 100.

The memory address of array-type arr is 2000. The value stored in this location is, and the actual data is stored at the starting position.

Why should I use two spaces for an array?

Can't I use only one space? Let's look at the following code:

int[] arrA = {1,2,3};int[] arrB = {4,5,6,7};arrA = arrB;

In this Code, the initial length of arrA is 3, the length of arrB is 4, and then the value of arrB is assigned to arrA. If arrA's memory space is directly stored as an array, it does not have enough space to accommodate all the elements of arrB.

Using two storage spaces, this is much simpler. The value stored by arrA is the same as that stored by arrB, where the address of the array content {, 6, 7} is stored, since then, access to arrA is the same as that to arrB, and the memory space of arrA {1, 2, 3} will be reclaimed due to unattended reference, as shown below:

arrA        {1,2,3} 

      \

        \

arrB  ->  {4,5,6,7}

From the above, we can also see that assigning values to array variables and assigning values to elements in the array are two different things. Assigning values to elements in an array changes the content of an array, while assigning values to an array variable directs the variable to a different position.

The length of the array cannot be changed. Immutable refers to the content space of the array. Once allocated, the length cannot be changed, but the value of the array variable can be changed, let it point to a space with different lengths, just as arrA later points to arrB in the preceding example.

Summary

Assigning a value to a variable is to set the memory space corresponding to the variable to a specific value. With the value, the variable can be loaded to the CPU, and the CPU can perform various operations on the value, the operation result can be assigned to the variable and saved to the memory.

What operations can be performed on data? How to perform operations?

------------------

Looking at the latest articles, please pay attention to the Public Account "lauma says programming" to explore the essence of Java programming and computer technology. Original article, retain all copyrights.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.