Automation QA Testing Course Content

Java Introduction

What is Java

Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, so James Gosling and his team changed the Oak name to Java

Platform: Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform.

History of Java

The history of Java is very interesting. Java was originally designed for interactive television, but it was too advanced technology for the digital cable television industry at the time. The history of Java starts with the Green Team. Java team members (also known as Green Team), initiated this project to develop a language for digital devices such as set-top boxes, televisions, etc. However, it was suited for internet programming. 
 Java was developed by James Gosling, who is known as the father of Java, in 1995. James Gosling and his team members started the project in the early '90s.

1) James GoslingMike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers called Green Team.
2) Initially designed for small, embedded systems in electronic appliances like set-top boxes.
3) Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.

Why Java named "Oak"?


5) Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries like the U.S.A., France, Germany, Romania, etc.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak Technologies


According to Sun, 3 billion devices run Java. There are many devices where Java is currently used. Some of them are as follows:
  1. Desktop Applications such as acrobat reader, media player, antivirus, etc.
  2. Web Applications such as irctc.co.in, javatpoint.com, etc.
  3. Enterprise Applications such as banking applications.
  4. Mobile
  5. Embedded System
  6. Smart Card
  7. Robotics
  8. Games, etc.

Types of Java Applications

There are mainly 4 types of applications that can be created using Java programming:

1) Standalone Application

Standalone applications are also known as desktop applications or window-based applications. These are traditional software that we need to install on every machine. Examples of standalone application are Media player, antivirus, etc. AWT and Swing are used in Java for creating standalone applications.

2) Web Application

An application that runs on the server side and creates a dynamic page is called a web application. Currently, Servlet, JSP, Struts, Spring, Hibernate, JSF, etc. technologies are used for creating web applications in Java.

3) Enterprise Application

An application that is distributed in nature, such as banking applications, etc. is called enterprise application. It has advantages of the high-level security, load balancing, and clustering. In Java, EJB is used for creating enterprise applications.

4) Mobile Application

An application which is created for mobile devices is called a mobile application. Currently, Android and Java ME are used for creating mobile applications.

Java Platforms / Editions

There are 4 platforms or editions of Java:

1) Java SE (Java Standard Edition)

It is a Java programming platform. It includes Java programming APIs such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. It includes core topics like OOPs, String, Regex, Exception, Inner classes, Multithreading, I/O Stream, Networking, AWT, Swing, Reflection, Collection, etc.

2) Java EE (Java Enterprise Edition)

It is an enterprise platform which is mainly used to develop web and enterprise applications. It is built on the top of the Java SE platform. It includes topics like Servlet, JSP, Web Services, EJB, JPA, etc.

3) Java ME (Java Micro Edition)

It is a micro platform which is mainly used to develop mobile applications.

4) JavaFX

It is used to develop rich internet applications. It uses a light-weight user interface API.

Java Version History

Many java versions have been released till now. The current stable release of Java is Java SE 10.
  1. JDK Alpha and Beta (1995)
  2. JDK 1.0 (23rd Jan 1996)
  3. JDK 1.1 (19th Feb 1997)
  4. J2SE 1.2 (8th Dec 1998)
  5. J2SE 1.3 (8th May 2000)
  6. J2SE 1.4 (6th Feb 2002)
  7. J2SE 5.0 (30th Sep 2004)
  8. Java SE 6 (11th Dec 2006)
  9. Java SE 7 (28th July 2011)
  10. Java SE 8 (18th Mar 2014)
  11. Java SE 9 (21st Sep 2017)
  12. Java SE 10 (20th Mar 2018)
  13. Java SE 11 (September 2018)
  14. JAVA SE 12 (March 2019)
  15. JAVA SE 13(September 2019)
  16. JAVA SE 14(March 2020)
  17. JAVA SE 15(September 2020)
  18. JAVA SE 16(March 2021)
  19. JAVA SE 17(LTS)(September 2021)
  20. Java SE 186222th March 2022September 2022 for OpenJDK and Adoptium
    Java SE 196320th September 2022March 2023 for OpenJDK
    Java SE 20March 2023September 2023 for OpenJDK
    Java SE 21 (LTS)September 2023September 2028September 2031 for Oracle[9]


