I was wondering, why can't I read .class files generated by the compiler on my notepad. I saw some strange figures between infrequent english alphabets. There were even some paragraph changes. What is this? What is a .class file? Why are there random alphabets in it? What is happening? Can I read this file somehow??

Well basically how a java program gets executed is divided into some steps. Starting of these is the conversion of java code written in the Java Programming language which is quite human readable.

Human readable... Hmmm. Does that mean the machine cannot understand it? Isn't the machine showing me all this, isn't it supposed to know?

Yes the machine is showing you all this. But you have to understand a machine just understands instructions. And your machine has a lot of them. Actually its just being fed with shit load of instructions, a couple of billions every second approximately. Speak of a hardworking individual.
So when you tried to read it on your text editor like notepad or textpad or gedit or any of them what you gave the machine was just a bunch of programmed instructions. Broadly those were to read a particular bunch of data and find out if there is some 'English' in there and if yes show. Lets take it step by step.
What is data? Well all data on my pc is just bits. A bit is the simplest piece of information. Its either a yes or a no. Thats all. Rest is all just rules on top to make sense of a bunch of them. Now we proceed to the next question.
Find out some English?
Well English unlike the computer is not just yes and no. We have words, phrases, poems, songs, quotes. If we try to break English down in terms of 'pure' data it is safe to say there are just alphabets 26 of them.
But wait that's not all. What about the ' on it's and more such stuff. English is full of them.
Ah.. yes nicely pointed out. Well those are also taken in let's even take the space between alphabets, paragraph changes, capital or small alphabet everything. But when I have to save it to my hard drive, what is 'write' there is alphabets in yes/no. How its done is through some rules but what the rules give us is simplicity. UTF and ASCII are some rules/bodies managing these rules. So I allot a number to every alphabet and save the number in binary. Go to my other blog post on binary.
Coming back to Notepad i tried to open my .class file on. The notepad has just some instructions to check if the data resembles the pattern of yes and no I get when I save alphabets to binary. Once it sees that it displays what the pattern represents.
So when you ask your Notepad to look at a .class file, which has the instructions meant to achieve whatever your .java file which got compiled into the .class file was written to accomplish, it comes back unsuccessful unexpectedly, and you can't even blame it. It is just following your instructions. Your instructions. If they were not good enough, can't really blame it.
But then what is written on the .class files that is not English. Can it be represented in English anway?
But of course. Its not meaningless bull crap. Now we have reached the cusp of this session perhaps. Lets introduce a new beast into the picture. Its name is Java Virtual Machine long for JVM. JVMs reside in the jungle of your personal computer and many other devices. They are all quite different to say the least. However they all have one similarity between them. Its that they all can understand a particular language. What language is this? Well strictly speaking its not a language. Imagine your computer as having so many components. Monitor, keyboard, speakers, web-cam the whole gimmick. This beast knows how to communicate with all these peripherals on a particular range of hardware. So some models of a smart phone can have a beast common for them. There are not many JVMs out there. So your Java code basically gets translated into the exact instructions you meant to happen. If its a video player you designed in your Java program, it knows how to access the speakers, the video files and monitor and run all of them together to give you that experience. You use libraries, a bunch of them to accomplish something as simple as playing a video file on your computer or your smart phone.  Your Java program is nothing but a set of synchronized instructions written on top of more instructions (the libraries). Your phone or PC's program called compiler breaks this instruction written in the Java Language into what is called the byte-code which can be understood uniformly by any JVM on this planet. This fraternity is quite helpful.
So this JVM. This is quite complex a thing right? Than the other thing the 'compiler'?
Well yes and no. No in a pedantic sense because we are maintaining a very loose colloquial tone to drive this thought process. Yes because whatever we are talking about is actually quite true. To answer this question though, there are 2 ways to look at it. When we say complex it may mean complexity of creating a compiler or a full on JVM. Or it may mean complex in terms of one being slower and taking more time to do a simple thing apparently. There can be more ways to visualize this complexity. It is definitely more complex to optimize the working of a JVM than a compiler which is more of a mistake catcher / translator. Also your computer is not just running the video player you asked it to play. Lot of things are happening in the background. The JVM is playing its part in a much bigger arena than the compiler which can be working with just a few libraries to achieve translation. In fact if you thing about it now, the compiler may actually run on the JVM! So JVM may be the bigger beast but with a faulty compiler none of that video player code you wrote is going to run.

Comments

Popular posts from this blog

Using cookies with HttpURLConnection

SPARQL

ffmpeg for Google Speech API