TAILIEUCHUNG - Sharing Variables

Copying int Variables and Classes All primitive types such as int are called value types. When you declare an int variable, the compiler generates code that allocates a block of memory big enough to hold an integer. | Copying int Variables and Classes All primitive types such as int are called value types. When you declare an int variable the compiler generates code that allocates a block of memory big enough to hold an integer. A statement that assigns a value such as 42 to the int causes the value to be copied to this block of memory. Class types such as Circle described in Chapter 7 are handled differently. When you declare a Circle variable the compiler does not generate code that allocates a block of memory big enough to hold a Circle all it does is allot a small piece of memory that can potentially hold the address of or a reference to another block of memory containing a Circle. The memory for the Circle object itself is only allocated when the new keyword is used to create the object. This demonstrates that value types are so called because they hold values directly. Reference types such as classes hold references to blocks of memory. NOTE If you are a C or C programmer you might be tempted to think of reference types simply as pointers. While reference types in C exhibit many similarities to pointers they provide far more functionality. For example in a C or C application it is possible to make a pointer reference almost any block of memory regardless of the type of data the block is holding. Sometimes this is useful but more often than not it is the cause of many insidious programming errors. In C all references are strongly typed you cannot declare a reference variable that refers to one type such as Circle and then use the variable to access a block of memory holding a different type. There are other differences as well concerning the way in which the common language runtime manages and reclaims memory. These features are discussed in Chapter 13 Using Garbage Collection and Resource Management. Because of the different ways that they hold data value types are sometimes called direct types and reference types are sometimes called indirect types. You need to fully .

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.