Java- introduction, I assume you have completed JDK installation in your machine which assures us that we can make settings to run/execute java programs in our machines. Please use following command in your CMD prompt to confirm you have completed Java installation rightly, you should be able to see the version of java in your machine.

D:\>java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)

As we all know Java – has object oriented features. We have a saying as write once/run anywhere is the major advantage for long run of Java language in this world

How Java Works?

The source code is which we write as .java file

When Java is installed in machine, we will have compiler which will compile our source code and it will not execute the code until the standards of Java coding is followed rightly

When our code is compiled, compiler creates an output code which we call as Java bytecode. Any machine which has Java will be capable of converting this bytecode into an output the source code is written for.

Virtual machines are those gadgets we have which have Java virtual machine (JVM) as software installed on it to execute all java related programs into an application for some great purpose in our life. The virtual machines reads and runs the bytecode.

So what we do is write java code and compile it using javac compiler and execute it in java virtual machines.

Source Code Structure in Java

Source code with .java extension holds a single class. Class represents a piece of program. Class must go within a pair of curly braces as mentioned below,

public class MyFirstProgram {

}

In real, class will contain number of methods. Class not only contains methods as far as for beginners lets go step by step – you can find the things which a class can really holds. Right now, lets assume class contains methods.

Method contains a set of statement, usually a method contains some logic. We can call a method as a function or procedure. But we continue to use as method. Example for method mentioned below,

public static void main(String[] args){
   // logic goes here
}

Let’s create a sample class with method,

public class MyFirstApp { //open class declaration

  public static void main(String[] args) { // open method declaration
    System.out.println("My First App"); // Printing to console
  } // close method
} // close class

Text followed by // are comments for user understanding.

Now, once the program is saved, open CMD prompt and navigate to the folder where the .java file is saved and type javac [classname.java] without square brackets and press ENTER to compile our program.

D:\>javac MyFirstApp.java

Once compiler succesfully, a .class file will be created in the same folder which means our program is compiled without any errors. Then type java classname to execute the program.

D:\>java MyFirstApp
Output of code:
My First App

Running a program is like telling Java Virtual Machine (JVM) to load the class and then start executing the main() method. And keep running until all the code in main method is executed. main() method is where your program starts running. No matter how many classes in your application, there should be one main() method to start executing all the classes. Its like key to start engine.

Conclusion

Hence we saw how to check java installation, how java works, source code, compiler, bytecode, class, main method and how to execute a simple class.

Please follow and like us:
error