Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Variables and references in Java

48 views
Skip to first unread message

Juha Nieminen

unread,
Aug 7, 2017, 7:39:12 AM8/7/17
to
Stefan Ram <r...@zedat.fu-berlin.de> wrote:
> SG <s.ges...@gmail.com> writes:
>>Every variable of a non-primitive type in Java is a reference.
>
> In Java, the concept of a variable is not the same as the
> concept of a reference.
>
> A variable is a location of storage that has a type (JLS 8,
> 4.12). (It's like the concept of "object" in C.) If that
> type is a reference type, the variable can /store/ a
> reference( value). (JLS8, 4.1).
>
> (A reference( value) is a pointer [JLS8, 4.3.1].)

He probably meant that in Java non-primitive variables cannot be
accessed by any other means than by a reference.

(Also, a Java reference might not be directly pointing to the value
it's referring to, as is the case in C++. Quite often it will have
at least one level of indirection.)

Alf P. Steinbach

unread,
Aug 7, 2017, 11:01:19 AM8/7/17
to
On 07.08.2017 13:38, Juha Nieminen wrote:

> (Also, a Java reference might not be directly pointing to the value
> it's referring to, as is the case in C++. Quite often it will have
> at least one level of indirection.)

Practically true, but worth noting that strictly speaking it's not the
case that pointers have no indirection C++.

Consider the common Windows PC platform. With a conventional C++
implementation used to create an ordinary program, the statements

int x;
int* p = &x;

will store a logical address in `p`.

This address is resolved, when you do e.g. `*p = 42`, through two layers
of indirection. First (on the PC platform), it's treated as an offset in
a segment. Happily both the data and code segment selectors (processor
segment registers) are configured to refer to the same segment, so we
happily code away as if we have one big linear address space. Though C++
supports different address spaces for code and data.

Second, after the logical address has been resolved to a
whatchammacallit actual linear address space address, it's treated as
number of bit fields that specify page tables and offsets in these
tables. Commonly a page is a 4 KB chunk of physical memory. This is
mainly in support of virtual memory, but it's also nice for e.g. setting
execution protection flags, read-only, and such, not to mention deferred
allocation of physical memory for the machine stack (its logical address
range must be reserved up front, but actual physical memory, pages, can
be allocated automatically as needed, keeping that to a minimum).

That's a lot going on for a simple pointer dereference: accessing a
segment descriptor table, and IIRC at least two page tables, before
finally getting a physical memory address. In the course of this, memory
contents may swapped out to and retrieved from persistent storage!

So how come that you as a C++ programmer don't notice?

One doesn't notice because all that is encapsulated in the effect of
`*p`. And ditto for use of a Java reference.

It so happens that with C++ the indirection is usually only the hardware
supported one, while with Java there can be additional indirection at
the purely software level. And possibly a Java object can then be moved
around without updating more than a single pointer to it.


Cheers!,

- Alf

Mr Flibble

unread,
Aug 7, 2017, 12:28:05 PM8/7/17
to
+1

Thanks for confirming that Java uses pointers in such an informative
manner; Stuckle take note.

/Flibble

woodb...@gmail.com

unread,
Aug 7, 2017, 2:07:55 PM8/7/17
to
Java dreary. C++ interesting.


Brian
Ebenezer Enterprises
http://webEbenezer.net

Manfred

unread,
Aug 7, 2017, 3:53:25 PM8/7/17
to
On 8/7/2017 5:00 PM, Alf P. Steinbach wrote:
> On 07.08.2017 13:38, Juha Nieminen wrote:
>
>> (Also, a Java reference might not be directly pointing to the value
>> it's referring to, as is the case in C++. Quite often it will have
>> at least one level of indirection.)
>
> Practically true, but worth noting that strictly speaking it's not the
> case that pointers have no indirection C++.

I disagree: C++ (and C) pointers have no indirection as far as the
language goes, and its implementation too.

The fact is that what you refer to is not part of any language
implementation: it is how the hardware and the OS handle virtual memory.
More precisely, virtual memory is a technology that is supported by
modern CPUs (including the good old 80386 and subsequent processors)
that expose a virtual (either linear or segmented) address space to
processes, that is converted into paged (and/or swapped) memory storage
/internally/ by the processor. Support from the OS is also required to
set up all required structures and operations, including a.o. context
switching between processes (which are each assigned its virtual address
space), and swap storage management.

When I say managed /internally/ by the processor, I mean that any
userspace process code that needs to access memory does so by setting
some memory register (e.g. rdi) with an address in /virtual/ address
space, and the conversion into physical memory address is performed by
the hardware - there is no userspace process code for this. Even in the
case of a page fault, the CPU raises an exception which is handled by OS
code to load the page from swap space - this is all transparent to
userspace code.
From the Intel software developer manual:
"When an operating system or executive uses paging, the paging mechanism
is transparent to an application program. All that the application sees
is linear address space."

Not even userspace asm code can see this - in order to modify the
virtual memory system, privileged instructions are required.

This is how any program that is compiled into native machine code runs,
there is no language interaction with any of this.
By the way, virtual address resolution in the CPU is /fast/, it is
optimized directly in the silicon, since protected mode "is the native
operating mode of the processor" (from Intel's "system programming guide")

Java pointers indirection has nothing to do with virtual memory
addressing, since a Java program is hosted inside the Java VM (i.e. the
Java runtime that is the Java interpreter), an it is the Java VM that is
the actual process that runs on the CPU. Anything that happens in a Java
program happens on top of the Java runtime.
This is why I wrote in another thread that a Java reference/pointer is a
handle instead of a memory address, since it references an object that
is hosted by the runtime, and not an object that is directly allocated
in RAM as for a C and C++ object.

This may not be that much of a difference with respect to pointer
semantics (although it is in specific cases), but it is a substantial
difference in how pointer operation is implemented.

Alf P. Steinbach

unread,
Aug 7, 2017, 4:11:10 PM8/7/17
to
On 07.08.2017 21:53, Manfred wrote:
> On 8/7/2017 5:00 PM, Alf P. Steinbach wrote:
>> On 07.08.2017 13:38, Juha Nieminen wrote:
>>
>>> (Also, a Java reference might not be directly pointing to the value
>>> it's referring to, as is the case in C++. Quite often it will have
>>> at least one level of indirection.)
>>
>> Practically true, but worth noting that strictly speaking it's not the
>> case that pointers have no indirection C++.
>
> I disagree: C++ (and C) pointers have no indirection as far as the
> language goes, and its implementation too.

Well, the first part is right, the part after the comma is IMO an
unwarranted assumption.

Are you aware that C++ interpreters exist?

Did you know that there are C++ compilers for virtual machines,
including for .NET (call it CLI or whatever)?


> [snip]
> This is why I wrote in another thread that a Java reference/pointer is a
> handle instead of a memory address, since it references an object that
> is hosted by the runtime, and not an object that is directly allocated
> in RAM as for a C and C++ object.
>
> This may not be that much of a difference with respect to pointer
> semantics (although it is in specific cases), but it is a substantial
> difference in how pointer operation is implemented.

I would be interested in the “specific cases”.


Cheers!,

- Alf

Manfred

unread,
Aug 7, 2017, 5:46:06 PM8/7/17
to
On 08/07/2017 10:10 PM, Alf P. Steinbach wrote:
> On 07.08.2017 21:53, Manfred wrote:
>> On 8/7/2017 5:00 PM, Alf P. Steinbach wrote:
>>> On 07.08.2017 13:38, Juha Nieminen wrote:
>>>
>>>> (Also, a Java reference might not be directly pointing to the value
>>>> it's referring to, as is the case in C++. Quite often it will have
>>>> at least one level of indirection.)
>>>
>>> Practically true, but worth noting that strictly speaking it's not
>>> the case that pointers have no indirection C++.
>>
>> I disagree: C++ (and C) pointers have no indirection as far as the
>> language goes, and its implementation too.
>
> Well, the first part is right, the part after the comma is IMO an
> unwarranted assumption.
>
> Are you aware that C++ interpreters exist?
>
> Did you know that there are C++ compilers for virtual machines,
> including for .NET (call it CLI or whatever)?

I was referring to compiled C++ code, not to interpreters.

>
>
>> [snip]
>> This is why I wrote in another thread that a Java reference/pointer is
>> a handle instead of a memory address, since it references an object
>> that is hosted by the runtime, and not an object that is directly
>> allocated in RAM as for a C and C++ object.
>>
>> This may not be that much of a difference with respect to pointer
>> semantics (although it is in specific cases), but it is a substantial
>> difference in how pointer operation is implemented.
>
> I would be interested in the “specific cases”.

These would be e.g. cases like setting a C++ pointer to a specific
address value - memory mapped registers come to mind. Other cases would
be pointer arithmetic - if I am not mistaken you can't do it in Java.
In these cases pointer semantics is substantially different between
C/C++ and Java.

>
>
> Cheers!,
>
> - Alf
>

Juha Nieminen

unread,
Aug 8, 2017, 1:16:01 AM8/8/17
to
Alf P. Steinbach <alf.p.stein...@gmail.com> wrote:
> On 07.08.2017 13:38, Juha Nieminen wrote:
>
>> (Also, a Java reference might not be directly pointing to the value
>> it's referring to, as is the case in C++. Quite often it will have
>> at least one level of indirection.)
>
> Practically true, but worth noting that strictly speaking it's not the
> case that pointers have no indirection C++.

I'm not talking about the memory mapping hardware nor the operating system.
I'm talking about the Java runtime. References are not memory addresses to
the value itself, but somewhere else. It's similar to how std::shared_ptr
works.

The reason why Java references have (at least one) indirection level is
that it allows the runtime to use memory compaction, and it may also aid
in garbage collection.

Öö Tiib

unread,
Aug 8, 2017, 1:25:01 AM8/8/17
to
On Monday, 7 August 2017 23:11:10 UTC+3, Alf P. Steinbach wrote:
> Are you aware that C++ interpreters exist?
>
> Did you know that there are C++ compilers for virtual machines,
> including for .NET (call it CLI or whatever)?

Most impressive is that Emscripten from C or C++ into JavaScript.
So we can run C or C++ programs in web browsers and thanks to asm.js
it runs relatively fastly. Then the layers of such emulation can be
nested and even old hardware and operating system can be emulated
in web browser smoothly.

For example some old MS-DOS games from Digger to Doom-2 ran in web
browser https://js-dos.com/games/
Works in most of web browsers. Haven't tried with IE, but who runs it
anyway?
0 new messages