Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Bytecode contains zero's and one's?  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i heard that when we compile java source code, it will create .class files. these class files contains bytecodes. Bytecodes contain zero's and one's. Is it correct? can you please explain about bytecode.

Then JustInTime(JIT) helps in fast execution. whether JIT helps to convert the bytecode into machine code?Please explain

Explain about native code also.Explain about compilation and execution process

Thank you in advance
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the most fundamental level, computers only work with zeroes and ones. If there's an electrical charge stored in a memory cell then the computer interprets that as a one, if there is no electrical charge it's a zero (or vice versa - that depends on how exactly the computer is designed at the most fundamental level). So, ultimately everything in the memory of the computer consists of zeroes and ones. Not just Java bytecode, but also the text you are reading, everything stored on your harddisk, etc.

The Java compiler compiles your source code into Java bytecode. The CPU in your computer cannot execute that bytecode directly. It first has to be converted to native machine code. That is what the JIT compiler does when you run your program. Native machine code is how instructions are encoded that the CPU in your computer can execute directly.

Find information on the web on how microprocessors work to understand this in more detail.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes bytecodes contains zeros and ones, if use the below command and try to get generate the bc of the java file you will see the internal details.

Create a testing.java file with just main method in it.
javac testing.java

This will create .class file, then using the below command create the bytecode
javap -c testing > testing.bc

Its will be as below

Compiled from "testing.java"
public class test.testing extends java.lang.Object{
public test.testing();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>")V
4: return

public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String hello world
5: invokevirtual #4; //Method java/io/PrintStream.printlnLjava/lang/String;)V
8: return

}

When you inspect the bytecode some opcodes are prefixed with a. For example at line 5 aload_0, The prefix is representative of the type that the opcode is working with. The prefix `a' means that the opcode is manipulating an object reference. The prefix `i' means the opcode is manipulating an integer. Other opcodes use `b' for byte, `c' for char, `d' for double, etc. This prefix gives you immediate knowledge about what type of data is being manipulated.

JIT is part of the JVM and is used to speed up execution time, it helps comoile parts of the bytecode that have similar functionality at the same time and reduces the amount of time needed for compilation. Basically its purpose it to take generic bytecode and compile them into more machine specific instructions.

hope this helps
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explain about native code

In Java terms, "native code" refers to code written in another language (usually C or C++).
My advice: avoid it until you know Java a lot better. If you really want to find out more, Google "JNI".
also.Explain about compilation and execution process

Those topics are simply too large to answer here and, with the best will in the world, you may get a lot of misinformation in a forum like this. The ONLY way to get definitive information is to look at the Java Language Specification. Alternatively, find a good book.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!