• Post Reply Bookmark Topic Watch Topic
  • New Topic

does java codes are converted into assembly by JVM ?  RSS feed

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know this is off topic , but i m curious to know this ,
i want to ask you foll questions
  • i was wondering , does java codes are converted into assembly by JVM ? if no , does JVM directly deals with the instruction set , register set and all ?
  • and machine code would be just 1's and 0's but the compiled codes are never 1's and 0's they are something different , what are they ?




  •  
    Saloon Keeper
    Posts: 4037
    94
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Java compiler creates bytecode, which is then interpreted by the JVM, or -more likely- compiled into machine code by the JVM (and its HotSpot compiler). Assembly code is never part of it. All these terms have extensive Wikipedia pages where you can find more detail.

    0s and 1s are how all files are stored - Java source code, assembler code, machine code, bytecode, PDFs etc. That has nothing to do with the content of what is being stored.
     
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:
  • and machine code would be just 1's and 0's but the compiled codes are never 1's and 0's they are something different , what are they ?



  • Everything is just 1s and 0s. Your .java files are 1s and 0s. A .txt file is 1s and 0s. What you're reading here is 1s and 0s.
     
    naved momin
    Ranch Hand
    Posts: 692
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:
    naved momin wrote:
  • and machine code would be just 1's and 0's but the compiled codes are never 1's and 0's they are something different , what are they ?



  • Everything is just 1s and 0s. Your .java files are 1s and 0s. A .txt file is 1s and 0s. What you're reading here is 1s and 0s.

    ya , but whenever we look the machine code or compiled code (for eg c , c++) you will never notice any 1's and 0's ,
  • 2nd lly how you or any one can convert something into machine code in any language ?
  •  
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    At a low level, the only thing a digital computer works with are 0s and 1s. The memory in your computer consists of billions of memory cells (bits), each of which can store a 0 or 1. The harddisk in your computer can store hundreds of billions of 0s or 1s. The operating system and all other software on your computer, and also all documents and files, consists of 0s and 1s on your harddisk and in the computer's memory.

    On a higher level, groups of bits are organized into bytes (8 bits = 1 byte). Things like text characters are ultimately represented by sequences of bytes. For example in the ASCII text encoding, a group of bits with the value 00100001 represents the character A. Other kinds of data and also instructions in a program are encoded with other sequences of bits. Each instruction in the instruction set of the CPU is represented by a unique sequence of bits in the memory. The CPU reads instructions from the memory and executes them.

    Assembly language is a human-readable form of machine code. Machine code consists only of sequences of numbers, which is too cumbersome for human beings to deal with. For a human being it's much easier to understand an instruction like "ADD AX, 34" than the corresponding machine code 00000101 00010010.

    The JVM isn't a human being so it doesn't need to deal with assembly language. It just translates Java bytecode to machine code directly.

    The Java compiler translates Java source code to Java bytecode, which is machine code for the JVM, which is independent of the actual CPU and system that the program runs on. The JIT compiler in the JVM translates the Java bytecode to native machine code, which the CPU can execute.

    So the process works like this:

    1. You write "a + 34" in your source code.
    2. javac converts this to the "iadd" bytecode.
    3. You run the program. The JIT converts the "iadd" bytecode to a native x86 ADD instruction.
    4. The CPU executes the ADD instruction.

    You can find all the Java bytecode instructions in the Java Virtual Machine Specification. CPU manufacturers, such as Intel, have documents on their website about the native instruction set of their CPUs.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:
    Jeff Verdegan wrote:
    naved momin wrote:
  • and machine code would be just 1's and 0's but the compiled codes are never 1's and 0's they are something different , what are they ?



  • Everything is just 1s and 0s. Your .java files are 1s and 0s. A .txt file is 1s and 0s. What you're reading here is 1s and 0s.

    ya , but whenever we look the machine code or compiled code (for eg c , c++) you will never notice any 1's and 0's ,


    It depends on how the app you're looking at it with interprets the bytes. Whether you're looking at a .txt file or a .class file or a .exe file, it could be displayed as 0s and 1s, or it could be displayed as hex codes, or it could be displayed as text, although not all byte sequences have valid textual representations.

    For instance, I create a file A.txt that only has the single character 'A' in it, then what's stored in that file is the bit pattern 01000001. If you look at it with Notepad, it will display as 'A'. If you look at it with a hex editor, it will display as '41'. If you look at it with a binary viewer, it will display as '01000001'.

  • 2nd lly how you or any one can convert something into machine code in any language ?


  • Very easily. You look at the rules for the source language, you see that , you see that the rules for that language mean that you need to multiply 6 by 9 and store the result in variable x. Then you look at the specs for the CPU on which you're going to execute that code. You see what the instructions are for multiplying and for storing values on the stack, and you generate those that correspond to the source code.
     
    Ranch Hand
    Posts: 607
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Great explanation Jesper!!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!