Introduction to java and its features
-
Last Updated: July 1, 2023
-
By: javahandson
-
Series
Java is a high-level object-oriented programming language that focuses on real-world entities. Java is very simple to use, it is highly secured, multithreaded and platform-independent. Java was developed at Sun Microsystems under the guidance of James Gosling in the 1990s who is known as the Father of Java.
Java is quite similar to C and C++ but it omits many of the features that make C and C++ complex and unsafe. Some of the features which are omitted from Java are pointers, multiple inheritance, operator overloading etc.
Java programming language is mainly designed to develop web applications by providing platform independency. Using C and C++ we can develop only standalone applications which means we can access applications on only one system where the application is running. But using Java we can develop stand-alone as well as web applications that can be executed from remote systems using network calls.
There is no abbreviation for Java. But while developing the Java language the engineers use to have a lot of coffee which helped them to stay focused and have increased energy levels that is the reason we will see a coffee cup in the logo of Java.
J2SE ( Java 2 standard edition ) – J2SE is used for developing stand-alone and client-side applications.
J2EE ( Java 2 enterprise edition ) – J2EE is used for developing web and server-side applications.
J2ME ( Java 2 micro edition ) – J2ME is used for developing micro or mobile applications.
Source Code – It is a program written by developers. Developers have to follow the language-specific syntax to write the code.
Compiled code – Developers write the source code in high-level language but our machine cannot understand the high-level code. So we have to convert it into low-language code. A compiler converts the source code into low language and this converted code is called compiled code or byte code.
Execution – It is the process of running the byte code to get the output.
Java Virtual Machine – JVM is a program available as part of Java software. JVM reads the compiled code line by line and converts it into the native understanding format of the operating system.
Note* The Java source code we write should be compiled first using the Java compiler. The compiler converts the source code to byte code. This byte code helps to achieve platform independence. JVM will execute the byte code to give the output.
Java has removed some complex and unsafe features like pointers.
Java provides a large set of APIs that helps us to perform many operations. API consists of packages that again contain sub-packages, classes and interfaces.
Ex. String class is available in java.lang package. String class is having different functions which help us to perform different operations like :
concat – Concatenates the specified string to the end of this string.
split – Splits this string around matches of the given regular expression.
equals – Compares this string to the specified object
Java provides an inbuilt garbage collector which is used to free and manage unused memory. As a developer, we do not have to free the memory.
If we write a Java program and compile that source code on one operating system and if we are able to execute that compiled code on another operating system then we can say the code written is platform-independent. The programming language that is used to create such programs is called platform-independent programming language. Java is a platform-independent language as the Java program compiled code can run on all operating systems.
Java programs can run on any platform without recompiling. We just have to compile the code once and we can port that byte code to any other machines and execute them without recompiling. Java is designed in such a way to run on each and every processor without considering its architecture. Java compiler and JVM together help us to achieve this feature.
In Java, we can create multithreaded applications. Using multithreading we can create multiple threads in an application that will help us to execute a single task parallelly. Multithreaded application takes less time to complete. It manages the workload of the OS properly and it doesn’t allow OS to sit idle which in turn optimizes the execution time of the application.
Ex. Say we have to read all the text files in our system. Now our system contains multiple directories and in every directory, we will have multiple text files. Here if we use only a single thread application then that single thread will sequentially traverse from one directory to another and get the text files which will consume a lot of time. But if we create multiple threads in an application then each thread can check an individual directory and fetch the text files just from that directory. This will reduce the execution time of the program and it can complete in minutes.
The compilation of byte code to machine native code happens in JVM. JIT compiler is a part of JVM. Just In Time ( JIT ) compiler helps in improving the performance of Java programs while compiling the byte code into native operating system code. JIT compiles the frequently used methods once and stores them in the memory. It will not compile the frequently used method twice which in turn helps to improve the performance.
JIT also has some inbuilt ways to optimize the code. Ex. If we write a code like an int sum = a + b; But say we are not using this sum variable anywhere so JIT will analyze this and it will not execute this instruction which in turn saves time and also improves performance.
An Inbuilt Garbage collector in Java also helps to improve the performance of the system by freeing up unused memory space. We as a developer do not have to write extra pieces of code which reduces the efforts as well.
Robustness means it is an ability to handle exceptions while running a program. Java throws an exception when it encounters something unusual and we as a developer can handle it.
Ex. int result = 10 / 0 ; // JVM will throw an Arithmetic exception
The exception thrown by JVM is understandable by programmers but not by the application user. Java provides a feature to handle such exceptions which is known as exception handling. Using exception handling we can handle the exception and generate a user-friendly message that a normal user can understand.
package com.javahandson; public class ExceptionHandling { public static void main(String[] args) { try { int result = 10 / 0 ; } catch (ArithmeticException exception) { System.out.println("A number cannot be divided by zero"); } } } Output : A number cannot be divided by zero ( a normal user will not see the ArithmeticException instead they will see this message )
Java contains a dedicated package ‘java.security.*’ for performing security operations like encryption, signature and hashing. We as a developer need not own security code as we can make use of the existing security package that is provided by Java.
This security API or package helps to protect confidential data and also helps to maintain the integrity of data.
There are 2 types of memory allocation techniques one is static and the other is dynamic. In static memory allocation the memory is allocated at compile time whereas in dynamic the memory is allocated at runtime.
Drawbacks of static memory allocation:
Java is dynamic hence the memory allocation happens at the time of execution and both the above problems are taken care of. We make use of a new operator to allocate the memory at runtime.
The purpose of networking is to share information between multiple machines that are located either in the same area or distant areas.
Java is specifically designed to create internet applications that we can access from any part of the world. Likewise, we do have intranet applications that we can access at a limited distance.
Java contains a separate set of APIs to develop internet and intranet applications. We can create internet applications using J2EE technology whereas we can create intranet applications using J2SE technology.
Using Java we can create distributed applications which means a single application can run on multiple machines instead of one. One machine will perform a set of tasks and the other machine will perform a different set of tasks. These machines can communicate together to give a single result.
In a distributed system we will have many components. One component will serve one purpose and all these components talk to each other to achieve a common goal.
Ex. Remote method invocation ( RMI ) and Enterprise Java beans (EJBs) are a couple of concepts in Java that help us to achieve the distributed state.
C++ is platform dependent.
Java is platform-independent.
C++ supports pointers and allows developers to use pointers in the program.
Java supports pointers but internally, developers cannot make use of pointers in programs
C++ supports multiple inheritance.
Java does not support multiple inheritances to avoid naming ambiguity.
C++ uses only compilers. C++ programs are compiled and run using the compiler. The compiler itself converts the source code to machine code.
Java uses both a compiler and an interpreter. The compiler converts the source code to byte code and the interpreter converts the byte code to machine code.
C++ APIs do not have inbuilt thread support. C++ uses third part libraries to support multithreading.
Java APIs have inbuilt multithreading support.
Yes, JVM contains a JIT compiler and interpreter but the JIT compiler is different from the actual compiler which compiles the source code to byte code. The interpreter converts the byte code to machine code and it passes on the frequently executed byte code to the JIT compiler. JIT helps to improve the performance of Java code.
JVM is a program available as part of Java software. JVM reads the compiled code line by line and converts it into a native understanding format of the operating system and executes that. JVM is also known as a runtime machine.
Java is a platform-independent language as the compiled code of Java can run on any operating system ( But we need a JVM for that ). We don’t have to compile the code again, we can directly execute it on JVM. Compiled code and JVM makes Java a platform-independent language. Java is also known as write once and run anywhere language.
So this is all about an introduction to Java and its features. I hope you like the article. If you are having any questions on this topic please raise them in the comments section.