Features of Java

The primary objective of Java programming language creation was to make it portable, simple and secure programming language. Apart from this, there are also some excellent features which play an important role in the popularity of this language. The features of Java are also known as java buzzwords.
A list of most important features of Java language is given below.
Java Features
  1. Simple
  2. Object-Oriented
  3. Portable
  4. Platform independent
  5. Secured
  6. Robust
  7. Architecture neutral
  8. Interpreted
  9. High Performance
  10. Multithreaded
  11. Distributed
  12. Dynamic

Simple

Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun, Java language is a simple programming language because:
  • Java syntax is based on C++ (so easier for programmers to learn it after C++).
  • Java has removed many complicated and rarely-used features, for example, explicit pointers, operator overloading, etc.
  • There is no need to remove unreferenced objects because there is an Automatic Garbage Collection in Java.

Object-oriented

Java is an object-oriented programming language. Everything in Java is an object. Object-oriented means we organize our software as a combination of different types of objects that incorporates both data and behavior.
Object-oriented programming (OOPs) is a methodology that simplifies software development and maintenance by providing some rules.
Basic concepts of OOPs are:
  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation

Platform Independent


Java is platform independent
Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. A platform is the hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides a software-based platform.
The Java platform differs from most other platforms in the sense that it is a software-based platform that runs on the top of other hardware-based platforms. It has two components:
  1. Runtime Environment
  2. API(Application Programming Interface)
Java code can be run on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. Java code is compiled by the compiler and converted into bytecode. This bytecode is a platform-independent code because it can be run on multiple platforms, i.e., Write Once and Run Anywhere(WORA).

Secured

Java is best known for its security. With Java, we can develop virus-free systems. Java is secured because:
  • No explicit pointer
  • Java Programs run inside a virtual machine sandbox
how Java is secured
  • Classloader: Classloader in Java is a part of the Java Runtime Environment(JRE) which is used to load Java classes into the Java Virtual Machine dynamically. It adds security by separating the package for the classes of the local file system from those that are imported from network sources.
  • Bytecode Verifier: It checks the code fragments for illegal code that can violate access right to objects.
  • Security Manager: It determines what resources a class can access such as reading and writing to the local disk.
Java language provides these securities by default. Some security can also be provided by an application developer explicitly through SSL, JAAS, Cryptography, etc.

Robust

Robust simply means strong. Java is robust because:
  • It uses strong memory management.
  • There is a lack of pointers that avoids security problems.
  • There is automatic garbage collection in java which runs on the Java Virtual Machine to get rid of objects which are not being used by a Java application anymore.
  • There are exception handling and the type checking mechanism in Java. All these points make Java robust.

Architecture-neutral

Java is architecture neutral because there are no implementation dependent features, for example, the size of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for 64-bit architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.

Portable

Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't require any implementation.

High-performance

Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc.

Distributed

Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the internet.

Multi-threaded

A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc.

Dynamic

Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.


Difference between JDK, JRE, and JVM


Java Development Kit (JDK)

The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), and other tools needed in Java development.

Java Runtime Environment (JRE)

The Java Runtime Environment provides the minimum requirements for executing a Java application. It consists of the Java Virtual Machine (JVM), java core packages, classes, and supporting files.

Java Virtual Machine (JVM)

The Java Virtual Machine is a specification that provides a runtime environment in which java bytecode can be executed. It means JVM creates a platform to run Java bytecode(.class file) and converting into different languages (native machine language) which the computer hardware can understand. Actually, there is nothing to install as JVM. When the JRE is installed, it will deploy the code to create a JVM for the particular platform. JVMs are available for many hardware and software platforms.

Java Virtual Machine Architecture

The JVM consists of three distinct components:

  1. Class Loader
  2. Runtime Memory/Data Area
  3. Execution Engine

