Compare and contrast in your own words primitive types with reference types in Java. Illustrate your answer using the partial code fragment below. This
Compare and contrast in your own words primitive types with reference types in
Java. Illustrate your answer using the partial code fragment below.
This code fragment is intended as an incomplete sketch of a situation that demonstrates
some differences between primitive types and reference types in Java. You
may interpret the /*...*/ comments as placeholders for any code you consider
appropriate for this purpose.
You are not required to dry run or explain every detail of this code. Use it to illustrate
points you make when comparing and contrasting primitive types with reference types.
Reword this essay below (java)
Primitive Types are the basic types of variables supported by Java (8 in number) i.e. byte, short, char, Boolean, int, long, float and double.
Primitive types generally store the value of the variable they are as a concrete value while Reference types store a reference to the variable they are dealing with. Reference type store more than just the value of the variable.
Reference types can be created using a number of primitive or primitive custom types by the programmer. Java supports both primitive and custom types.
Differences.
1. Primitive types are immutable while reference types are mutable. Primitive types usually store the value of a variable directly hence cannot be mutated (changed) internally but can be used to store a new value. However, since reference types store references to the data of the variables that they reference, their internal structure is mutable.
2. Arithmetic operations can be performed on primitive types but cannot be performed on reference types. Since primitive types are generally numeric in java, arithmetic operations such as addition and subtraction can be performed on the data stored by the variables. This cannot be done on reference types since their internal state may reference different data types that are composite.
3. Data inside primitive types can be accessed directed using the variable name/identifier but for reference types, once needs to access internal fields on a reference types individually by first accessing the composite/reference type and then accessing its internal field/variable depending on its types.
Illustration form the code:
Primitive types:
The following are the java primitive types from the code given:
a) Int
Int is a java keyword to denote an integer primitive data types.
There are three instances of the use of the primitive data types such declaration int f inside the class MyRecord, int x inside the main method of the class Main and int y as a parameter to the procedure foo of the class Main.
Reference types:
The reference types are mainly obtained from the custom classes created inside the program. The following are the reference types in the program:
a) MyRecord
There are 2 declarations of reference variables to the class MyRecord. i.e. MyRecord r and MyRecord s. Both the variables r and s reference a variable of type MyRecord and thus cannot be used as primitive data types.
b) Main
Main is also another reference type inside the program. Its usage has however not been declared but a reference from the class can be declared such as Main m.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps