Question

In: Computer Science

How are primitive and reference types different an examples of how copying works differently for these...

How are primitive and reference types different an examples of how copying works differently for these two types.

Solutions

Expert Solution

Primitives vs. References

  • primitive types are the basic types of data
    • byte, short, int, long, float, double, boolean, char
    • primitive variables store primitive values
  • reference types are any instantiable class as well as arrays
    • String, Scanner, Random, Die, int[], String[], or any objects.
    • reference variables store addresses

Assignment

  • copies the contents of RHS variable into LHS variable
    • primitives: the primitive value is copied
    • references: the address is copied
  • implications: for references the object is not copied, it is shared (reference variables are aliases)

Example

  • primitive types
    • int x = 10;
    • int y=x;

This above statement copies the value stored in x(that is 10) to y.

  • reference types
    • int x[] = {1,2,3,4,5};
    • int y[] = x;
    • y[3] = 7;

The above statement assigns the array x to values {1,2,3,4,5} and then we copy y[] = x, this does not copy the entire array but y is given as a reference of the memory represented by x.

So in the next line when we assign y[2] = 7, it is also reflected in x as we are using the same memory for both the arrays. Generally programming languages comes with specific utilities if we need to copy the values in the array.

If we print x[] and y[] after the y[2]=7 statement, we will find both x and y are same.


Related Solutions

Explain the differences between primitive and reference data types. Define the scope of a variable (hint:...
Explain the differences between primitive and reference data types. Define the scope of a variable (hint: local and global variable) Why is initialization of variables required. Differentiate between static, instance and local variables. Differentiate between widening and narrowing casting in java. the following table shows data type, its size, default value and the range. Filling in the missing values. TYPE SIZE (IN BYTES) DEFAULT RANGE boolean 1 bit true, false Char 2 ‘\0000’ to ‘\ffff’ Byte 0 Short 0 -215...
6. Know the examples of different types of unemployment 7. What are the different types of...
6. Know the examples of different types of unemployment 7. What are the different types of wages?
How can corporate culture be changed? Give examples. (250 words) No Plagiarism (copying) please.
How can corporate culture be changed? Give examples. (250 words) No Plagiarism (copying) please.
clarify by examples please Clarify how the Earth materials from the different types of rocks and...
clarify by examples please Clarify how the Earth materials from the different types of rocks and soils may affect positively or negatively the engineering works, by giving examples.
What is the difference between primitive types and abstract datatypes?
What is the difference between primitive types and abstract data types?
With reference to real world examples assess the pros and cons of different exchange rate systems....
With reference to real world examples assess the pros and cons of different exchange rate systems. In your answer you should refer to: Floating exchange rate regimes – you should in particular consider whether floating currencies are conducive to promoting international trade. Pegged exchange rate regimes and pegged with bands exchange rate regimes – you should consider the possibility of currency crises in relation to the pegged with bands currency regimes and should consider an actual currency crisis such as...
What are the different types of decisions taken in an organization? Give examples.
What are the different types of decisions taken in an organization? Give examples.
Describe different types of credit market instruments and provide examples
Describe different types of credit market instruments and provide examples
Describe different types of data structures in Computer Science with examples.
Describe different types of data structures in Computer Science with examples.
1. how is gram stain done? 2. What types of bacteria are stained differently in the...
1. how is gram stain done? 2. What types of bacteria are stained differently in the Gram stain? 3. What is the key step in the Gram stain that distinguishes between two different cell types? Explain why. The laboratory reported gram-negative bacteria arranged inside the leukocytes of the pus sample. Other tests confirmed the pathogen was Neisseria gonorrhoeae, indicating Tom had the sexually transmitted infection gonorrhea. 4. Describe the shape of the gram-negative bacteria shown in the photo. Dr. Nelson...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT