Are Java 'pointers' integers? - java

I am taking data structures and analysis. We have gone over how assignment and comparisons of object types is much slower than assignment and comparisons for basic types, such as int.
I recall learning C (all those almost thirty years ago) and how pointers in C are (or were) integer calls. Is Java similar today, is a reference to an instanced object internally an integer address of memory, and as such are comparisons such as
if (MyObject != null) {...}
an integer operation within the framework?
I hope my question is clear. I've been researching around and I can't find a clear answer of how Java manages its dynamic memory.

The short answer is yes, a reference to an object is stored as pointer like it is in C.
I am not sure what you mean by stored as "integer", but if what you want is to do some operation on them like you can do in C (e.g. add integer to a pointer, etc.), then you can't with Java.
For the rest, it's pretty much the same except that it is handled by Java.
JLS 4.3.1 specifies that a reference to an object is stored as pointer:
The reference values (often just references) are pointers to these
objects, and a special null reference, which refers to no object.

Related

How "==" works on primitive types

I wonder how "==" operator works on primitive values. I understand that "==" checks if two references refers to the same object on heap.
But how it works in context of primitive values wihich are stored on stack?
e.g
int a = 5;
int b = 5;
I assume that these values aren't stored in the same place in memory but
a == b returns "true".
My hypotesis is that JVM treats all values stored in stack as stored in one place in memory and it returns true in case of "==".
Could you explain me this matter in a little more detailed way?
Regards
Your hypothesis is wrong. When comparing primitives there are no memory addresses in play. It's a simple instruction to compare whether a value is equal to another value, implemented in bytecode as a comparison (of a register value) and a conditional jump.
Edit: As pointed out by James and Varun, my original answer would not apply to the OP's example since an object reference is a numerical value just like an integer. So the comparison operation would actually be the same in this case.
In other languages there is something called operator overloading. Java does not support user operator overloading, but understanding how those work will give you a better idea how the same operator can use different logic depending on the context.
when you are comparing a variable and a primitive, the machine code that is produced is performing a different comparison operation than when comparing two objects. The same syntax is used because the idea of comparison is logically similar enough to warrant using the same operator symbol rather than defining a completely different symbol for the two different functionalities.
For any comparison operation, JVM looks for data type of the operands. Depending on the operand type different Java byte code instructions are used for comparison.
JVM works on two kinds of data types.
According to JVM 7 specification:
Like the Java programming language, the Java Virtual Machine operates
on two kinds of types: primitive types and reference types. There are,
correspondingly, two kinds of values that can be stored in variables,
passed as arguments, returned by methods, and operated upon: primitive
values and reference values.
References too are values of type reference.
The Java Virtual Machine contains explicit support for objects. An
object is either a dynamically allocated class instance or an array. A
reference to an object is considered to have Java Virtual Machine type
reference. Values of type reference can be thought of as pointers to
objects. More than one reference to an object may exist. Objects are
always operated on, passed, and tested via values of type reference.
For arithmetic operations on primitive and reference types, JVM uses opcodes which specify data type of the arguments along with operation.
For example,
lcmp - Compare two long values
Finally, for JVM any arithmetic operation is byte arithmetic on given operands.

What are Value Types from Project Valhalla?

I've started reading about Project Valhalla and there is something I really don't understand and it's the Value Types.
This is what I understand:
1) Are objects that can't be compare it as reference?
final ValueType a = new ValueType();
final ValueType b = a;
System.out.println(a==b); returns false????
In Google AutoValue code example, it states
if(o == this){return true;}//equals method implementation what is this? I am comparing references here right?
2) According to Wikipedia, highly-efficient small 'objects' without inheritance. What do Small Objects? and Without inheritance mean?
Is this not possible using VT?
public final class ValueType extends Any //is this not possible??
3) Why are they used? Which scenario would be used and how would it be used.
4) According to Google AutoValue Library, in a nutshell, a value-typed object is an object without an identity, i.e. two value objects are considered equal if their respective internal state is equal. My question is: do they have state and should they implement equals and hashcode. What does object without an identity mean?
5) Is this assertion correct?
public static void main(final String[] args)
{
final Test clazz = new Test();
final AutoValue value = new AutoValue("Java Belongs to SUN");//Constructor Name
clazz.mutate(value);
System.out.println(value.getName()); //would print: Java Belongs to SUN??
}
private void mutate(final AutoValue value){value.setName("Java now is part of Oracle Corporation");return;}
If it is so, would JVM gain memory not tracking this Objects or Values between methods calls?
Project Valhalla is part of initial Project of Java 10 would be ready in 2018 or so.
Your final assertion is correct. The ValueType variables are entirely copied when passing them as a parameter to a function, rather than typically just getting a copy of the reference to an object. This allows you to treat a small object as if it were a value type like int or boolean.
1) Under Project Valhalla, two ValueTypes would be compared by fields directly, even for == checks, much like primitive types. With Google's AutoValue types, you would never use == directly, because that would still be an identity check.
2) Small Objects means that this should be used for objects that only have a few fields, as the whole content of the object is going to be copied repeatedly. Large objects would be better served with passes by reference.
Without Inheritance means that you won't be able to use polymorphism for Value Type objects. Because Value Types are meant to be stored directly, like primitive values, they don't include any class information, so the JVM must always be able to infer what the object is from the program itself, instead of from any information on the object. For example, an Integer field could be a Value Type member, while a Number field would have to still be by reference.
3) They are used to avoid the dereference penalty normally required for accessing an object's members. For example, with a List of Points, each Point is actually a reference to the x and y values in memory, so iterating over the list will involve many dereferences. If the Points were stored directly in the list, this would be avoided.
4) Object Without an Identity means that all that matters about the object is its value. Just as an int valued 1 should be the same as all other ints valued 1, and all Strings "hello world" are equal to all other Strings "hello world", whether or not they are actually the same object. By contrast, two ArrayLists that are both empty, while at the time equal, have identity, because they are mutable, and adding an element to one list must be distinct from adding an element to the other list.
5) Under Project Valhalla, the AutoValue, if it is a Value Object, would be immutable, so there would be no setName method to call. It would be similar to how you can never mutate 1 into 2, you instead modify where a 1 is located so that a 2 is there instead.
Source: http://cr.openjdk.java.net/~jrose/values/values-0.html
The other answers are fine, but there is one other perspective about the core point of value objects: they give you stack semantics.
Meaning: before project Valhalla, you either have primitive "objects" and
reference objects. The first ones can exist on the stack, but real objects only live on the heap.
Value objects will change that. You can have "real" objects - but their data only resides on the stack. This means that you do not have a reference (and therefore cost for de-referencing) anything - just like primitive types, the value is directly placed on the stack. But now that value can be more than just a single int, long, ... - you can have a real, "complex" object - but all its data is directly there on the stack.
And because of that, you can nicely do a == b now - because now you are no longer comparing references that point to the heap - but a and b directly have their corresponding values.
UPDATE 2019/2020, Build 14-valhalla+4-55 (2019/8/30)
Inline Class is the new name of "Value Type". According to current project state Inline Classes don't support inheritance but are able to extend interfaces. Following statement is legal:
public inline class MyInlineClass extends SomeJavaInterface {}
They do have equals, hashcode (and toString) methods. "==" operation and equals (default implementation) are same for simple case, where only Inline Classes and primitive fields are involved. But it is not true in general.
It becomes messy, if you add a classic Java Object (including String and Array) as a field inside of Inline Class. In this case "==" operation most probably won't work as expected anymore and you have to fallback to classic custom equal implementation.
Inline objects are immutable and don't support clone (clone makes no sense).
A possible usecase for Inline Classes would be complex numbers or matrix algebra, which is essential for neural networks algorithms of artificial intelligence systems.

Why are arrays Objects, but can not be used as a base class?