Let's take a look at each of them in more detail.

ClassLoader

ClassLoader is responsible for loading class file into the memory area. The classloader is an abstract class, generates the data which constitutes a definition for the class using a class binary name which is constituted of the package name, and a class name. 

Now you got a clear idea about the inside mechanism of the ClassLoader. There are three types of ClassLoaders as shown below.

  • Bootstrap ClassLoader — Load classes from JRE/lib/rt.jar
  • Extension ClassLoader — Load classes from JRE/lib/ext
  • Application/System ClassLoader — Load classes from classpath, -cp, Manifest

  • Bootstrap Class Loader - This is the root class loader. It is the superclass of Extension Class Loader and loads the standard Java packages like java.langjava.netjava.utiljava.io, and so on. These packages are present inside the rt.jar file and other core libraries present in the $JAVA_HOME/jre/lib directory.
  • Extension Class Loader - This is the subclass of the Bootstrap Class Loader and the superclass of the Application Class Loader. This loads the extensions of standard Java libraries which are present in the $JAVA_HOME/jre/lib/ext directory.
  • Application Class Loader - This is the final class loader and the subclass of Extension Class Loader. It loads the files present on the classpath. By default, the classpath is set to the current directory of the application. The classpath can also be modified by adding the -classpath or -cp command line option.

The classLoading mechanism consists of three main steps as follows.

  • Loading
  • Linking
  • Initialization

Loading

Whenever JVM loads a file, it will load and read,

  • Fully qualified class name
  • Variable information (instance variables)
  • Immediate parent information
  • Whether class or interface or enum

When the class is loaded to the JVM, it creates an object of class type and is put into the heap area. This class type object will be created only the very first time the class is loaded to the JVM.

Linking

This is the process of linking the data in the class file into the memory area. It begins with verification to ensure this class file and the compiler.

  1. Make sure this compiler is valid
  2. The class file has the correct formating
  3. The class file has the correct structure

When considering security threads, there is a possibility that some programs like malware can change or add some content to the class file. In this case, it will be identified by the byte code verifier and throw an exception called “verify exception”.

When the verification process is done, the next step is preparation. In this stage, all the variables are initialized with the default value. As an example, it will assign 0 for int variables, null for all objects, false for all boolean variables, etc.

Java allows you to use any domain-specific words in your java program. As an example, you can create a class by giving any name (except java keywords). But the JVM cannot understand these words. Therefore, JVM replaces these words from a memory address. That process is called

  • a boolean, which is false.

Resolution: In this phase, symbolic references are replaced with direct references present in the runtime constant pool.

F

private static f resolution. At the end of these three steps, the java file is loaded to the memory area.

The JVM uses the ClassLoader.loadClass() method for loading the class into memory. It tries to load the class based on a fully qualified name.

If a parent class loader is unable to find a class, it delegates the work to a child class loader. If the last child class loader isn't able to load the class either, it throws NoClassDefFoundError or ClassNotFoundException

Initialization

This is the final stage of class loading. In this stage, the actual values are assigned to all static and instance variables. There is a rule that every class must be initialized before doing any active use. There are six active uses as follows.

  1. Use new keyword.
  2. Invoke static methods.
  3. Assign values for static fields.
  4. Initialize class.
  5. Use getInstance() in Reflection API.
  6. Instantiation of sub-class.

There are 4 ways to initialize a class in Java.

  1. New Keyword: Goes through the normal initialization process.
  2. Reflection API: getInstance() method and goes through the normal initialization process.
  3. Clone Method: Gets the information from the source object.
  4. IO.ObjectInputStream: Gets data from non-transient variables passed in the parameter.



Memory Area

This is the place data will store until the program is executed. It consists of 5 major components as follows.

There are five components inside the runtime data area:

image-32

Let's look at each one individually.

Method Area

