Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I never had any formal education in CS. So after being an average developer for 7 years now, I am feeling the need to understand questions like: What exactly happens when I write a java class. i.e. How does it actually interact with the machine hardware to give me a result? It seems mind boggling how even the most complex form of data (like a video) is actually converted and stored in form of 1s and 0s.

Are there any good books that explain these questions in a lucid way? I realise i am asking for a lot because the topic by itself is quite complex. After researching this site, I found this recommendation but it seems like a 800 page one. Are there anything else a bit easier? http://www.amazon.com/dp/0123745144/?tag=stackoverfl08-20

share|improve this question
    
SEE IF THIS HELPS: docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf –  user60812 Aug 10 '12 at 20:02
    
1  
Java is not a good choice for this kind of thing, as it has a quite complex additional layer (the JVM). –  Michael Borgwardt Aug 10 '12 at 20:35
    
I know, but he was asking regarding what happens when he writes a java class, so i thought i'd point him to the above. –  user60812 Aug 10 '12 at 22:08

4 Answers 4

up vote 8 down vote accepted

I don't think you will find a single book that will take you through Java code -> On the CPU.

You can probably find books about bytecode and Jitting and assembly languages.

What I would recommend in order to understand what does go on in a CPU is Code by Charles Petzold. He takes you from flash lights going on and off (binary), though different ways to encode text (Braille, Morse code, etc...) all the way to building up a CPU from relays.

share|improve this answer

+1 for Code by Charles Petzold. That's a great book to get started.

http://www.amazon.com/Code-Language-Computer-Hardware-Software/dp/0735611319/ref=sr_1_1?s=books&ie=UTF8&qid=1344629989&sr=1-1&keywords=petzold+code

Two others that are fantastic are the Write Great Code series, by Randall Hyde.

Volume 1: Understanding the Machine covers a great deal about computer architecture and how code executes within the hardware organization.

http://www.amazon.com/Write-Great-Code-Understanding-Machine/dp/1593270038

Volume 2: Thinking Low-level, Writing High-level covers how machine instructions are executed, how compilers turn source code into machine code, and what common programming constructs boil down to at the machine code level.

http://www.amazon.com/Write-Great-Code-Volume-High-Level/dp/1593270658

I highly recommend all three of these books.

share|improve this answer
    
Thanks. The only reason I chose the other response was because I was using FIFS (first in first served) –  Kaushik Aug 10 '12 at 21:29
  1. Something called a lexer inside javac splits your class into tokens, which are things like keywords, numbers, punctuation, etc. In the line a += 42; the tokens are a, PLUSEQUALS, 42, and SEMICOLON.
  2. Something called a parser inside javac takes those tokens and uses sophisticated algorithms to create an abstract syntax tree.
  3. The compiler uses sophisticated algorithms to generate byte code from the abstract syntax tree. Byte code is machine code for a pretend CPU, designed to be easily translatable into real machine code for a real CPU.
  4. When you run the program, another compiler inside the JVM converts the byte code into machine code for your particular platform, and loads it into memory to execute. Machine code is what the CPU understands. Your a += 42; could end up looking like 00000100 00101010. The first byte, more commonly represented as the hexadecimal number 0x04, means add the next byte, the binary representation of 42, to the AL register, then store the result back into the AL register.
  5. You can think of each bit in memory as a wire with either 1.3 volts or zero volts. Using complex combinations of logic gates, the result of the addition appears on another set of wires. The circuit below adds two 1-bit numbers. Other logic circuits look at the 0x04 from the machine code to decide what voltages get sent to A and B. The lowest zero from your 42 might end up on A, and the lowest bit that was already in the AL register goes to B. The voltage that comes out on Sum ends up getting routed back to the AL register. Put a billion or so logic gates together, and you have a processor.

    half-adder

  6. Logic gates are implemented in terms of transistors. The above circuit can be implemented using 10 transistors or so. You can buy some transistors from Radio Shack and make it at home if you want, but don't be surprised if your significant other isn't impressed that you could build a computer capable of adding 1 + 1.

  7. Transistors are implemented by carefully controlling patterns of semiconducting material, insulating material, and metal on a silicon wafer.

Congratulations, you have now had an entire university degree condensed to a few bullet points. It's not something you're going to be able to understand overnight. Pick one layer and tackle it one step at a time.

share|improve this answer
    
I agree with point #6. Keep expectations at the door.:) –  Kaushik Aug 13 '12 at 15:15

Hans Petter Messmer's The Indispensible PC Hardware Book gve me my start into the wonderful world of ones and zeroes.

If I were you I'd forget starting with Java. Get out a C compiler and get it to generate the assembly code listing when it compiles (interleaved with the actual source code of course!)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.