The Java language specification specifies that
In the Java programming language arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.
So, considering arrays are objects — why did the Java designers make the decision not to allow inherit and override from it, for example, toString() or equals()?
The current syntax wouldn't allow creating anonymous classes with an array as the base class, but I don't think that was the reason for their decision.
Java was a compromise between non-object languages and very slow languages of that time where everything was an object (think about Smalltalk).
Even in more recent languages, having a fast structure at the language level for arrays (and usually maps) is considered a strategic goal. Most people wouldn't like the weight of an inheritable object for arrays, and certainly nobody wanted this before JVM advances like JIT.
That's why arrays, while being objects, weren't designed as class instances ("An object is a class instance or an array"). There would be little benefit in having the ability to override a method on an array, and certainly not a great-enough one to counterbalance the need to check for the right method to apply (and in my opinion not a great-one enough to counterbalance the increased difficulty in code reading, similar to what happens when you override operators).
I came across UNDER THE HOOD - Objects and arrays which explains almost anything you need to know about how JVM handles arrays. In JVM, arrays are handled with special bytecodes, not like other objects we are familiar with.
In the JVM instruction set, all objects are instantiated and accessed
with the same set of opcodes, except for arrays. In Java, arrays are
full-fledged objects, and, like any other object in a Java program,
are created dynamically. Array references can be used anywhere a
reference to type Object is called for, and any method of Object can
be invoked on an array. Yet, in the Java virtual machine, arrays are
handled with special bytecodes.
As with any other object, arrays cannot be declared as local
variables; only array references can. Array objects themselves always
contain either an array of primitive types or an array of object
references. If you declare an array of objects, you get an array of
object references. The objects themselves must be explicitly created
with new and assigned to the elements of the array.
Arrays are dynamically created objects, and they serve as a container that hold a (constant) number of objects of the same type. It looks like arrays are not like any other object, and that's why they are treated differently.
I'd like to point out this article. It seems as though arrays and objects follow different opcodes. I can't honestly summarize it more than that however it seems, arrays are simply not treated as Objects like we're normally used to so they don't inherit Object methods.
Full credits to the author of that post as it's a very interesting read, both short & detailed.
Upon further digging into the topic via multiple sources I've decided to give a more elaborate version of my previous answer.
The first thing to note that instantiation of Objects and Arrays are very different within the JVM, their follow their respective bytecode.
Object:
Object instantiation follows a simple Opcode new which is a combination of two operands - indexbyte1 & indexbyte2. Once instantiated the JVM pushes the reference to this object onto the stack. This occurs for all objects irrespective of their types.
Arrays:
Array Opcodes (regarding instantiation of an array) however are divided into three different codes.
newarray - pops length, allocates new array of primitive types of type indicated by atype, pushes objectref of new array
newarray opcode is used when creating arrays that involve primitive datatypes (byte short char int long float double boolean) rather than object references.
anewarray - pops length, allocates a new array of objects of class indicated by indexbyte1 and indexbyte2, pushes objectref of new array
anewarray opcode is used when creating arrays of object references
multianewarray - pops dimensions number of array lengths, allocates a new multidimensional array of class indicated by indexbyte1 and indexbyte2, pushes objectref of new array
multianewarray instruction is used when allocating multi-dimensional arrays
Object can be a class instance or an array.
Take from Oracle Docs
A class instance is explicitly created by a class instance creation expression
BUT
An array is explicitly created by an array creation expression
This goes hand in hand with the information regarding the opcodes. Arrays are simply not developed to be class interfaces but are instead explicitly created by array creation expression thus naturally wouldn't implicitly be able to inherit and/or override Object.
As we have seen, it has nothing to do with the fact that arrays may hold primitive datatypes. After giving it some thought though, it isn't very common to come across situations where one might want to toString() or equals() however was still a very interesting question to try and answer.
Resources:
Oracle-Docs chapter 4.3.1
Oracle-Docs chapter 15.10.1
Artima - UnderTheHood
There are many classes in standard java library that you cannot subclass, arrays aren't the only example. Consider String, or StringBuffer, or any of the "primitive wrappers", like Integer, or Double.
There are optimizations that JVM does based on knowing the exact structure of these objects when it deals with them (like unboxing the primitives, or manipulating array memory at byte level). If you could override anything, it would not be possible, and affect the performance of the programs very badly.

Why Java does not allow null while declaring primitive data types [duplicate]

This question already has answers here:
Why can't primitive data types be "null" in Java?
(8 answers)
Closed 9 years ago.
This is in continuation to my previous question and accroding to answers of this question Declaration of wrapper classes
Java wraps primitive data type to wrapper classes then why
char c = null; // invalid
int i = null; // invalid
is not allowed but
Character cObj = null; // valid
Integer iObj = null; // valid
is allowed.
Because primitives represent value and Object variables represent references (something like pointers) to complex data objects. There is no null value general, it is a special keyword that "references to nothing" or empty reference - this is highly unprofessional answer, but I guess it will be most appropriate.
Besides, what could be in your opinion, numerical value of null? 0? -1? But, those are valid integers so what else?
I strongly suggest that you start familiarizing yourself with the following complex java tutorial. Every aspect that you have been asking about is explained there and supported with examples.
null means "lack of an object". References can lack an object, primitives can't.
Java primitive type variables are store-by-value instead of store-by-reference variables. Where as the wrapper classes are objects basically like any other Java object except that all they do is wrap a primitive type.
The reference values (often just references) are pointers to these
objects, and a special null reference, which refers to no object.
As per jls-4.3.1, it is pointless to take about the null reference without the existence of an object.
Along with all above answer i would like to add this point too.
For primitive types,we have fixed memory size i.e for int we have 4 bytes and char we have 2 bytes. And null is used only for objects because there memory size is not fixed.
So by default we have,
int a=0;
and not
int a=null;
Same with other primitive types and hence null is only used for objects and not for primitive types.
Objects like Character and Integer are pointers: the actual number stored in the bytes that are that variable's value represents an address in memory for the rest of the JVM's memory. So, it is possible and meaningful to set that number to an address that goes nowhere, which is what null is.
A primitive like int or char, however, has a number that is interpreted as a number (integer, or ASCII code), and there's no way to make it "not a number" because all that the memory can possibly store is numbers.
Referring to unboxing/autoboxing, you have to imagine them like are two ways that the compiler adopt to save you from going mad with continuos "casting" from primitive to object and vice-versa, but they are not flawless.
What happens if your Integer wrapper is null and you do a division? Not a division by 0 but a Null pointer exception, because java cannot unbox a not referenced object!
So it's safe and logical to keep different init rules for primitives and objects.

Why can't we use pointers in Java? [duplicate]

This question already has answers here:
Does Java have pointers?
(12 answers)
Closed 9 years ago.
Why we are not using the pointer here?
Which concept is used instead of pointer in Java?
Why can't we use pointers in Java?
Because the language designers chose not to include pointers in the language.
Why we are not using the pointer here.
Because the designers of Java thought it was a tricky-to-use and error prone construct.
Which concept is used instead of pointer in Java?
References (which are quite similar to pointers if you disregard from pointer arithmetic).
Keep in mind that all objects you create, you create on the heap (using the new keyword). This fact, together with the fact that there is no "dereference operator" (* in C/C++) means that there's no way to get hold of an object! Since you can't get hold of an object, there's no way you can store an object in a variable. Therefor all variables (except the ones holding primitive types) are of reference-type.
It was a language design decision.
From the sun white paper The Java Language Environment:
Most studies agree that pointers are one of the primary features that
enable programmers to inject bugs into their code. Given that
structures are gone, and arrays and strings are objects, the need for
pointers to these constructs goes away. Thus, Java has no pointer data
types. Any task that would require arrays, structures, and pointers in
C can be more easily and reliably performed by declaring objects and
arrays of objects. Instead of complex pointer manipulation on array
pointers, you access arrays by their arithmetic indices. The Java
run-time system checks all array indexing to ensure indices are within
the bounds of the array.
You no longer have dangling pointers and trashing of memory because of
incorrect pointers, because there are no pointers in Java.
It's good isn't it? You don't have pointers in the C programming sense, the Virtual Machine looks after all that. However, that's not to say you don'f have have similar capabilities. For example, variables for objects are references, when you pass a variable for an object you are passing a reference which is sort of a pointer. You might also use a variable to store the index of an item in an array, that again is a kind of pointer in a programmatical sense.
Everything in Java is accessed only through pointers. Java references are pointers. The creator of java Dr.Gosling says this in his book, "The Java Language Specification" (somewhere around p.40 in one edition).
In fact another book "The Java Virtual Machine" says: "A reference is a pointer to a pair of pointers; one of theses two pointers points to another pointer [...]".
There are marked differences between Java references and C pointers but nevertheless references are pointers.
In Java any reference to a simple type is always by value and any reference
to an object is always a pointer except in cases of special objects.
every object you create using new(); is actually pointer..
XXX a=new XXX();
a is actually a pointer..
It's just you cannot do fancy thing like pointer arithmetic to them like you can in C (for safety reasons).
so actually if you are passing an object, you are passing the pointer to the object (object created by new() resides in heap).
You can access pointer mechanics using java.misc.unsafe, but that's... mmm... unsafe.
Ah, about replacing concept. What aspect are you interested in? Variables are already represented by references to them - that's one point of view. If you need pointers to implement efficient data structures, you, most probably already have one, or there is a library providing that feature, or you can write some native code and call it from java wrapper. Anyway, please define aspects you are interested in and maybe community will give you more detailed answer.
Java follows the principle of call by value. So if you create an object, you will get back a copy of a reference to the created object in the heap space. This copy of a reference is what a variable gets assigned to. If you then use this variable as an argument for a method call, again a copy of that reference is created and used instead. Java never uses references or pointers! Everything in java is a copy. Hence Java follows the call by value principle.
The most difficult thing to manipulate in any programming language is with pointers.
If u just miss something you get lots of errors namely segmentation faults.
Java is one such language providing users the flexibility to do things using CALL BY VALUE .
If you want to change the value of a variable in a C function you pass the address (pointer) of the variable to the function. If you want to use the value of a variable in a function and do not wish to change its value, you pass the name (reference) of the variable to the function. Many programmers never used pointers for any thing else, like linked lists and b trees. C pointers and structures can make some applications infinitely easier. Structures unlike arrays can contain different types of variables and grouping them together makes life easier. Structures in a linked lists contain pointers (addresses to the next link in the list and also to the previous link if the list is double linked. This can make sorting a breeze.
A link in the list can also contain pointers to different structures which allow you to associate the linked lists of different materials or conditions with each link in the first list.
Since the memory is allocated where and as needed rather than being dimensioned as in an array, it gives you tremendous flexibility.The main application I have used this for is calculating the cost of projects to bid jobs.
This flexibility demands close attention to details and no holds barred debugging.

Categories

Resources