4

What's the difference between java byte code ( the compiled language and otherwise known as object code ) and machine code ( code that is native to the current computer ). I have read in books that they refer to byte code as binary instructions and I don't know why.

6
  • Yeah but the books don't really cover it Commented Jul 4, 2016 at 13:38
  • 1
    The name "binary instructions" is not a very good name for machine code, and it's not a name that has a clear, specific meaning. Commented Jul 4, 2016 at 13:39
  • In the book they refer to the byte code AS binary instructions, not the machine code Commented Jul 4, 2016 at 13:40
  • 1
    Whatever, the same still applies. The word "binary" doesn't mean a lot in this context. It's probably just used to indicate "not human-readable". Commented Jul 4, 2016 at 13:43
  • 1
    Ultimately, everything in the computer's memory is ones and zeros. Commented Jul 4, 2016 at 13:45

2 Answers 2

8

Bytecode is platform-independent, bytecodes compiled by a compiler running in windows will still run in linux/unix/mac. Machine code is platform-specific, if it is compiled in windows x86, it will run ONLY in windows x86.

Continue reading your books =)

Sign up to request clarification or add additional context in comments.

5 Comments

Okay but why is it called binary if it's actually byte code? Shouldn't machine code be called binary??
Both bytecode and machine code are binary. But bytecode should be compiled into machine code before you can run it. I'd recommend you read this: en.wikipedia.org/wiki/Machine_code And this: en.wikipedia.org/wiki/Bytecode
Yes, it is. Java code compiled into a bytecode, bytecode compiled into a machine code.
Oh right yes, last question why does java need to convert the source code into byte code only to again convert it to machine code? Why is that better because it sounds slower, why not straight away convert it to machine code?
You can compile your source code into platform independent bytecode once (this operation is slow), and then each time your user will be run your app, it will be compiled bytecode=>machine really fast on every platform! Each time compile like source code => machine code will be too slow.
3

Bytecodes are the machine language of the Java virtual machine. When a JVM loads a class file, it gets one stream of bytecodes for each method in the class. The bytecodes streams are stored in the method area of the JVM. The bytecodes for a method are executed when that method is invoked during the course of running the program. They can be executed by intepretation, just-in-time compiling, or any other technique that was chosen by the designer of a particular JVM.

A method's bytecode stream is a sequence of instructions for the Java virtual machine. Each instruction consists of a one-byte opcode followed by zero or more operands. The opcode indicates the action to take. If more information is required before the JVM can take the action, that information is encoded into one or more operands that immediately follow the opcode.

Each type of opcode has a mnemonic. In the typical assembly language style, streams of Java bytecodes can be represented by their mnemonics followed by any operand values. For example, the following stream of bytecodes can be disassembled into mnemonics:

// Bytecode stream: 03 3b 84 00 01 1a 05 68 3b a7 ff f9
// Disassembly:
iconst_0 // 03
istore_0 // 3b
iinc 0, 1 // 84 00 01
iload_0 // 1a
iconst_2 // 05
imul // 68
istore_0 // 3b
goto -7 // a7 ff f9

The bytecode instruction set was designed to be compact. All instructions, except two that deal with table jumping, are aligned on byte boundaries. The total number of opcodes is small enough so that opcodes occupy only one byte. This helps minimize the size of class files that may be traveling across networks before being loaded by a JVM. It also helps keep the size of the JVM implementation small.

All computation in the JVM centers on the stack. Because the JVM has no registers for storing abitrary values, everything must be pushed onto the stack before it can be used in a calculation. Bytecode instructions therefore operate primarily on the stack. For example, in the above bytecode sequence a local variable is multiplied by two by first pushing the local variable onto the stack with the iload_0 instruction, then pushing two onto the stack with iconst_2. After both integers have been pushed onto the stack, the imul instruction effectively pops the two integers off the stack, multiplies them, and pushes the result back onto the stack. The result is popped off the top of the stack and stored back to the local variable by the istore_0 instruction. The JVM was designed as a stack-based machine rather than a register-based machine to facilitate efficient implementation on register-poor architectures such as the Intel 486.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.