Blogs  

What are the components of Java Architecture

Components of Java Architecture

 Through Java Architecture we have the compilation and the interpretation both combined. And the architecture details all the processes executed while we formulate the Java Program. And before we proceed with the article, let's have a brief on what we are going to cover. We are going to cover what Java architecture is, what are the components of Java, why Java is platform-independent, and what is JIT in Java. And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. 

So, what is the Java Architecture?

It’s the compilation and interpretation joined together. We write the code in Java, convert it into Byte code, and it is done by Java Compiler. The JVM converts the byte code to the machine code. The machine code is platform-independent, and its executed by the machine. However, the JDK must be installed on the machine. Post Java 10, the JRE is included within the JDK. And you will not find the JRE separately now. And the JVM is included in the JRE. 

Hence, the javac compiles the source code, which we write in Java in the IDE or any editor. Now the javac generates the .class file. And this class file is the byte code. The bytecode is the set of instructions to the JVM. The JVM is the part of JRE, and it contains both the compiler and the interpreter. The interpreter interprets the byte code into machine code, and the CPU can then execute the machine code for the final output. 

Remember,

  1. The interpreter is the part of the JVM, and JVM is machine-independent. However, JRE is machine-dependent. Also, the JDK is machine-dependent. Now the JRE is included in JDK since post-Java 10. JVM is part of JRE. And JVM is machine-independent.

  2. Hence, Java is platform-independent because of the JVM and not because of JRE or JDK.  And that is why we say Java supports the WORA. 

Also, remember:

  1. The bytecode contains the .class file for each class separately. 

  2. The .exe files of C++/C can be executed directly, and hence are not secure. However, the .class files or the bytecode cannot be executed directly, and hence Java is secure.

  3. The .exe files of C++/C are platform-dependent, and the .class files or the bytecode are platform-independent. 

  4. Also, the .exe files cannot be converted back to source code. However, the bytecode can be converted back to the source code.

Let’s have a look at the java architecture more deeply, and let’s look at the components of Java.

Various components of the Java Architecture

We have three components of the Java language: JRE, JVM, and JDK. And we already know their full form by now, and hence I am not including it here. However, we are going to discuss them deeply one by one below.

Java Virtual Machine

The Java applications support the WORA as we can run the Java code on any platform.  And this is due to the JVM. It’s the component of Java that caters to us an environment for Java program execution. The JVM has an interpreter that interprets the bytecode into machine code. And this is executed in the machine where the Java program runs.

And in a nutshell, the JVM does the following functions: It loads the code, does the verification of the code, executes it, and provides the runtime environment.

Now, let's have a look at the JVM architecture now. The classes are loaded by the Classloader into the JVM memory, which has four parts, the method area, the stack, the heap, and the native stack. The execution engine is formed of the JIT compiler and the Garbage collector, there is a native interface that acts as the interface between the JVM memory, execution engine, and the native libraries.

Explanation:

ClassLoader: The Classloader happens to be the subsystem of the JVM. Java uses it to load the .class files. And when we run the Java program, the class files are loaded first. 

Class Method area: This is the Data Area in the JVM, and the class data is stored there. The static variables, static methods, static blocks, and instance methods are being stored here.

Heap: This is made when the JVM starts. And it can decrease as well as increase in size while the application runs.

Stack: The JVM stack is the thread stack. This is the data area in the JVM memory made for a single execution thread. This is used by the thread for storing various elements like partial results, local variables, and the data for invoking the method and returns.

Native stack: This subsumes all of the native methods in the application.

Execution Engine:

  • JIT compiler

  • Garbage collector

JIT compiler: The JIT is a section of the runtime environment. And it betters the performance of the Java applications as it compiles the bytecode into machine code at run time. By default, it is enabled. And when we compile a method, the JVM directly fetches the compiled code of the method. The JIT compiler compiles the bytecode into machine code and compiles it JIT for running.

Garbage collector: It is used for collecting unused content. It’s a part of JVM. And it keeps a record of each object in the JVM, and it removes all that is unwanted. It follows two steps for working, which are Mark and Sweep.

Mark: This is where the garbage collector finds which memory is in use and which is not in use.

Sweep: This removes the object which is found during the Mark stage.

Java Runtime Environment:

The JRE software develops the runtime environment, where the Java programs can be executed. It is the on-disk system that picks the Java code, combines it with all the libraries required, and then finally JVM executes it.  The JVM has all the libraries as well as the software required for running the Java program. It is a part of the JDK; however, it can be downloaded separately also.

JDK or the Java Development Kit:

This is the software development environment that is made use of for developing the applets and the Java Applications. It has the JRE and various development tools, like loader/interpreter, archiver like a jar, javac (compiler), a document generator like Javadoc, as well as various other tools. 

