基础知识:Java中的数据保存与传递
In python,we have something like the list, say L[1,2,3]. We can also add the last digit to the list by doing so:L.append(i)
However, in JAVA, we do not have a built-in List Type. And in this chapter, I am going to build my own list from scratch.
Before doing so, i need to list some basic knowledge of java.
First, how are the information stored in our computer?
All information in your computer is stored in memory as a sequence of ones and zeros. Some examples:
- 72 is often stored as 01001000
- 205.75 is often stored as 01000011 01001101 11000000 00000000
- The letter H is often stored as 01001000 (same as 72)
- The true value is often stored as 00000001
One interesting observation is that both 72 and H are stored as 01001000. This raises the question: how does a piece of Java code know how to interpret 01001000?
The answer is through types! For example, consider the code below:
char c = 'H';
int x = c;
System.out.println(c);
System.out.println(x);
If we run this code, we get:
H
72
In this case, both the x and c variables contain the same bits (well, almost...), but the Java interpreter treats them differently when printed.
There are 8 types in Java in total, and they are byte, short, int, long, float, double, boolean, char.
Other types, including the array, are called the reference types.
Second, what are the theories of declaring variables?
When you declare a variable of a certain type, Java finds a contiguous block with exactly enough bits to hold a thing of that type. For example, if you declare an int, you get a block of 32 bits. If you declare a byte, you get a block of 8 bits. Each data type in Java holds a different number of bits. The exact number is not terribly important to us in this class.
Below is a picture of showing the process of declaration:
Java does not write anything into the reserved box when a variable is declared. In other words, there are no default values. As a result, the Java compiler prevents you from using a variable until after the box has been filled with bits using the =
operator. For this reason, I have avoided showing any bits in the boxes in the figure above.
When you assign values to a memory box, it is filled with the bits you specify. For example, if we execute the lines:
x = -1431195969;
y = 567213.112;
Then the memory boxes from above are filled as shown below, in what I call box notation.
The Golden Rule of Equals (GRoE):
When you write x=y in the Java program, you are telling the Java interpreter to copy the bits from x to y.
The reference variable declaration
Above, we said that there are 8 primitive types: byte, short, int, long, float, double, boolean, char. Everything else, including arrays, is not a primitive type but rather a reference type
.
When we declare a variable of any reference type (Walrus, Dog, Planet, array, etc.), Java allocates a box of 64 bits, no matter what type of object.
It is a little bit strange, since an array can definitely include information that exceeds 64 bits.
However, this problem is easily resolved with the following piece of information: the 64 bit box contains not the data about the stored information, but instead the address of that in memory.(pointer)
As an example, suppose we call:
Walrus someWalrus;
someWalrus = new Walrus(1000, 8.3);
The first line creates a box of 64 bits. The second line creates a new Walrus, and the address is returned by the new
operator. These bits are then copied into the someWalrus
box according to the GRoE.
If we imagine our Walrus weight is stored starting at bit 5051956592385990207
of memory, and tuskSize starts at bit 5051956592385990239
, we might store 5051956592385990207
in the Walrus variable. In binary, 5051956592385990207
is represented by the 64 bits 0100011000011100001001111100000100011101110111000001111000111111
, giving us in box notation:
We can also assign the special value null
to a reference variable, corresponding to all zeros.
Box and Pointer Notation
Just as before, it's hard to interpret a bunch of bits inside a reference variable, so we'll create a simplified box notation for reference variable as follows:
- If an address is all zeros, we will represent it with null.
- A non-zero address will be represented by an arrow pointing at an object instantiation.
This is also sometimes called "box and pointer" notation.
For the examples from the previous section, we'd have:
Parameter Passing
When you pass parameters to a function, you are also simply copying the bits. In other words, the GRoE also applies to parameter passing. Copying the bits is usually called "pass by value". In Java, we alwayspass by value.
Instantiation of Arrays
As mentioned above, variables that store arrays are reference variables just like any other. As an example, consider the declarations below:
int[] x;
Planet[] planets;
Both of these declarations create memory boxes of 64 bits. x
can only hold the address of an int
array, and planets
can only hold the address of a Planet
array.
Instantiating an array is very similar to instantiating an object. For example, if we create an integer array of size 5 as shown below:
x = new int[]{0, 1, 2, 95, 4};
Then the new keyword creates 5 boxes of 32 bits each and returns the address of the overall object for assignment to x.
for more information, please view the website:https://joshhug.gitbooks.io/hug61b/content/chap2/chap21.html
new
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通