This is a shared resource (only 1 method area per JVM). Method area stores class-level information such as

  • ClassLoader reference
  • Run time constant pool
  • Constructor data — Per constructor: parameter types (in order)
  • Method data — Per method: name, return type, parameter types (in order), modifiers, attributes
  • Field data — Per field: name, type, modifiers, attributes

Heap Area

All the objects and their corresponding instance variables are stored in the heap area. As an example, if your program consists of a class called “Employee” and you are declaring an instance as follows,

Employee employee = new Employee();

When the class is loaded, there is an instance of employee is created and it will be loaded into the Heap Area.

Stack Area

There are separate stack areas for each thread. The stack is responsible for hold the methods (method local variables, etc) and whenever we invoke a method, a new frame creates in the stack. These frames use Last-In-First-Out structure. They will be destroyed when the method execution is completed.

Program Counter (PC) Register

PC Register keeps a record of the current instruction executing at any moment. That is like a pointer to the current instruction in a sequence of instructions in a program. Once the instruction is executed, the PC register is updated with the next instruction. If the currently executing method is ‘native’, then the value of the program counter register will be undefined.

Native Method Area

A native method is a Java method that is implemented in another programming language, such as C or C++. This memory area is responsible to hold the information about these native methods.

Execution Engine

Once the bytecode has been loaded into the main memory, and details are available in the runtime data area, the next step is to run the program. The Execution Engine handles this by executing the code present in each class.

However, before executing the program, the bytecode needs to be converted into machine language instructions. The JVM can use an interpreter or a JIT compiler for the execution engine.

image-33

Interpreter

The interpreter reads and executes the bytecode instructions line by line. Due to the line by line execution, the interpreter is comparatively slower.

Another disadvantage of the interpreter is that when a method is called multiple times, every time a new interpretation is required.

JIT Compiler

The JIT Compiler overcomes the disadvantage of the interpreter. The Execution Engine first uses the interpreter to execute the byte code, but when it finds some repeated code, it uses the JIT compiler.

The JIT compiler then compiles the entire bytecode and changes it to native machine code. This native machine code is used directly for repeated method calls, which improves the performance of the system.

The JIT Compiler has the following components:

  1. Intermediate Code Generator - generates intermediate code
  2. Code Optimizer - optimizes the intermediate code for better performance
  3. Target Code Generator - converts intermediate code to native machine code
  4. Profiler - finds the hotspots (code that is executed repeatedly)

To better understand the difference between interpreter and JIT compiler, assume that you have the following code:

int sum = 10;
for(int i = 0 ; i <= 10; i++) {
   sum += i;
}
System.out.println(sum);

An interpreter will fetch the value of sum from memory for each iteration in the loop, add the value of i to it, and write it back to memory. This is a costly operation because it is accessing the memory each time it enters the loop.

However, the JIT compiler will recognize that this code has a HotSpot, and will perform optimizations on it. It will store a local copy of sum in the PC register for the thread and will keep adding the value of i to it in the loop. Once the loop is complete, it will write the value of sum back to memory.

Note: a JIT compiler takes more time to compile the code than for the interpreter to interpret the code line by line. If you are going to run a program only once, using the interpreter is better.

Garbage Collector

The Garbage Collector (GC) collects and removes unreferenced objects from the heap area. It is the process of reclaiming the runtime unused memory automatically by destroying them.

Garbage collection makes Java memory efficient because because it removes the unreferenced objects from heap memory and makes free space for new objects. It involves two phases:

  1. Mark - in this step, the GC identifies the unused objects in memory
  2. Sweep - in this step, the GC removes the objects identified during the previous phase

Garbage Collections is done automatically by the JVM at regular intervals and does not need to be handled separately. It can also be triggered by calling System.gc(), but the execution is not guaranteed.

