Java is a plat form independent , object oriented language
invented by scientist "James Gosling" in sun microsystem
initiated in June 1991
java was originally designed for developing software for network consumer electronic devices
language was initially called OAK.
OAK is a tree which is located outside of James gosling office
name "java" specifically doesn't have any meaning rather it refer to the hot aromatic drink coffee
as name java is derived from coffee cup java programming language icon is coffee cup
java features
1)simple
2)secure
3)Robust
4)Portable
5. Architecture neutral
6. object oriented
7. multi-threaded
8. high performance
9. Distributed
10. Dynamic
11. Byte coded
12. Interpreted
13. Garbage collected
14. open source
platform independent : program that is compiled on windows can run on Linux and vice versa
Object Oriented : Way of organizing programs as collection of objects each of which represents as instance of a class
Platform Independent: program that is compiled on windows can run on Linux and vice-versa
Object Oriented: way of organizing programs as collection of objects, each of which represents an instance of a class
Abstraction, Encapsulation, Inheritance, Polymorphism -> 4 main concepts of OOP
Robust: puts a lot of emphasis on early checking for possible errors
main features of java that makes it robust are garbage collection, Exception Handling and memory allocation
Secure: We don’t have pointers and we cannot access out of bound arrays
several security flaws like stack corruption or buffer overflow is impossible to exploit in Java.
Distributed: java programs can be distributed on more than one systems that are connected to each other using internet connection.
Objects on one JVM (java virtual machine) can execute procedures on a remote JVM
Multithreading: allows concurrent execution of two or more parts of a program for maximum utilization of CPU.
Portable: byte code can be carried to any platform for execution that makes java code portable.
- JVM - Java Virtual Machine
- Provides Runtime Environment in which Java bytecode can be executed
- Tasks of JVM
- Loads Code
- Verifies Code
- Executes Code
- Provides Runtime Environment
- JVM is platform dependent i.e. for each platform we have different JVM Configuration
- JVM does not exists Physically. It is abstract in nature.
- The primary function of JVM is to execute the byte code produced by compiler
- Each operating system has different JVM, however the output they produce after execution of byte code is same across all operating systems. Which means that the byte code generated on Windows can be run on Mac OS and vice versa.
- The JVM doesn’t understand Java source code, that’s why we need to have javac compiler that compiles *.java files to obtain *.class files that contain the byte codes understood by the JVM
- JVM makes java portable (write once, run anywhere). Each operating system has different JVM, however the output they produce after execution of byte code is same across all operating systems.
- Java is a high level programming language. A program written in high level language cannot be run on any machine directly. First, it needs to be translated into that particular machine language
- The javac compiler does this thing, it takes java program (.java file containing source code) and translates it into machine code (referred as byte code or .class file).
JRE
- JRE- Java Runtime Environment
- JRE = JVM + set of Libraries
- JRE contains set of libraries that JVM uses at runtime
- JRE Physically exists
- JRE is Platform Dependent
- JRE is the environment within which the java virtual machine runs
- You can run the code in JRE but you can’t develop and compile the code in JRE.
- JRE contains Java virtual Machine(JVM), class libraries, and other files excluding development tools such as compiler and debugger.
- Actually, JRE is the implementation of JVM, as JVM runs the program by using class libraries and other files provided in JRE
- If you only want to run any Java program, you should have JRE installed in the system, but we don’t need JDK to run any Java program.
JDK
- JDK – Java Development Kit
- JDK= JRE+ Development Tools
- It is a fully featured Software Development Kit
- Development Tools: javac, java, javadoc etc
- JDK is a superset of JRE, it contains everything that JRE has along with development tools such as compiler, debugger etc.
- It is a kit that is the combination of developing tools to develop Java programs and JRE (Java Runtime Environment) to run the programs.
Java Installation
- Follow Instruction on this page
https://www.guru99.com/install-java.html
IDE Installation
- Intellij Installation
https://treehouse.github.io/installation-guides/windows/ intellij-idea-win.html - Eclipse Installation
https://www.guru99.com/install-eclipse-java.html
Java Terms
- Compiler (Javac)
- Byte-Code
- ClassPath
- Just In Time Compiler (JTI)
- Garbage Collector
- Java is a high level programming language. A program written in high level language cannot be run on any machine directly. First, it needs to be translated into that particular machine language. The javac compiler does this thing, it takes java program (.java file containing source code) and translates it into machine code (referred as byte code or .class file).
- Javac compiler of JDK compiles the java source code into bytecode so that it can be executed by JVM
- The classpath is the file path where the java runtime and Java compiler look for .class files to load. By default, JDK provides many libraries. If you want to include external libraries they should be added to the classpath.
- The Just-In-Time (JIT) compiler is a an essential part of the JRE, that is responsible for performance optimization of java based applications at run time. Interpreting the bytecode affects the speed of execution. The JIT compiler aids in improving the performance of Java programs by compiling bytecode into native machine code at run time. A just-in-time (JIT) compiler is a program that turns bytecode into instructions that can be sent directly to a computer's CPU. JIT compiler runs after a program starts and compiles code. A common way to say this is that a JIT compiler compiles code on the fly, or in other words, just in time. JIT compilers contrast different compiler types such as a traditional compiler, which will compile all code to a machine language before a program starts to run. bytecode must be compiled and translated to a language a CPU can properly understand. However, how that bytecode is translated into a native language may have a large impact on the speed and performance of an application. To improve performance, JIT compilers will, at runtime, compile suitable bytecode sequences into machine code. When the same block of bytecode is needed again, the previously created object code will be used. https://www.theserverside.com/
definition/just-in-time- compiler-JIT - Java applications obtain objects in memory as needed. It is the task of garbage collection (GC) in the Java virtual machine (JVM) to automatically determine what memory is no longer being used by a Java application and to recycle this memory for other uses. Because memory is automatically reclaimed in the JVM, Java application developers are not burdened with having to explicitly free memory objects that are not being used. https://www.eginnovations.com/
blog/what-is-garbage- collection-java/
JVM Architecture
Working of JVM
- Behaves as a run-time engine to run Java applications
- JVM performs the following tasks:
- Loads the code
- Verifies code
- Executes the code
- Provides a run-time environment for various applications
- JVM provides a Memory area
- Provides a Register set
- JVM provides the garbage collection heap
Subsystems of JVM
There are three main subsystems in JVM Architecture
- Class Loader
- Memory Area
- Execution Engine
JVM Classloader
Three important functions of ClassLoader are Initialization, Loading, Linking
Loading
- The Classloader reads the .class file, generates the corresponding binary data, and saves it in the method area.
- After loading the .class file, JVM creates an object of type Class to represent this file in the heap memory.
- To get this object reference we can use the getClass() method of Object class.
Three built-in classloaders in Java are:
- Bootstrap ClassLoader
This is the classloader which is the super class of Extension classloader. It loads the rt.jar file.
- Extension ClassLoader
It is the ClassLoader which loads the jar files present in the ext directory. This is the child Bootstrap classLoader and parent of System classloader.
- System/Application ClassLoader
It is the classLoader which loads the class files from the application classpath. This is the child of the Extension classloader.
Linking
- This operation combines different files in the main program together
- Performs verification, preparation, and (optionally) resolution
- Verification: The Verification phase checks the correctness of the .class file. It means that it checks that the file formation and generation is by a valid compiler or not. If the verification fails then we get a java.lang.Verify Exception.
- Preparation: JVM allocates memory for class variables and initializes the memory to default values.
- Resolution: Resolution is the process of replacing symbolic references with direct references. It uses searching into the method area to locate the referenced entity.
Initialization
- Involves the assignment of all static variables with their specific values
JVM Memory area
- 1. Method Area– It stores the structure of each class like method data, field data, runtime pool, metadata.
- 2. Heap– Heap is the runtime area where object allocation takes place.
- 3. Stacks– Stacks store the partial results and local variables of a program. Whenever a thread is created, there is a simultaneous creation of JVM stack. When we invoke a method, a new frame creates and destroys at the same time when the invocation process completes.
- 4. PC Registers – It stores the address of the currently executing JVM instruction.
- 5. Native Method Stacks – It includes all the native methods required in any application. It is not written in java.
JVM execution engine
It is the component of JVM that reads data from memory locations and executes the instructions. It has three major components namely a virtual processor, an interpreter, and a JIT compiler.
- Virtual Processor
- Interpreter: Read the bytecode stream then execute the instructions.
- Just-In-Time(JIT) compiler: It improves performance. JIT compiles parts of the byte code with similar functionality at the same time and reduces the amount of time needed for compilation.
What is JAR
- Java Archive
- Compressed format of compiled Java Project
- Contains .class files + metadata + other resources
- Used to distribute java code to be used by other projects/direct execution of java programs.
- It is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform
- In simple words, a JAR file is a file that contains a compressed version of .class files, audio files, image files, or directories
My first Java Program
// basic java program to print "hello world"
import java.io.*;
class MyFirstJavaProgramme
{
public MyFirstProgramme
{
public static void main(String[] args)
{
// prints hello world
System.out.println("Hello world");
}
}
Comments
- Comments are used for explaining code.
- Compilers ignore the comment entries and do not execute them
Single Line Comments
Syntax: // Single Line Comment
Multi Line Comment
Syntax: /* Multi line comments*/
Explanation
Package
- Mechanism to encapsulate a group of classes, sub packages and interfaces
- Packages are named in reverse order of domain names
- Packages that are inside another package are the subpackages
- Built In Packages:
- Java.lang
- Java.io
- Java.util etc
- Preventing naming conflicts
- Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
- Providing controlled access
- Packages can be considered as data encapsulation
- 1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List, Dictionary and support ; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations. - // All the classes and interfaces of this package will be accessible but not subpackages.
import package.*;
- import java.io.*: This means all the classes of io package can be imported. Java io package provides a set of input and output streams for reading and writing data to files or other input or output sources.
- class : class keyword is used to declare classes in Java. The class contains the data and methods to be used in the program.
- public : It is an access specifier. Public means this function is visible to all.
- static : static is again a keyword used to make a function static. To execute a static function you do not have to create an Object of the class. The main() method here is called by JVM, without creating any object for class.
- void : It is the return type, meaning this function will not return anything.
- main : main() method is the most important method in a Java program. This is the method which is executed, hence all the logic must be inside the main() method. If a java class is not having a main() method, it causes compilation error.
- String[] args : This represents an array whose type is String and name is args. We will discuss more about array in Java Array section.
- System.out.println : This is used to print anything on the console like printf in C language.
Compile and Run
- Save your program with .java extension
- Compile program with: javac MyFirstJavaProgram.java
- Run program with: java MyFirstJavaProgram
Comments
Post a Comment