CSAPP / CSE 351 学习笔记(Java vs. C)


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
2
3
4
5
iload 1  // push 1st argument from table onto stack
iload 2 // push 2nd argument from table onto stack
iadd // pop top 2 elements from stack, add together, and
// push result back onto stack
istore 3 // pop result and put it into third slot in table
  • i stands for integer
  • a stands for reference
  • b stands for byte
  • c stands for char
  • d 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#).
0%