The JVM contains 3 different types of garbage collectors:

  1. Serial GC - This is the simplest implementation of GC, and is designed for small applications running on single-threaded environments. It uses a single thread for garbage collection. When it runs, it leads to a "stop the world" event where the entire application is paused. The JVM argument to use Serial Garbage Collector is -XX:+UseSerialGC
  2. Parallel GC - This is the default implementation of GC in the JVM, and is also known as Throughput Collector. It uses multiple threads for garbage collection, but still pauses the application when running. The JVM argument to use Parallel Garbage Collector is -XX:+UseParallelGC.
  3. Garbage First (G1) GC - G1GC was designed for multi-threaded applications that have a large heap size available (more than 4GB). It partitions the heap into a set of equal size regions, and uses multiple threads to scan them. G1GC identifies the regions with the most garbage and performs garbage collection on that region first. The JVM argument to use G1 Garbage Collector is -XX:+UseG1GC

Note: There is another type of garbage collector called Concurrent Mark Sweep (CMS) GC. However, it has been deprecated since Java 9 and completely removed in Java 14 in favour of G1GC.

Java Native Interface (JNI)

At times, it is necessary to use native (non-Java) code (for example, C/C++). This can be in cases where we need to interact with hardware, or to overcome the memory management and performance constraints in Java. Java supports the execution of native code via the Java Native Interface (JNI).

JNI acts as a bridge for permitting the supporting packages for other programming languages such as C, C++, and so on. This is especially helpful in cases where you need to write code that is not entirely supported by Java, like some platform specific features that can only be written in C.

You can use the native keyword to indicate that the method implementation will be provided by a native library. You will also need to invoke System.loadLibrary() to load the shared native library into memory, and make its functions available to Java.

Native Method Libraries

Native Method Libraries are libraries that are written in other programming languages, such as C, C++, and assembly. These libraries are usually present in the form of .dll or .so files. These native libraries can be loaded through JNI.

Common JVM Errors

  • ClassNotFoundExcecption - This occurs when the Class Loader is trying to load classes using Class.forName()ClassLoader.loadClass() or ClassLoader.findSystemClass() but no definition for the class with the specified name is found.
  • NoClassDefFoundError - This occurs when a compiler has successfully compiled the class, but the Class Loader is not able to locate the class file at the runtime.
  • OutOfMemoryError - This occurs when the JVM cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.
  • StackOverflowError - This occurs if the JVM runs out of space while creating new stack frames while processing a thread.

---------------------------------------------------------------------------------------------

Refer below link for more information on JVM:

https://www.freecodecamp.org/news/jvm-tutorial-java-virtual-machine-architecture-explained-for-beginners/

--------------------------------------------------------------------------------------------------------


JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.
The implementation of JVM is also actively released by other companies besides Sun Micro Systems.
JRE

JDK

JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:
  • Standard Edition Java Platform
  • Enterprise Edition Java Platform
  • Micro Edition Java Platform
The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), etc. to complete the development of a Java Application.
JDK

CORE JAVA INTRODUCTION QUESTIONS?

1)What is Java?

2)who is the father of java?

3)who is in green team?

4)where and all java is used?

5)what are different types of java applications?

6)what are different types of java editions?

7)what is java compiler name?

8)why java is platform independent?

9)what are different tasks performed by JVM?

10)what is the difference between JRE, JDK ?

11)Explain about Internal architecture of JVM?

12)WORA

What does WORA in Java stand for?
a)Write Once Run Anywhere
b)Westside One Residents' Association
c)When Online RPGs Attack
d)Weave On Right Advantage
13)

Demystifying 'System.out.println()' in Java

In the Java programming language, what occurs when you invoke the 'System.out.println()' function and why?

14)

Not a black box

Here is a classic "Hello, World!" program — a friendly greeting from your computer. What do its first three lines do?

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

You may not understand every word as when learning a foreign language. Try to use your language intuition and associate lines with their functions.

Match the items from below
1)public class HelloWorld  
2)public static void main(String[] args)
3)System.out.println("Hello, World!");








a)This is where execution begins. It's the heart of our program.








b)
We're telling Java to create a new public class. This is like the birth certificate of our program.

































































c)These are our program's first words! This command instructs Java to print "Hello, World!".





























































































































No comments:

Post a Comment

Note: Only a member of this blog may post a comment.