Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java make distinction between value type and reference type

Tags:

C# makes distinction of those two. Does java do the same or differently?

like image 776
user496949 Avatar asked Mar 05 '11 02:03

user496949


People also ask

What is the difference between reference type and value type in Java?

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.

Does Java have reference types?

Following are the reference types in Java. class types − This reference type points to an object of a class. array types − This reference type points to an array. interface types − This reference type points to an object of a class which implements an interface.

Is Java assignment by value or reference?

Java is officially always pass-by-value. The question is, then, “what is passed by value?” As we have said in class, the actual “value” of any variable on the stack is the actual value for primitive types (int, float, double, etc) or the reference for reference types.

What is the difference between reference type and primitive type in Java?

Variables in Java are classified into primitive and reference variables. From the programmer's perspective, a primitive variable's information is stored as the value of that variable, whereas a reference variable holds a reference to information related to that variable.


2 Answers

In Java, all objects and enums are reference types, and all primitives are value types. The distinction between the two is the same as in C# with respect to copy semantics, but you cannot define a new value type in Java.

like image 75
ide Avatar answered Oct 05 '22 01:10

ide


In Java, the primitives are value types, classes and arrays are reference types.

like image 27
Brandon Frohbieter Avatar answered Oct 05 '22 03:10

Brandon Frohbieter