Java Tutorials Basics From Guru99
Java Tutorials Basics From Guru99
This video introduces the Java platform, and explains why Java is a platform as well as a
programming language.
What is PC?
A computer is an electronic device capable of performing computations, and we all know that
it is composed of a monitor, keyboard, mouse, and memory to store information. But the
most important component of the computer is a PROCESSOR. Which does all thinking of
computer, but the question is how the computer does this thinking? How does it understand
text, images, videos, etc.?
Eight bits of such signals are group together to interpret Text, numerical and symbols.
For example, the # symbol is identified by computer as 10101010. Similarly, the pattern for
adding a function is represented by 10000011.
This is known as 8-bit computing. Current day processor is capable of decoding 64-bit time.
But what is the relation of this concept with the programming language JAVA? Let
understand these as an example.
Suppose if you want to tell the computer to add two number (1+2) which is represented by
some binary numbers (10000011), how are you going to tell the computer? Yes, we going to
use assembly language to get our code executed.
We are going to give the command to a computer in this format as shown below. Your code
to add two numbers in this language would be in this order.
But how are we going to do this? Back in 1950's when computers were huge and consumed a
great deal of power, you would convert your assembly code into corresponding machine code
to 1 and 0's using mapping sheets. Later this code will be punched into the machine cards and
feed to the computer. The computer will read these code and execute the program. These
would be a long process then until ASSEMBLER came to help.
But alone assembler is not involved in this whole process; it also requires the compiler to
compile the long code into a small chunk of codes. With advancement in software
development languages, this entire assembly code could shrink into just one line print f 1+2
A with the help of software called COMPILER. It is used to convert your c language code
into assembly code, and the assembler converts it into corresponding machine code, and this
machine code will be transmitted to the processor. The most common processor used in PC or
Computers are Intel processor.
Though present-day compilers come bundled with assembler can directly convert your higher
language code into machine code.
Now, suppose Windows operating system is running on this Intel processor, a combination of
Operating System plus the processor is called the PLATFORM. The most common platform
in the world is the Windows, and Intel called the Wintel Platform. The other popular
platforms are AMD and Linux, Power PC, and Mac OS X.
Now, with a change in processor, the assembly instructions will also change. For example the
And obviously, with a change in Operating System, the level and nature of O.S level calls
will also change.
Step 1) The code to display addition of two numbers is System.out.println(1+2), and saved as
.java file.
Step 2) Using the java compiler the code is converted into an intermediate code called the
bytecode. The output is a .class file.
Step 3) This code is not understood by any platform, but only a virtual platform called the
Java Virtual Machine.
Step 4) This Virtual Machine resides in the RAM of your operating system. When the Virtual
Machine is fed with this bytecode, it identifies the platform it is working on and converts the
bytecode into the native machine code.
In fact, while working on your PC or browsing the web whenever you see either of these
icons be assured the java virtual machine is loaded into your RAM. But what makes java
lucrative is that code once compiled can run not only on all PC platforms but also mobiles or
other electronic gadgets supporting java.
Hence,
Bytecode is understandable to any JVM installed on any OS. In short, the java source code
can run on all operating systems.
In other programming languages, the compiler produces machine code for a particular
system. However, Java compiler produces code for a Virtual Machine known as Java Virtual
Machine.
First, Java code is complied into bytecode. This bytecode gets interpreted on different
machines
Between host system and Java source, Bytecode is an intermediary language.
JVM is responsible for allocating memory space.
In this tutorial, you will learn-
What is JVM?
JVM Architecture
Software Code Compilation & Execution process
C code Compilation and Execution process
Java code Compilation and Execution process
Why is Java both interpreted and Compiled Language?
Why is Java slow?
JVM Architecture
Let's understand the Architecture of JVM. It contains classloader, memory area, execution engine
etc.
1) ClassLoader
The class loader is a subsystem used for loading class files. It performs three major functions
viz. Loading, Linking, and Initialization.
2) Method Area
JVM Method Area stores class structures like metadata, the constant runtime pool, and the
code for methods.
3) Heap
All the Objects, their related instance variables, and arrays are stored in the heap. This
memory is common and shared across multiple threads.
Java language Stacks store local variables, and it’s partial results. Each thread has its own
JVM stack, created simultaneously as the thread is created. A new frame is created whenever
a method is invoked, and it is deleted when method invocation process is complete.
5) PC Registers
PC register store the address of the Java virtual machine instruction which is currently
executing. In Java, each thread has its separate PC register.
Native method stacks hold the instruction of native code depends on the native library. It is
written in another language instead of Java.
7) Execution Engine
It is a type of software used to test hardware, software, or complete systems. The test
execution engine never carries any information about the tested product.
The Native Method Interface is a programming framework. It allows Java code which is
running in a JVM to call by libraries and native applications.
Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the
Execution Engine.
1) Editor – To type your program into, a notepad could be used for this
2) Compiler – To convert your high language program into native machine code
3) Linker – To combine different program files reference in your main program together.
4) Loader – To load the files from your secondary storage device like Hard Disk, Flash
Drive, CD into RAM for execution. The loading is automatically done when you execute
your code.
5) Execution – Actual execution of the code which is handled by your OS & processor.
With this background, refer the following video & learn the working and architecture of the
Java Virtual Machine.
Suppose in the main, you have called two function f1 and f2. The main function is stored in
file a1.c.
The next step is integrating all these object files into a single .exe file with the help of linker.
The linker will club all these files together and produces the .exe file.
During program run, a loader program will load a.exe into the RAM for the execution.
The Java VM or Java Virtual Machine resides on the RAM. During execution, using the class
loader the class files are brought on the RAM. The BYTE code is verified for any security
breaches.
Next, the execution engine will convert the Bytecode into Native machine code. This is just
in time compiling. It is one of the main reason why Java is comparatively slow.
NOTE: JIT or Just-in-time compiler is the part of the Java Virtual Machine (JVM). It
interprets part of the Byte Code that has similar functionality at the same time.
A compiler is a program which converts a program from one level of language to another.
Example conversion of C++ program into machine code.
The java compiler converts high-level java code into bytecode (which is also a type of
machine code).
In Java, the Just In Time Code generator converts the bytecode into the native machine code
which are at the same programming levels.
However, the latest version of Java has addressed the performance bottlenecks to a great
extent.
Summary:
JVM or Java Virtual Machine is the engine that drives the Java Code. It converts Java
bytecode into machines language.
In JVM, Java code is compiled to bytecode. This bytecode gets interpreted on different
machines
JIT or Just-in-time compiler is the part of the Java Virtual Machine (JVM). It is used to speed
up the execution time
In comparison to other compiler machines, Java may be slow in execution.
Let's look into the steps to set the PATH and CLASSPATH
Step 7) Paste Path of bin folder in Variable value and click on OK Button.
Note: In case you already have a PATH variable created in your PC, edit the PATH variable
to
Here, %PATH% appends the existing path variable to our new value
Note: In case you java installation does not work after installation, change classpath to
CLASSPATH = <JDK installation directory>\lib\tools.jar;
file /sbin/init
Step 3) Download latest Java (a zip file e.g., jdk-8-linux-x64.tar.gz) from Oracle site-
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Step 4) In the terminal, navigate to the location of your zip file
To do so –
Enter command
. /etc/profile
Step 7) Its time to let Ubuntu know where our JDK/JRE is located.
java -version
2. A Text Editor
In this example, we'll use Notepad. it is a simple editor included with the Windows Operating
System. You can use a different text editor like NotePad++
This video will help you code your first Java program.
Step 3) Save the file as FirstProgram.java make sure to select file type as all files while
saving the file in our working folder C:\workspace
Step 4) Open the command prompt. Go to Directory C:\workspace. Compile the code using
command,
javac FirstProgram.java
Step 5) If you look in your working folder, you can see that a file named A.class has been
created.
Step 6) To execute the code, enter the command java followed by the class name, as expected
output Hello World is displayed now.
java A
Note: Java is case sensitive Programming language. All code, commands, and file names
should is used in consistent casing. FirstProgram is not same as firstprogram.
Step 7) If you copy and paste the same code in IDE like Eclipse the compiling and execution
is done with the click of a button Using IDE is convenient and improves your efficiency but
since you are learning Java, we recommend you stick to notepad.