So the JDK means JRE + development tools. And the JRE means JVM + library files. 

java: This is the launcher of the java application.

javac: this compiles the Java source code.

javadoc: This generates the API documentation.

jar: This creates as well as manages the JAR files.

And we can now have a look because Java is platform-independent.

So how the Java is Platform Independent?

A programming language that runs on all the operating systems when we talk of the development and compilation.

And Java is platform-independent due to the bytecode, and we have already discussed this. However, let's repeat what it is. It is the code instruction to the JVM which is machine-independent.

Below, we are briefing the complete step again in detail. The source code is compiled, and the javac generates the Bytecode, and the JIT compiler in JVM converts the bytecode into native machine code at run time.

Hence java source code or .java ? javac (.class), JVM (sample.obj) ? final output.

Hence, the source code is converted into a .class file by the java compiler. And the .class file is converted to the byte code by the javac. The JVM converts it to the object file. And that is seen as the final output, which you see over the screen

Now, let's understand the JIT in java.

JIT in Java

This deals with the performance optimization of the Java-based applications at run time.  And the performance depends upon the compiler. And we have already seen the whole process, and how JIT does the magic.

The JIT compiler does the compilation of the byte code and converts it into machine code, and it compiles Just in Time for running. When JVM compiles the method, it calls directly the compiled code of the method.

Let’s have a broader look.

The bytecode requires interpretation or compilation to a well-defined machine instruction which depends upon the instruction set passed. And execution can be direct if the instruction architecture is based on bytecode. The bytecode interpretation takes time, and hence the execution speed gets affected.

For better performance, the JIT compiler does the communication with the JVM at run time and it compiles the appropriate bytecode categorization into a native machine code.   And through the JIT compiler, the hardware executes the native code, and not like JVM interpreting the same bytecode sequence again and again. That can incur overhead on the translation procedure.

And that brings us to the end of this article. Hope you now have the complete detail of the Java architecture. And we will be coming up with more on Java. Hence, do keep reading our blog.

Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let's have a look at what you will get with this Java training package:

  • You need to pay a nominal fee.

  • You can choose any Java certification, as per your skills and interest.

  • You have the option to select from online and classroom training.

  • A chance to study at one of the best Java training institutes in India 

  • We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.

  • Naresh I technologies cater to one of the best Java training in India.

  • And a lot more is waiting for you.

Contact us anytime for your complete Java online training.

Coupling in Java and its different types

Coupling in Java and its different types

Java happens to be an Object-oriented programming language. Coupling is quite essential in Java while you deal with Java Classes and Objects.  It generally refers to what level one class is dependent on the other class. In this article, you will learn all about coupling. Coupling is essential for all programming languages, and here you will find various examples in detail explaining what is coupling. The topics covered in this article are what is coupling in Java, various types of coupling, tight and loose coupling and the difference between these two types of coupling. You should know that coupling is the extent to which a class knows about some other class. And, It's a very important feature of any programming language. So let's begin this article. And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training.

Coupling in Java

Coupling is a scenario where an object can be used by another object. It ensures collaboration and doing the task for each other.  And in a nutshell. It is the calculation that explains to what level an object needs another for completing its task. It’s the usage of an object by some other object, and thus a low coupling will mean a low level of dependency of one module over the other. We know hence couping as a collaboration of one class on the logic of another class. Hence, you can easily understand that for a better module-based programming experience the programming language needs to ensure low coupling. That means the modules are not dependent on one another and hence programmers can work on them separately without bothering about the other modules. And this is because the modules are independent of each other, as the programming language is ensuring low coupling. 

Types of coupling

We have two types of coupling in Java, and they are as follows:

  • Tight coupling

  • Low coupling

Let's have a look at each of them separately.

Tight coupling

We see tight coupling in conditions where a group of classes is extensively dependent on each other. And this happens when there are so many responsibilities in the classes, or when one liability is on various classes, and not merely one class. And this scenario when one object creates another object for its purpose is known as Tight coupling. And the parent object knows a lot about the other child object, and that’s why there is high coupling between the two. The dependency level, and the fact that anybody cannot change the object, make them support tight coupling.

Now, let's have some examples related to tight coupling. Keep in mind that Java supports both tight coupling and loose coupling. However, for good architecture, a software design should support loose coupling. It's one of the prime features of a good software design.

