Jump to content

.class files - What is their purpose?

When I write some java files and compile them, ".class" files get generated for each class in my program.

I am not sure about what the purpose is of the .class files though. Could someone explain it?

 

Thank you.

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

They contain bytecode that is output by the java compiler. The bytecode is what the JVM understands and executes.

 

All Java applications are run inside the Java Virtual Machine, which is why the same Java code works on every java platform.

Link to comment
Share on other sites

Link to post
Share on other sites

When I write some java files and compile them, ".class" files get generated for each class in my program.

I am not sure about what the purpose is of the .class files though. Could someone explain it?

 

Thank you.

 

 

Well, .class is a file containing a bytecode. It's made for being executed on the JVM (Java Virtual Machine). The files contain everything the JVM needs to know about a particular interface. Many times, they are compiled if your source file has more than one class; each class is then compiled into a separate file.

The main parts of the .class are primarily: Version, Access Flags, Interfaces, Fields, Magic, Methods & Attributes. I believe that's all of them.. Not 100% on that part, though.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


« Current PC ~ Phantom Beast »


.::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::.

Link to comment
Share on other sites

Link to post
Share on other sites

They contain bytecode that is output by the java compiler. The bytecode is what the JVM understands and executes.

 

All Java applications are run inside the Java Virtual Machine, which is why the same Java code works on every java platform.

 

Very good. Thank you. 

Could you explain why it displays weird characters when I try to edit it with notepad?

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

Very good. Thank you. 

Could you explain why it displays weird characters when I try to edit it with notepad?

Your text editor is trying to display binary data as ascii.

Link to comment
Share on other sites

Link to post
Share on other sites

Your text editor is trying to display binary data as ascii.

Binary data are just 1's and 0's, right? That isn't to hard to display, is it?

Learning

Link to comment
Share on other sites

Link to post
Share on other sites

So the point of the "binary data" is that binary data is just a bunch of data in raw form.

 

This is just a pseudo-example...not correct at all, but should demonstrate what is in a class file.

Java code (for a class):

class myClass {    public int add(int a, int b) { return a+b; }    public int subtract....    public int multiply....    public int secret....    public int anotherfunction....}

Now java needs to interpret the code....the first time you compile a program it can take some time, that is because it needs to understand the code we read and put it in a more manageable form....

Pseudo-Partially compiled form (syntax is just made up):

class myClassfunctions:add 10 (which line to find the function on)subtract 20multiply 30secret 40anotherfunction 502 Parameters a b//Line 10add a b => cret c

This isn't anywhere close to assembly, but it should show that it is easy to scan for a function and see the code it will need to run (and the code has been made even more basic and easy to predict.  Now this is could be used, but it still is slow because you need to read in "add" for each time you do a simple operation.  So they change this into byte-code.  So add might be 255 (only 1 byte long).  So add a b might become 255 1 2 3 (4 bytes long saying 255 = add, 1 = variable 1, 2 = var 2 etc).

 

So you basically have a smaller file that is very quick to read and understand.  In fact it is a bit more complex, but it is similar in concept though.  Look at http://en.wikipedia.org/wiki/Assembly_language basically your code gets made into assembly (with extra information about the class so people can use the class file after you release it).  From assembly your code gets turned into byte-code (which in some respects is close to machine level...although in java this isn't quite the case, it still is a good comparison).  If you look at this picture http://en.wikipedia.org/wiki/File:Motorola_6800_Assembly_Language.png

You will see on the left hand side are things such as C00D, this in just saying the next instruction is at another address...much like a function.  Anyways look at the image.  The left side has an equivalence to byte-code, the middle is the assembly...your code is still one higher up, but again is converted to assembly first (or something similar to assembly first)

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

opening a class file with notepad is like opening an exe file with notepad, it's that simple

it's just not made for notepad

Link to comment
Share on other sites

Link to post
Share on other sites

Binary data are just 1's and 0's, right? That isn't to hard to display, is it?

No.

 

Your text editor looks for ASCII codes and displays the correct ascii character for that code. If you open raw binary data then the text editor is going to look for symbols in that raw data. It will show up as random characters and symbols.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×