Data in Java
Yes, Java has pointers - they are called references
- however, Java references are much more constrained than C’s general pointers
- Pointers in C can point to any memory address
- References in Java can only point to an object
- And only to its first element - not to the middle of it
Strings are bounded by hidden length field at beginning of string.
Java Array:
So, it can triggers a bounds-check.
In Java, all variables are references to objects.
Casting
- In C, we can cast any pointer into any other pointer.
- In Java, we can only cast compatible object references.
JVM Operand Stack Example:
1 | iload 1 // push 1st argument from table onto stack |
i
stands for integera
stands for referenceb
stands for bytec
stands for chard
stands for double
We have no knowledge of registers or memory locations (each instruction is 1 byte - bytecode)
We leave many information (address) to the C program that interprets bytecode.
Class File Format
- Magic number
- Version of class file format
- Constant pool
- Access flags
- This class
- Super class
- Interfaces
- Fields
- Methods
- Attributes
Other languages for JVMs
JVMs run on so many computes that compilers have been built to translate many other languages to Java bytecode:
(just using Java bytecode)
- AspectJ
- JRuby
- Jython
- Scale
- And many others, even including C
Microsoft’s C# and .NET Framework
- C# has similar motivations as Java
- Virtual machine is called the
Common Language Runtime
; Common Intermediate Language is the bytecode for C# and other languages in the .NET framework (VB.NET, J#).