Example: Just think of a situation when we have two classes. The first class is the class with parameters for the area, and the second class has the methods for calculating the area of the rectangle. However, it inherits the parameters from the first class. Hence, these classes are loosely coupled to some extent as the change in one class does affect the other class to a low level however. However, suppose the first-class make use of the method for calculating the area that is defined in the second class. See below, and then you will find that the two classes are more tightly coupled, as the first class has the dependency in the form of the second class. If we remove the second class then we cannot just execute the first classes, more in second case, and to lesser extent in first case. And hence, the two classes are tightly coupled in both cases. In first case, we will not get the output that we need without the second class. And in the second case, an exception will be thrown, like the method definition missing. It's not good for the software design, and hence we make use of the interfaces. We will see that later in some in this blog. Please just keep reading this article on coupling.

Class parameters

{

     public int len=10;

     public int breath=10;

     public static void main(String[] args)

     {

           Area b=new Area();

            int a = b.area(this.len, this.breath);

           System.out.printlin(“ Area=”, +a);

      }

Class Area{

        public void area(int a, int b)

          {

                 return(a*b);

          }

}

Output:

Area = 100

Above we see that the two classes are being bound together, and they work like a one unit, and one part does not work without the other part. It’s one of the simplest examples in Java of tight coupling. Let's have one more example explaining tight coupling. And we repeat that tight coupling is not good for a proper software design.

Example 2:

 package CalculateArea; 

public class A {

      public static void main(String args[]) {

           Area x = new Area();

           x.PrintA();

      }

}

class Area {

      LenBreath y;

      public Area() {

            y = new LenBreath();

      }

      public void PrintA() {

            System.out.println("Area");

            y.PrintArea();

      }

}

class LenBreath {

       public int a;

       public int b;

       public LenBreath() {

             a=12;

             b=12;

       }

       public void PrintArea() {

             System.out.println(this.a * this.b);

        }

}

Output 

Area

144

Above we see even more tight coupling and this is not good for a good software design. Also, you can understand that different programmer in a software development team code different module. And If there is tight coupling, then they cannot work independently. Let's now see what is the loose coupling in detail. And we will find that it is better for a good software design than to

Loose coupling

When a certain object gets the object from any external source, then we term it as loose coupling. This implies that the objects are loosely coupled and are independent. If the objects are loosely coupled, then the maintenance becomes easier, and that means less effort is required. And you should know that it’s the heavy maintenance effort required, which Is the disadvantage of the tight coupling. And this has been removed through the implementation of loose coupling. Let's have a look at two code examples, explaining the loose coupling. And you will find that we make use of the interface. And if you want to learn these in detail, please join our Java training classes. It will cost you a nominal fee, and by the end, you will be a complete Java developer. Please be assured. Contact us now, or anytime.

Example 1:

package loose_coupling;

class Area {

      public static void main(String args[]) {

           Rectangle r = new Rectangle(15, 15);

           System.out.println(r.getArea());

      }

}



final class Rectangle {

       private int area;

       Rectangle(int length, int breath) {

             this.area = length * breath;

  }

       public int getArea() {

             return area;

       }

}

Output:

225

 Example 2:

package loose_coupling;

import java.io.IOException;

public class Main {

  public static void main(String args[]) throws IOException {

           PrintShape x = new Square();

           PrintShape y = new Rectangle();

           Master a = new Master(x);

           a.PrintResult();

           Master a2 = new Master(y);

           a2.PrintResult();

      }

}

interface PrintShape {

      public void Printshape();

}



class Master {

      PrintShape s;

      public Master(PrintShape s) {

           this.s = s;

      }

      public void PrintResult() {

           System.out.println("Master");

           s.Printshape();

       }

}



class Square implements PrintShape {

       public Square() {

       }

       public void Printshape() {

            System.out.println("Square");

       }

}

class Rectangle implements PrintShape {

       public Rectangle() {

       }

       public void Printshape() {

            System.out.println("Rectangle");

       }

}

Output:

Master

Square

Master

Rectangle

Explanation:

Through the use of coupling, we have ensured above the loose coupling. We see above that the Square and Rectangle classes can exist without each other. Hence, they are independent of each other. If there is no Rectangle class then as well the Square class can exist. And this has been made possible through the use of an interface. Hence, an interface concept plays an important role in ensuring the Loose coupling. And this is not true only for Java. But in fact, this true for all object-oriented programming languages.

What a tight coupling and loose coupling differ in between each other

Differences between the tight coupling and the loose coupling are as below:

  1. Tight coupling leads to more interdependency, and loose coupling leads to low dependency and it supports more test-ability.

  2. The loose coupling follows the GOF program of the interface and not the implementations. And the tight coupling does not cater to us the interface concept

  3. There is more information from tight coupling, and there is less information flow in loose coupling.

  4. There is less coordination in loose coupling and more coordination in tight coupling.

  5. It’s easier to code in the loose coupling, and quite complex to code in tight coupling.

Hence, loose coupling is always better than tight coupling. And with this, we come to the end of this tutorial on the Java coupling concept. We hope you now have understood what coupling is all about. If you will learn software project management then you will find that loose coupling is always better than tight coupling for software development. And you have seen it here as well.

Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let's have a look at what you will get with this Java training package:

  • You need to pay a nominal fee.

  • You can choose any Java certification, as per your skills and interest.

  • You have the option to select from online and classroom training.

  • A chance to study at one of the best Java training institutes in India 

  • We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.

  • Naresh I technologies cater to one of the best Java training in India.

  • And a lot more is waiting for you.

Contact us anytime for your complete Java online training.

Hello World Program in Java

Hello World Program in Java

The first program a beginner writes in Java is the Hello World Program. However, on many occasions, we do not take into account the details of basic syntax. In this article, we discuss the details of the Hello world program in Java.  We will be covering the topics like Hello world program, analysis of syntax, how to compile the program, and how we can execute it. So, let's begin our article. And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. 

Hello World Program in Java

Before we start curating the details, let's first have a look at the coding and check how the basic hello world program in Java can be coded.

public class demo {

    public static void main (String[] args) {

        System.out.println( "Hello World" );

    }

}

And that’s the first program written by most of the beginners. Hence let’s analyze this program syntax in detail.

 

Syntax Analysis

Public class Demo {

Here through the use of the Class keyword, we have declared a new class known as Demo. We all know that Java is an object-oriented programming language (OOPS), and the whole class definition including the members must be enclosed within the curly braces. Also, we have used the public keyword, and that means the class is accessible from outside the package as well. If we make use of the other scope specifiers like Private or Protected that will affect the accessibility of the class. And that we will see in a separate blog. Please do not worry about it for now. 

Second Line: public static void main (String[] args){

This line has a method known as main(String[] args). This is the main method. It’s the actual entry point for the Java compiler. And it starts the program execution from here onward. And hence, whenever we execute a program, the first method invoked is the main method. And various other methods are invoked from inside the main method. And in all standard Java applications, we must have one main method as the program execution is triggered through it.

Let’s now discuss each word of this line.

Public: This is an access modifier. And it sets the visibility. And since it's public, the JVM can execute the main method from anywhere.

Static: This keyword makes the class static. And the main method Is declared static as we don’t need to define an object to call the main method. It gets invoked by JVM without object creation. And this saves the memory.

Void: This represents the return type of the method. The java main method never returns any value and hence it's always declared void.

Main(): This is the name of the method configured in JVM. 

String[]: This means that the main method can take in a single line argument which is of a type string array. And it's the java command line arguments. There are various other main method signatures possible. And they are:

static public void main(String[] args)

final public static void main(String[] args) 

And there are various others possible. We will discuss them in a later blog post. Please do not worry about it hence, for now.

Third line: System.out.println(“Hello World”);

system: This is in Java.lang package, and is a pre-defined class that has various variables and methods.

out: This is of type Printstream and is a static member field.

println: This method is in PrintStream class and we use it for printing the arguments which are passed to the console, and it also prints a newline. You can omit this and use the print() method if you do not want a newline.

We can also make use of System.exit() here. And if we pass 0 to the exit method, it means successful termination. And 1 means unsuccessful termination.  This is available in java.lang package.

And that completes our analysis of our first Java program. Let's now discuss how we can compile the Hello world Java program.

Compiling the Program

You first require to type this program in a text editor and save it. The file name will be the class name that you have mentioned in your program... Here it will hence be demo.java.

And in the next step, we need to open the console window, and then we need to navigate to the directory where the program is saved.  For compiling the program we need to make use of the command:

javac demo.java

You need to understand that java is case sensitive, and hence make sure that you are typing the file name in correct format.

If the program is executed successfully, then a demo.class file gets generated, and this is independent of the machine and also portable by nature.

And now that we have done the program compilation successfully, we need to execute the Hello world program in Java for getting the output.

Executing the Program

For executing the Hello World java program via the command line, we require typing the below code, which is 

java demo

Congratulations, you have executed your first Java program successfully. 

If you are using any IDE, you can omit all these steps, and just tap on execute button there, and execute your first java program.

And that finishes our article on the first program written by any beginner in Java. If you want to learn more you can join our Java course. And for that, you can contact us anytime and from anywhere in the world.

Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let's have a look at what you will get with this Java training package:

  • You need to pay a nominal fee.

  • You can choose any Java certification, as per your skills and interest.

  • You have the option to select from online and classroom training.

  • A chance to study at one of the best Java training institutes in India 

  • We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.

  • Naresh I technologies cater to one of the best Java training in India.

  • And a lot more is waiting for you.

Contact us anytime for your complete Java online training.