1. Java Introduction
Srinivas Prasad K T
Engineering Manager @ Decathlon Technology || Ex Harman || Ex E2Open || Oracle Java? || Spring Boot? || WDIO? || Playwright? || Java Script & Type Script? || Scrum? || Gatling? || AI? || Tricentis Tosca? || ACCELQ?
Core Java
Core Java Programming
??Basics of programming
Object Oriented Programming
Java Library
Introduction
What is Java?
What is a programming language?
???- A programming language is a formally constructed language designed to communicate instructions to a machine, particularly to a computer
??- Programming language is used to control the behavior of a machine.
?? Program- A program is a set of instructions given to a computer to control behaviors.
A program is made up of statements.
Programming languages are classified into 3 types
Low-level language?
High-level language
Assembly level language:-
Machine Language:-
Platform-?
Any hardware or software environment in which a program runs is known as a platform.
Since Java has its own runtime environment (JRE) and API. It is called a platform.
Where it is used?
There are many devices where Java is currently used. Some of them are as follows:
Types of Java Applications
There are mainly 4 types of applications that can be created using Java programming.
History of java
The history of Java starts with the Green Team. Java team members (also known as Green Team), initiated a revolutionary task to develop a language for digital devices such as set-top boxes, televisions, etc.
James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers is called the Green Team.
Originally designed for small, embedded systems in electronic appliances like set-top boxes.
Firstly, it was called "Green Talk" by James Gosling and the file extension was .gt.
After that, it was called Oak and was developed as a part of the Green project.
Why Oak? Oak is a symbol of strength and is chosen as a national tree of many countries like the U.S.A., France, Germany, Romania, etc.
In 1995, Oak was renamed "Java" because it was already a trademark by Oak Technologies.
Java Versions
There are many Java versions that have been released. The current stable release of Java is Java SE 8.
Features of java?
There are many features of Java. The Java Features given below are simple and easy to understand.
Simple
According to Sun, Java language is simple because:
??????Syntax is based on C++ (so easier for programmers to learn it after C++).
??????removed many confusing and/or rarely-used features e.g., explicit pointers, operator overloading, etc.
??????No need to remove unreferenced objects because there is an Automatic Garbage Collection in Java.
Object-oriented
Object-oriented means we organize our software as a combination of different types of objects that incorporate both data and behavior.
Object-oriented programming (OOP) is a methodology that simplifies software development and maintenance by providing some rules.
The basic concepts of OOPs are:
Platform Independent
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's a software-based platform that runs on top of other hardware-based platforms. It has two components:?
Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris, and Mac/OS etc. Java code is compiled by the compiler and converted into bytecode. This byte code is a platform-independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere (WORA)
Secured
Java is secured because:
Robust
Robust simply means strong. Java uses strong memory management. There is a lack of pointers that avoid security problems. There is automatic garbage collection in Java. There is an exception-handling and type-checking mechanism in Java. All these points make Java robust.?
Architecture-neutral
There are no implementation-dependent features e.g. size of primitive types is set.
Portable
We may carry the Java byte code to any platform.?
High-performance
Java is faster than traditional interpretation since byte code is "close" to native code but still somewhat slower than a compiled language (e.g., C++)?
Distributed
We can create distributed applications in Java. RMI (Remote method invocation) and EJB (Enterprise Java Bean) are used for creating distributed applications. We may 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 shares the same memory. Threads are important for multi-media, Web applications, etc.?
JDK installation
Download the latest JDK version from the Oracle website https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html and install it on the system.
Java Installation:-
Set a path:-
Paste the copied path in the system environment variable
How to check whether Java is installed or not:-
Open command prompt
Why do we need to set the path in Java?
Java programs will be written in high-level language but CPU can’t understand the high-level language.
To convert high-level language into machine-understandable language we use a translator.?
Translator
Translators are classified into two types.
Compiler: It takes an entire program and converts at a shot.
Interpreter: it converts line by line of the code.
Java is a compiler and interpreter-dependent language.
JVM can’t understand the high-level language.
The compiler will convert the high-level language into byte code.
JVM converts that byte code to CPU-understandable language, then the CPU will generate the results.
Simple Program of Java
On this page, we will learn how to write a simple program in Java. We can write a simple Hello Java program easily after installing the JDK.
To create a simple Java program, you need to create a class that contains the main method. Let's understand the requirements first.
File name: Simple.java?
public class Simple?
{
public static void main(String[] args)?
{
System.out.println("Hello, Java");
}
}??
Save this file as Simple.java
领英推荐
On command prompt
To compile:
javac Simple.java
To execute:
java Simple
Output: Hello Java
A Java Development Kit (JDK) is a program development environment for writing Java applets and applications. It consists of a runtime environment that "sits on top" of the operating system layer as well as the tools and programming that developers need to compile, debug, and run applets and applications written in the Java language.
The Java Runtime Environment (JRE), also known as Java Runtime, is part of the Java Development Kit (JDK), a set of programming tools for developing Java applications. The Java Runtime Environment provides the minimum requirements for executing a Java application; it consists of the Java Virtual Machine (JVM), core classes, and supporting files.
The short form of JVM is Java Virtual Machine.
An abstract computing machine, or virtual machine, JVM is a platform-independent execution environment that converts Java byte code into machine language and executes it. Most programming languages compile source code directly into machine code that is designed to run on a specific microprocessor architecture or operating system, such as Windows or UNIX.
A Machine within a Machine
JVM -- a machine within a machine -- mimics a real Java processor, enabling Java byte code to be executed as actions or operating system calls on any processor regardless of the operating system. For example, establishing a socket connection from a workstation to a remote machine involves an operating system call. Since different operating systems handle sockets in different ways, the JVM translates the programming code so that the two machines that may be on different platforms are able to connect.
JVM (Java Virtual Machine)
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime environment in which Java byte code can be executed.?
JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).
What is JVM?
It is:
What does it do?
The JVM performs the following operation:
JVM provides definitions for the:
Internal Architecture of JVM
Let's understand the internal architecture of JVM. It contains a class loader, memory area, execution engine, etc.?
1) Class loader:
The class loader is a subsystem of JVM that is used to load class files.
2) Class (Method) Area:
Class (Method) Area stores per-class structures such as the runtime constant pool, field, and method data, and the code for methods.
3) Heap:
It is the runtime data area in which objects are allocated.
4) Stack:
Java Stack stores frames. It holds local variables and partial results and plays a part in method invocation and return.
Each thread has a private JVM stack, created at the same time as the thread.
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
5) Program Counter Register:
PC (program counter) registers. It contains the address of the Java virtual machine instruction currently being executed.
6) Native Method Stack:
It contains all the native methods used in the application.
7) Execution Engine:
It contains:
1) A virtual processor
2) Interpreter: Read byte code stream then execute the instructions.
3) Just-In-Time(JIT) compiler: It is used to improve the performance.JIT compiles parts of the byte code that have similar functionality at the same time and hence reduces the amount of time needed for compilation. Here is the term. Compiler? Refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
Internal Details of Hello Java Program
public class Simple?
{
public static void main(String[] args)?
{
System.out.println("Hello, Java");
}
}
In the previous page, we have learned about the first program, how to compile, and how to run the first Java program. Here, we are going to learn, what happens while compiling and running the Java program. Moreover, we will see some questions based on the first program.
What happens at compile time?
At compile time, a Java file is compiled by Java Compiler (It does not interact with OS) and converts the Java code into byte code.
What happens at runtime?
At runtime, the following steps are performed:
Class loader: is the subsystem of JVM that is used to load class files.
Bytecode Verifier: checks the code fragments for illegal code that can violate access rights to objects.
Interpreter: read bytecode stream then execute the instructions.
Q) Can you save a Java source file by a other name than the class name?
Yes, if the class is not public. It is explained in the figure given below:
To compile:
javac Hard.java
To execute:
java Simple
Q) Can you have multiple classes in a Java source file?
Yes, as the figure given below illustrates:
Simple Program of Java
On this page, we will learn how to write a simple program in Java. We can write a simple Hello Java program easily after installing the JDK.
To create a simple Java program, you need to create a class that contains the main method. Let's understand the requirements first.?
Requirement for Hello Java Example
For executing any Java program, you need to?
Creating a Hello Java example
Let's create the Hello Java program:
public class Simple?
{
public static void main(String[] args)?
{
System.out.println("Hello, Java");
}
}
and save this file as Simple.java
To compile:
javac Simple.java
To execute:
java Simple
Output: Hello Java
To write the simple program, open Notepad by start menu -> All Programs -> Accessories -> Notepad and write the simple program as displayed below:
As displayed in the above diagram, write the simple program of Java in Notepad and save it as Simple.java. To compile and run this program, you need to open the command prompt by starting the menu -> All Programs -> Accessories -> command prompt.
To compile and run the above program, go to your current directory first; my current directory is c:\new. Write here:
To compile:
javac Simple.java
To execute:
java Simple
Resolving an error "javac is not recognized as an internal or external command"?
If there is a problem like displayed in the below figure, you need to set a path. Since DOS doesn't know Javac or Java, we need to set a path. The path is not required in such a case if you save your program inside the JDK/bin folder. But it's a good approach to set a path.