Thinking Logic of computer programs (2)-Assigning values

Source: Internet
Author: User

Assign value

We talked about data types and variables, and by declaring variables, assigning a data type and a meaningful name to each variable, we tell the computer what data we want to manipulate.

With the data, we can do a lot of things. But this article is only about the first operation on the data: assign a value. After declaring a variable, the memory is allocated a piece of space, but the content of the position is unknown, and the assignment is to set the content of the location to a certain value.

There are significant differences in the assignment of primitives, arrays, and objects in Java, and this article describes the assignment of basic types and arrays, which are described in more detail in subsequent articles about objects.

Let's start with the assignment of the basic type, and then say the assignment of the array.

Assignment of the base type

Integer type

The integer types are byte, short, int, and long, each occupying 1/2/4/8 bytes, and the range of values is:

-2^7 ~ 2^7-1
short -2^15 ~ 2^15-1
-2^31 ~ 2^31-1
long -2^63 ~ 2^63-1

We use ^ to denote the exponent, 2^7 is 2 of the 7-time Square. This range we do not need to remember so clearly, there is a general understanding of the scope can be, most of the daily application, generally with an int can be. Subsequent articles will further analyze the scope of the presentation from a binary perspective.

The assignment form is very simple, the familiar numeric constant form is assigned to the variable, and the corresponding memory space value is changed from the unknown to the definite constant. However, constants cannot exceed the representation range of the corresponding type. For example:

byte b = All;  Short s = 3333; int i = 9999; long l = 32323;

However, when assigning a long type, if the constant exceeds the representation range of int, you need to increase the number of writes or lowercase l, i.e. L or L, after the constant, for example:

long a = 3232343433L;

This is because the number Changshime is considered to be of type int.

Decimal type

Decimal types have float and double, occupy a memory space of 4 and 8 bytes respectively, have different range of values and precision, double represents a larger range, more accurate, specifically:

Type name Range of values
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 range of values looks strange, and generally we don't need to remember that there is a general impression. E represents a 10-based exponent, followed by the + and-sign of E, which represents the positive and negative exponents, for example: 1.4E-45 means 1.4 times 10-45. Subsequent articles will further analyze the binary representation of decimals.

For double, assign the familiar decimal notation to the variable directly, for example:

double d = 333.33;

For float, however, you need to increase the number of letters F or lowercase f behind the numbers, for example:

float f = 333.33f;

This is because the decimal Changshime is considered to be a double type.

In addition to decimals, you can assign integers directly to a float or double, for example:

float f =; double d = 3333333333333L;

Boolean type

This is simple, use TRUE or false to assign values, respectively, to represent true and false, for example:

Boolean true  false;

Character type

The character type char is used to represent a character, which can be either Chinese characters or English character. In memory, Java uses two bytes to represent one character. When assigning a value, enclose the constant character in single quotation marks, and do not use double quotes, for example:

char c = ' A '; char z = ' Medium ';

There are some details about character types, and subsequent articles will be further deeply parsed.

Some notes

The assignment described above is to set a constant value directly to the variable. But you can also assign variables to variables, such as:

int a = +; int B = A;

Variables can be performed in various operations (subsequent articles), or they can be assigned to variables, for example:

int a = 1; int b = 2; int // 2 times the value of a plus the value of B is assigned to C

The assignment described above is assigned when declaring a variable, but it is not required, you can declare the variable first, and then assign the value.

Array type

Assignment syntax

An array of primitive types has three assignment forms, as follows:

int [] arr = {A-i}; int New int []{1,2,3}; int New int [3];    arr[0]=1; arr[1]=2; arr[2]=3;

The first and second are known in advance to know the contents of the array, and the third is to allocate the length first, and then assign values to each of these elements.

In the third form, even if each element is not assigned a value, each element has a default value, which is related to the array type. The value of a numeric type is 0,boolean to False, and Char is a null character.

The length of the array can be dynamically determined as follows:

int length = ...; // dynamic calculation based on some conditions int New int [Length];

Although it can be dynamically determined, but can not be changed after the array has a length property, but can only read, can not be changed.

A small detail that cannot be given the initial value at the same time as the fixed length, that is, the following format is not allowed:

int New int [3] {A-i}

This is understandable, because the initial value has decided the length, and then give a length, if not consistent, the computer will be at a loss.

The difference between an array and a base type

A basic type variable, there will only be a block of memory space in memory. But the array has two blocks, one for storing the contents of the array itself, and the other for storing the contents.

With an example to illustrate, there is an int variable A, and an int array variable arr, whose code, variables correspond to the memory address and memory contents as follows:

Code Memory address Memory data
int a = 100; 1000 100
int arr = {A-i}; 2000 3000
3000 1
3004 2
3008 3

The memory address of basic type A is 1000, and this location stores its value 100.

Array type the memory address of ARR is 2000, and the value stored in this location is a location where the 3000,3000 begins to store the actual data in the first place.

Why do arrays use two blocks of space

Can't you just use a piece of space? Let's look at the following code:

int [] ArrA = {n/a}; int [] Arrb = {4,5,6,7= ARRB;

In this code, the initial length of the ARRA is the length of the 3,ARRB is 4, then the value of ARRB is assigned to ARRA. If the ARRA corresponding memory space is directly stored in the array contents, then it will not have enough space to accommodate all the elements of ARRB.

With two pieces of space storage, this is much simpler, the value of the ARRA store becomes the same as the ARRB, the storage is the array content {4,5,6,7} address, after which the access Arra and ARRB is the same, and ARRA {* * * * * * * * * memory space because no reference will be garbage collection, As shown below:

ArrA {A-i}

\

\

ARRB, {4,5,6,7}

From the above, it can be seen that assigning values to array variables and assigning values to elements in an array are two different things. Assigning a value to an element in an array changes the contents of an array, and assigning a value to an array variable points to a different position.

Above we say that the length of the array can not be changed, immutable refers to the array of content space, once allocated, the length can not be changed, but the value of the array variable may be changed, so that it points to a different length of space, as in the above example Arra later pointed to ARRB.

Summary

Assigning a value to a variable is to set the memory space of the variable to an explicit value, after which the variable can be loaded into the CPU,CPU to perform various operations on the values, and the result of the operation can be assigned to the variable and saved in memory.

What can be done with the data? How do you do the math?

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

To be continued, check out the latest articles, please pay attention to the public number "old horse programming", in layman's words, explore the nature of Java programming and computer technology. Original article, All rights reserved.

Thinking Logic of computer programs (2)-Assigning values

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.