Question

Could someone please explain or guide me to a resource that explains how software works on hardware , for example , if I declare a private or public variable how is this enforced on hardware , in other words , what enforces the fact that they are public and private , as every instruction has to happen on the hardware , how is this integrity maintained ?

Was it helpful?

Solution

In Java the access rights (public/private etc.) are managed by the compiler and the Java Virtual Machine (JVM). e.g If you load a Java library you can dynamically change the access rights to a field and make it public. There's no immediate hardware analogue.

There are so many layers between a Java program and the hardware that this question is very difficult to answer. Java code results in Java bytecode (similar to machine code), and you don't have direct memory access. The JVM provides this interface and recompiles code into machine code as required but that's largely hidden from you.

If you're interested in the interface between hardware and software I would look at writing some code in C and C++, and the resultant machine code/assembler that is generated. C/C++ are much closer to the hardware and will give you an appreciation of what's going on.

OTHER TIPS

This is not enforced on hardware but at compilation time only. A variable is only a spot in memory meaning you can access it from anywhere as long as you have a pointer to it (or through jni if you're in java =D). In other words, access rights are processed and enforced when compiling your .class file.

This information is handled at a higher level than the machine code. The machine itself only understands about memory addresses and values - it doesn't care if you try to access a private field in another class. It's the compiler that keeps track of visibility rules and uses its internal representation of the classes to enforce them. At compile time, the types of objects that you access methods and properties on are checked to determine whether those names (e.g. bar in foo.bar()) can be resolved. This takes into account scoping and a number of other things, depending on the language.

It's the compiler that enforces private vs public. not the machine.

Java is a compiled language, and when it compiles, it gets translated into machine code.

The private-public scopes are enforced while the program compiles

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top