1024programmer Java JavaScript-Can the data in the stack be modified? For example, can numerical data be modified?

JavaScript-Can the data in the stack be modified? For example, can numerical data be modified?

var str = "abc";
 str[0]; // "a"
 str[0] = "d";
 str; //Still "abc"

The string is immutable so it cannot be modified

Can the data in the stack be modified?
For example, num=10;num=11;
What is changed is the value 10 in the stack is changed to 11,
or is a new one created in the stack? num=11, delete the original memory?

Reply content:

var str = "abc";
 str[0]; // "a"
 str[0] = "d";
 str; //Still "abc"

The string is immutable so it cannot be modified

Can the data in the stack be modified?
For example, num=10;num=11;
What is changed is the value 10 in the stack is changed to 11,
or is a new one created in the stack? num=11, delete the original memory?

My understanding is

var str = "abc";//Created a primitive value string type in js, and the primitive value is immutable
 str[0] = "d";//This sentence is a typical syntax for referencing object attributes. We can understand it this way. str is actually immutable, but if you do
 Writing will cause strTemp = new String(str); to be created, that is, a temporary object is created and assigned strTemp[0] = "d";
 Destroy immediately afterwards.
 That is to say, you have to remember that the original value in js is immutable.
 Objects are mutable.  
Let’s put it this way, let me use strings as an example:
 var str = "Hello";//"Hello" is a literal, primitive value (Primitive Type). This is how I understand it. There is a piece of memory to store "Hello", and str is just a variable name, used to reference this piece.  memory, use it.

 var str = "World";//This sentence actually only changes the position of the reference, because the original value remains unchanged, and if the "Hello" above is not used, the automatic garbage processing of js will be recycled.  

If you still don’t understand, I’ll try my best——>The secret of js original value

Make progress together.

Script strings are immutable.

  1. The pointer [0] in str[0] = “d”; can read its value.

Supplement:

None of the methods defined by the String class can change the content of the string.  Methods like String.toUpperCase() return a completely new string instead of modifying the original string.  

Suggestions:

The above information comes from http://www.w3school.com.cn/jsref/jsref_obj_string.asp
 If you encounter a problem that you feel is relatively basic, check the documentation.
 

Come on!

Should be newly created

Memory distribution includes heap, stack and constant pool. The space of the heap is larger than that of the stack. The heap usually contains objects, functions and the like. The declared variables are placed in the stack, and the constant pool generally contains regular characters and numbers. So what I would like to add is that some things are stored in different locations than you said.

The string type in Javascript is borrowed from the string type in Java. Once the string literal is declared, it cannot be changed

There is a hermit conversion inside

Variable (value)→Memory
In numerical and Boolean variables, (value) is a numerical value
In strings, objects, arrays, etc., (value) is an address pointing to memory. What is compared during comparison is (value).
When you reassign a variable, you change the (value), and when you assign a variable to another variable, you transfer (copy) the (value) to another variable.

In the picture
n=10;n=11;n remains unchanged, what changes is (value).
b=n; value is passed, n remains unchanged, b=10.
str= “aa”; str=”bb”; str remains unchanged, (value) changes to point to another string.
str2=”aa”, str2 is different from str, it does not point to the original “aa” but a new one “aa”.
str3=str2; The (value) of str3 is the same as str2, pointing to the same string, equivalent.

Strings are copied and passed by reference and compared by value
Numbers and Boolean values ​​(true and false) are copied, passed and compared by value
Objects, arrays and Functions are copied, passed, and compared by reference

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/719559

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索