(Link for Concepts and features of OOPs)
Java Definition and Features are as follows: –
Definition of Java
- Java is a simple, scalable (easy to integrate), object-oriented (able to program real-life complexities), class-based, concurrent, general purpose, open source, high-level programming language with powerful features, which can be used to develop a variety of applications from simple web animations to high-end business applications that program hand-held devices, microwave appliances, cross-platform server applications, etc.
Characteristics/Features of Java
-
Simple Structure – Java is considered a simple language because it is easy to learn, its syntax is quite simple, clean, and easy to understand –
-
Java is easy to learn for programmers because it is (syntax) similar to C and C++ and most of the complex parts of C/C++ have been excluded including operator overloading, multiple inheritance, storage classes, goto, and pointers.
- Approximately half of the bugs in C and C++ programs are related to memory allocation and de-allocation processes. But in Java memory allocation and de-allocation is automatic.
- In practice, java is both a simple as well as complex language depending on how we use it because Java has a wide range of applications, simple to complex.
-
-
Typed Language – Java is a strongly typed language i.e. they clearly distinguish between the compile time errors (that must be detected at compile time) and run-time errors (that occur at run time).
-
Object Oriented –
-
In Java, everything is considered an object which contains some data and its related methods/behaviors. Objects have two sections. The first is Data (instance variables) and the second is methods. Data represents what an object is. A method represents what an object does. The Data and methods are closely related to the real-world structure and behavior of objects. Object-oriented programming has several advantages like. Simpler to read program, efficient reuse of programming segments, robust and error-free code.
- Java is a true object-oriented language, which provides a platform to develop an effective and efficient application and program real-life complexities.
- Java does not allow methods without class, thus an application consists of only the object which makes it true OOP.
-
This object feature helps Java to extend easily as it is based on an object model.
- As we know all the codes of the Java program are written as a class and object form and follow & results oops output such as code re-usability, maintainability, etc.
-
- Compiled & Interpreted – Generally a computer programming language is either compiled or interpreted, but Java is both compiled as well as interpreted.
- A Java program/source code is first compiled/translated using Java compiler (which is known as a name javac) and results in a portable intermediate product/class file (not a machine code) called “Java byte code/Byte code”. Now this Java byte code is converted into final machine code by the Java Interpreter (which is known as a name java). This Byte code is an intermediate code and independent of any machine and any operating system. Now, the Java interpreter executes the translated byte codes directly on the system that implements the Java Virtual Machine to run/execute the Java program. This byte code is the actual power of Java to make it popular, platform-independent, and dominating over other programming languages.
- To execute the Java program successfully, first of all, it is necessary to compile it to make errors free and then it must be interpreted to get related output.
- Reliability & Robustness –
- Java provides checking for possible problems at two levels, one at the compile time
and the other at the run time, so programs are highly reliable and eliminate situations
that are error-prone. - Java is a robust(strong) language because it has strong memory management, automatic garbage collection, run time error/exception handling mechanism, type checking mechanism, etc. in Java. All these features make Java robust. In other words, Java eliminates pointer manipulation completely from the language and therefore eliminates a large source of run-time errors. Also, Java programmers
need not remember to de-allocate memory in programs since there is an automatic garbage collection mechanism that handles the de-allocation of memory. Thus, It provides a powerful and robust exception-handling mechanism to deal with both expected and unexpected errors at run time.
- Java provides checking for possible problems at two levels, one at the compile time
- Secure – As we know Java is intended to be used in networking/distributing environments so it is necessary to implement several security mechanisms to protect against malicious code that might try to invade our file system.
- The absence of pointers in Java makes it impossible for applications to gain access to memory locations without proper authorization as the memory allocation and referencing model is completely opaque to the programmer and controlled entirely by the underlying run-time platform.
- Since Java program is both compiled and interpreted hence these two steps of compilation and interpretation also allow extensive code checking and improved security.
- Java program always runs in a Java run time environment with almost null interaction with OS, hence it is more secure.
- In Java, All the references to memory are symbolic references which is one of the security mechanisms in Java, i.e. the user is not aware of where in the memory program is present, it depends on the JVM and machine on which the program is running.
- Each Java applet program is loaded on its own memory space, which avoids the information interchange between applets and thus secures the information.
- Each Java applet program can be executed in its own run time environment that restricts it from viruses, deleting, and modifying files in the host computer.
- The Java-enabled web browser checks the byte code of applets to ensure that it does not do anything wrong before it runs the applet.
- Java is also a strongly typed language, which means that variables should be declared and variables should not change types. Also, typecasting is strictly limited or highly sensible in Java.
- In Java, it is easier to write bug-free code than in other languages because unintended bugs are responsible for more data loss than viruses.
-
Platform Independent –
-
A platform, in computer science, is the combination of hardware and software environment but practically software environment/operating system is considered mostly in which a program runs.
-
This means that the programs written on one platform can run on any other platform without having to rewrite or recompile them. In other words, it follows the ‘WORA’ approach.
-
As we know Java programs are compiled into a byte-code format which does not depend on any machine architecture hence it can be easily translated into a specific machine by a Java Virtual Machine (JVM) for that machine. This is a significant advantage when developing applets or applications that are downloaded from the Internet and are needed to run on different systems.
- A Java program can run on any computer system/environment (such as Linux, Windows, Mac, etc.) for which a JVM (Java Virtual Machine) and some library routines have been installed.
-
- Architecture Neutral – Here, architecture-neutral means Java produces bytecodes that are independent of any platform/architecture i.e. it can be now easily translated into a machine code for a specific platform/architecture whose JVM receives it.
- Dynamic –
- Java shows dynamic features which means that we can add more classes and plug in new methods to classes, creating required new classes as sub-classes, etc. This makes Java very easy to augment/dynamic as per our requirements.
- Java was specially designed to adapt to an evolving environment because the Java compiler is smart and dynamic.
- Java compiler can also determine whether a source code has been changed since the last time it was compiled.
- In Java, classes that were unknown to a program when it was compiled can still be loaded into it at run time. For example, a web browser can load applets of other classes without recompilation.
- When we are compiling a Java file that depends on other non-compiled Java files, then the compiler will try to find and compile them also.
- The compiler can also handle methods that are used before they’re declared.
- Java compiler can also determine whether a source code has been changed since the last time it was compiled.
- Distributed –
- Java is considered a distributed programming language because the source code/program of Java can be compiled onto one local machine and easily transferred(via the internet) to another local or remote machine (either located near or far) where it can be executed successfully with no change because of bytecode presence.
- To enable Java applications to execute anywhere on the network, the compiler generates an architecture-neutral object file format/bytecode.
- Due to its distributed nature, the Java program can be designed to run in networking environments. For this, java has an extensive library of classes for communicating, using TCP/IP protocols such as HTTP and FTP mainly. This makes creating network connections much easier. Thus, we can read and write objects on remote sites via URL with the same ease that programmers are used to when reading and writing data from and to a file. This helps the programmers at remote locations to work together on the same project.
- Java’s advanced features RMI and EJB are used for creating distributed applications.
- Web & Network – Java can create network and web applications much more.
- J2EE is used for developing network-based applications.
- The applet is used for developing client-side web applications mainly.
- JSP is used for developing server-side web applications mainly.
- Multithreading –
- Since the Java platform is designed with multithreading capabilities, built into the language as well. Thus we can build Java applications with many concurrent threads of activity, resulting in highly interactive and responsive applications. Thus, java handles more than one job at a time, so gets more processes done in less time than it could with just one thread.
- By using threads, an individual program is capable of doing more than one thing at the same time efficiently.
- Java is inherently multi-threaded i.e. A single Java program can have many different threads executing independently and continuously. for example, different Java applets on the same web page can run together while getting equal time from the processor.
- To make use of threads Java offers features for synchronization between threads.
- High Performance –
- Java source code is first compiled into bytecode which is highly optimized by the Java compiler, so that the Java virtual machine (JVM)/Interpreter can execute Java applications at full speed/efficiency hence giving an efficient output.
- The advanced version of JVM uses the adaptive and Just-in-time (JIT) compilation technique that improves performance by converting Java bytecodes to native machine instructions on the fly.
- In general, interpreters are slow, because an interpreter executes programs instruction by instruction while Java is a fast-interpreted language.
- Java has also been designed so that the run-time system can optimize its performance by compiling bytecode to native machine code on the fly (execute immediately after compilation). This is called a “just in time” (JIT) compilation.
- Portable – Java program is a write once, run anywhere (WORA) programming language i.e. java uses the WORA concept which makes it portable. The portability comes from the architecture-neutrality concept.
- Some other features of Java are –
- Java is a strictly case-sensitive programming language.
- They are considered pure object-oriented programming languages because all the codes in Java are included inside the class, even the main method.
Java Virtual Machine(JVM)
- JVM stands for “Java Virtual Machine”. They were designed by Sun MicroSystems and introduced in 1994. Sun JVM is written in C whereas Oracle JVM is written in C++.
- JVM is an abstract/virtual computing machine that provides a runtime environment in which Java bytecode(compiled program of Java) can be executed. Here, JVM forms a key part of the Java Runtime Environment (JRE), and the Java Development Kit (JDK) and Java interpreter are the parts of the Java runtime environment.
- JVM plays a crucial role in the execution of Java applications, providing a consistent, secure, and efficient runtime environment across different platforms.
- The objective of JVM is to provide an execution environment for applications built to run on it.
- JVM is responsible for allocating the necessary memory needed by the Java program.
- When a Java program is compiled it is converted into intermediate byte code which is then executed by the Java interpreter by translating the byte code into machine instructions.
- JVM is an abstract computing model like a real machine, it has its own instruction set, and execution engine, and serves to manipulate memory areas at run time.
- The JVM is called virtual because it is generally implemented in software, on top of a real hardware platform and operating system.
- The JVM has two primary functions – to allow Java programs to run on any device or operating system and to manage and optimize program memory.
- JDK is for development purposes whereas JRE(Java Runtime Environment) is for running Java programs. Here, JDK and JRE both contain JVM so that we can run our Java program. The JRE is the environment within which the virtual machine runs. JRE is the container, and JVM is the content. Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc.
- JVM manages memory allocation, garbage collection, exception handling, and other runtime services required for executing Java programs in JRE.
- JVM is the heart of Java programming language that provides platform/OS independence. For this, JVM is responsible for executing the bytecode on different platforms, providing the ability to run Java applications on any device or operating system that has a compatible JVM implementation.
- JVM can interpret bytecode or use Just-In-Time (JIT) compilation to translate bytecode with the help of Java Interpreter into native machine code at runtime. Interpretation involves executing bytecode instructions one by one, while JIT compilation translates bytecode into native machine code for improved performance.
- JVM provides a secure execution environment for Java applications.
- JVM dynamically loads classes and resolves dependencies at runtime using class loaders.
- JVM includes various optimization techniques and profiling tools to improve the performance of Java applications.
- JVM is a platform-independent execution environment that converts Java bytecode into machine language and executes it.
- JVM is the main component of Java architecture, and it is part of the JRE (Java Runtime Environment).
- The JVM plays the main role in making Java portable. It provides a layer of abstraction between the compiled Java program and the hardware platform and operating system.
- Any machine for which a Java interpreter is available can execute the compiled byte code. That’s why Java is called machine-independent and Architecture-neutral.
Simple Java Program Example
Create and Run Java Programs –
- STEP 1 – First of all write a Java Program in any simple Text Editor Program (such as Notepad, DOS editor, Brief, vi editor of Linux or Unix, etc.) as below –
{
Line 4 : public static void main (String args[ ] )
{
Line 5 : System.out.println(“Simple Java Application Program Examples”);
}
}
- STEP 2 – Save the created Java program in a particular computer drive (say c drive) with a certain name having extension .java such as program1.java.
- STEP 3 – Now, compile the created java program(program1.java) with java compiler(javac) by opening dos prompt(run – cmd) – c:\>javac program1.java (Press Enter button)
- STEP 4 – Remove the errors in the program, if any, and finally save the program.
- STEP 5 – Now, Interpret the compiled class/bytecode file(as class name, here it is Example.class) with java Interpreter(java) as – c:\>java Example (Press Enter button)
- STEP 6 – Result displayed on the DOS screen.
NB 1 : – Java programs can also be written and run using Java IDE applications(such as Netbeans, eclipse, Bluej, etc)without involving DOS.
NB 2 : – We can not use a heavy text editor to write Java programs because this editor such as a word processor like Microsoft Word saves their files in a proprietary format and not in pure ASCII text.
NB 3 : – Before compiling a Java program in a computer system we must first ensure that our Java environment is correctly configured. To set this PATH and CLASSPATH is properly set.
Explanation of Java Program –
Line 1 : Comments Line – In Java, two types of comment lines is applicable called Single line(//) and Multiple line( /* – – – */ ). Here, a comment line is used to describe the details of the program which is useful for developing the readability/understandability of the program. This comment line is ignored by the compiler.
Line 2 : Required packages are included to provide useful system-defined methods of a particular class, applied in the codes using the import keyword.
Line 3 : To define a Java class(here is an Example) using a keyword class that includes everything inside it.
Line 4 : Definition of Java Main() methods as public static void main (String args[ ] ). This is the starting point from where the Java program started for execution calling the main() method. Here each keyword has a unique meaning –
static – This keyword allows the main () method to be executed without creating an object of that class.
Create and Run Applet Program –
- To create an applet program, 3 steps can be followed –
1. Create an applet program file in a simple text editor program say Notepad
public class Program1 extends Applet
{
public void paint (Graphics g)
{
g.drawString(“Hello India!”, 150, 200);
}
}
2. Compile the source/program file as a Java application program
Now the ready Java applet program is compiled using a Java compiler named ‘Javac’.
To compile this we have –
C:\> Javac Program1.Java
After successful compilation, the Program1.class file is created as an output of compilation.
3. Run the applet program
To execute the applet program finally, we now create an HTML file with a .html extension again which includes the given applet class file Program1.class, and then run this HTML file either using java enabled web browser(such as Internet Explorer, Netscape Navigator, etc) or applet viewer, a java plug-in tool. The HTML file is –
<HTML>
<HEAD>
<TITLE> A Simple Applet </TITLE>
</HEAD>
<BODY>
The output of the applet program is
<APPLET CODE=” Program1.class” WIDTH=150 HEIGHT=50></APPLET>
</BODY>
</HTML>
Simply run this created HTML file(say Program2.html) by double-clicking on it It opens in the web browser and displays the output.
NB : Both the files of the applet program(say here Program1.java and Program2.html) must be kept in the same directory or folder.
Explanation of Applet Program –
- Here, in the code java.applet.Applet package is required to create an applet program and java.awt.Graphics package is required to display/paint the output of the applet program on the screen.
- Next, Program1 is the executable class which is public in nature and it extends from the applet class which means that a programmer builds the applet code on the standard Applet class.
- In the Program1 applet class, we have defined the paint method inside which we are using the object of Graphics class. Graphics class is available in Java.awt package.
- The drawString method is used to write/display the “Hello India!” message on the screen. It is a method of Graphic class. This drawString method takes three arguments. The first argument is a message and the other two are pixel positions or locations on the X-axis and Y-axis respectively, from where the printing/output of the string on the screen will begin.
For more details about applet theory & programs, click this link.
0 Comments