A Java course file is a document containing Java bytecode and having .class extension that have the right to be enforcement by JVM. A Java class record is produced by a Java compiler from .java documents as a an outcome of effective compilation. Together we understand that a solitary Java programming language source document (or we have the right to say .java file) might contain one course or more than one class. For this reason if a .java document has an ext than one class then each class will compile into a separate course files.
You are watching: Difference between .java and .class
For Example: conserve this listed below code together Test.java on her system.
For Compiling:javac Test.javaAfter compilation there will certainly be 3 class papers in matching folder called as:Sample.classStudent.classTest.class
A solitary class paper structure contains attributes that define a class file.Representation the Class file StructureClassFile magic_number; minor_version; major_version; constant_pool_count; constant_pool<>; access_flags; this_class; super_class; interfaces_count; interfaces<>; fields_count; fields<>; methods_count; methods<>; attributes_count; attributes<>;Elements that class document are as follows:magic_number: The an initial 4 bytes the class file are termed as magic_number. This is a predefined worth which the JVM usage to identify whether the .class file is generated by precious compiler or not. The predefined value will it is in in hexadecimal kind i.e. 0xCAFEBABE.Now let’s see what occur when JVM will certainly not discover valid magic number. Mean we have actually a .java record named together Sample.java as follows and follow step by step procedure on your system.// course Declarationclass Sample public static void main(String<> args) System.out.println("Magic Number"); Step 1: Compile utilizing javac Sample.javaStep 2: currently open the Sample.class file. It will looks favor following.Step 3: now erase at least solitary symbol indigenous this Sample.class record from beginning of record and save it.Step 4: Now shot to run this using java Sample command and also see the magic i.e. Friend will obtain run time exception (See the highlighted text in listed below image):Note: This can vary depending on how much you remove the .class paper data.minor_version & major_version: this both together represents .class file version. JVM will use these versions to recognize which variation of the compiler generates the current .class file. Us denotes the version of class paper as M.m wherein M represents major_version and m represents minor_version
Note: reduced version compiler created .class document can be enforcement by high variation JVM but greater version compiler produced .class document cannot be executed by reduced version JVM. If us will shot to execute we will gain run time exception.This demonstrate is for home windows OS together follows:Step 1: open a command prompt window and shot to inspect java compiler version and also JVM version using following regulates respectively (Highlighted text in photo are the commands)Output because that 1.8 variation will be:Step 2: Now examine with another version which may be higher or reduced than already installed.thisDownload link.And download this to your pc or laptops and note the environment address.Step 3: open a 2nd command prompt window and set the path of bin folder of installed jdk mounted during second step. And also check because that Java compiler version advertisement JVM version.Step 4: currently on first command prompt translate into the any valid .java file. For example: See over Sample.java file. Compile that as:Step 5: now on second command notice window try to run the above compiled password class record and view what happen. Over there is a operation time exemption which I have highlighted in listed below image.Note: inside jdk 1.5 version means 49.0 and 1.6 method 50.0 and also 1.7 means 51.0 etc. Class file version wherein the digits before the decimal point represent the major_version and also digits after decimal allude represents the minor_version.constant_pool_count: It to represent the number of the constants present in the constant pool (When a Java record is compiled, all referrals to variables and methods room stored in the class’s constant pool as a symbolic reference).constant_pool<>: It to represent the information around constants current in continuous pool file.access_flags: It administer the information about the modifiers i m sorry are declared to the course file.this_class: that represents fully qualified surname of the course file.super_class: the represents completely qualified name of the prompt super course of existing class. Consider above Sample.java file. Once we will certainly compile it, climate we can say this_class will be Sample class and also super_class will be Object class.interface_count: It returns the number of interfaces implemented by current class file.interface<>: It return interfaces information implemented by current course file.fields_count: It to represent the variety of fields (static variable) present in current class file.fields<>: it represent fields (static variable) information existing in current course file.method_count: that represents variety of methods current in current course file.method<>: It returns information around all methods current in current course file.attributes_count: It return the number of attributes (instance variables) existing in current class file.
See more: Which Of The Following Professions Is Most Similar To An Mnc? ?
attributes<>: It provides information around all qualities present in current class file.Attention reader! Don’t stop learning now. Acquire hold of all the vital Java Foundation and Collections concepts with the Fundamentals of Java and also Java collections Course at a student-friendly price and also become industry ready. To complete your ready from discovering a language to DS Algo and also many more, please refer Complete Interview preparation Course.