0% found this document useful (0 votes)
211 views

Java Tutorials Basics From Guru99

The document discusses the Java platform and how it allows Java programs to run on different operating systems. It defines the Java platform as a collection of programs that help develop and run Java programs, including an execution engine, compiler, and libraries. It explains that the Java platform makes Java both a programming language and platform-independent by using a Java Virtual Machine that converts Java bytecode into native machine code. This allows Java programs to run on any device that supports the Java Virtual Machine.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
211 views

Java Tutorials Basics From Guru99

The document discusses the Java platform and how it allows Java programs to run on different operating systems. It defines the Java platform as a collection of programs that help develop and run Java programs, including an execution engine, compiler, and libraries. It explains that the Java platform makes Java both a programming language and platform-independent by using a Java Virtual Machine that converts Java bytecode into native machine code. This allows Java programs to run on any device that supports the Java Virtual Machine.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

What is Java Platform?

Ultimate Guide for


Programmers
What is Java?
Java is a programming language and a computing platform for application development. It
was first released by Sun Microsystem in 1995 and later acquired by Oracle Corporation. It is
one of the most used programming languages.

What is Java Platform?


Java platform is a collection of programs that help to develop and run programs written in the
Java programming language. Java platform includes an execution engine, a compiler, and a
set of libraries. JAVA is platform-independent language. It is not specific to any processor or
operating system.

This video introduces the Java platform, and explains why Java is a platform as well as a
programming language.

To understand JAVA programming language, we need to understand some basic concept of


how a computer program can run a command and execute the action.

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.?

What is Assembly Language?


The computer is an electronic device, and it can only understand electronic signals or binary
signals. For example, the 5-volt electronic signal may represent binary number 1 while 0
volts may represent binary number 0. So your PC is continuously bombarded with these
signals.

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.

"Assembly Language is the most elementary form of software development languages."

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.

 Store number 1 at memory location say A


 Store number 2 at memory location say B
 Add contents of Location A & B
 Store results

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.

What are Assembler and Compiler?


With the advancement in technology i/o devices were invented, you could directly type your
program into the PC using a program called ASSEMBLER. It converts it into corresponding
machine code (110001..) and feeds to your processor. So coming back to our example
addition of (1+2), the assembler will convert this code into machine code and give the output.
That apart, you will also have to make calls to create Operating System provided functions to
display the output of the code.

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

 Add instruction in Intel may be called ADDITION for AMD


 OR Math ADD for Power PC

And obviously, with a change in Operating System, the level and nature of O.S level calls
will also change.

As a developer, I want my software program to work on all platforms available, to maximize


my revenues. So I would have to buy separate compilers which convert my print f command
into the native machine code.
But compilers come expensive, and there is a chance of compatibility issues. So buying and
installing a separate compiler for different O.S and processor is not feasible. So, what can be
an alternative solution? Enter Java language.

How Java Virtual Machine works?


By using Java Virtual Machine, this problem can be solved. But how it works on different
processors and O.S. Let's understand this process step by step.

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,

"Java is a programming language as well as a Platform"

How is Java Platform Independent?


Like C compiler, Java compiler does not produce native executable code for a particular
machine. Instead, Java produces a unique format called bytecode. It executes according to the
rules laid out in the virtual machine specification.

Bytecode is understandable to any JVM installed on any OS. In short, the java source code
can run on all operating systems.

Java Virtual Machine (JVM) & its


Architecture
What is JVM?
JVM is a engine that provides runtime environment to drive the Java Code or applications. It
converts Java bytecode into machines language. JVM is a part of JRE(Java Run
Environment). It stands for Java Virtual Machine

 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.

4) JVM language Stacks

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.

6) Native Method Stacks

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.

8) Native Method interface

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.

9) Native Method Libraries

Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the
Execution Engine.

Software Code Compilation & Execution process


In order to write and execute a software program, you need the following

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.

Click here if the video is not accessible

C code Compilation and Execution process


To understand the Java compiling process in Java. Let's first take a quick look to compiling
and linking process in C.

Suppose in the main, you have called two function f1 and f2. The main function is stored in
file a1.c.

Function f1 is stored in a file a2.c

Function f2 is stored in a file a3.c


All these files, i.e., a1.c, a2.c, and a3.c, is fed to the compiler. Whose output is the
corresponding object files which are the machine code.

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.

Java code Compilation and Execution in Java VM


Let's look at the process for JAVA. In your main, you have two methods f1 and f2.

 The main method is stored in file a1.java


 f1 is stored in a file as a2.java
 f2 is stored in a file as a3.java
The compiler will compile the three files and produces 3 corresponding .class file which
consists of BYTE code. Unlike C, no linking is done.

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.

Why is Java both Interpreted and Compiled Language?


Programming languages are classified as

 Higher Level Language Ex. C++, Java


 Middle-Level Languages Ex. C
 Low-Level Language Ex Assembly
 finally the lowest level as the Machine Language.

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).

An interpreter is a program which converts a program at one level to another programming


language at the same level. Example conversion of Java program into C++

In Java, the Just In Time Code generator converts the bytecode into the native machine code
which are at the same programming levels.

Hence, Java is both compiled as well as interpreted language.

Why is Java slow?


The two main reasons behind the slowness of Java are
1. Dynamic Linking: Unlike C, linking is done at run-time, every time the program is run in Java.
2. Run-time Interpreter: The conversion of byte code into native machine code is done at run-
time in Java which furthers slows down the speed

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.

How to Download & Install Java JDK 8 in


Windows
This Java Development Kit(JDK) allows you to code and run Java programs. It's possible that
you install multiple JDK versions on the same PC. But Its recommended that you install only
latest version.

Following are steps to install Java in Windows

Step 1) Go to link. Click on Download JDK. For java latest version.


Step 2) Next,

1. Accept License Agreement


2. Download latest Java JDK for your version(32 or 64 bit) of java for Windows.
Step 3) Once the download is complete, run the exe for install JDK. Click Next
Step 4) Once installation is complete click Close

How to set Environment Variables in Java: Path and


Classpath
The PATH variable gives the location of executables like javac, java etc. It is possible to run
a program without specifying the PATH but you will need to give full path of executable like
C:\Program Files\Java\jdk1.8.0_131\bin\javac A.java instead of simple javac A.java

The CLASSPATH variable gives location of the Library Files.

Let's look into the steps to set the PATH and CLASSPATH

Step 1) Right Click on the My Computer and Select the properties


Step 2) Click on advanced system settings

Step 3) Click on Environment Variables


Step 4) Click on new Button of User variables

Step 5) Type PATH in the Variable name.


Step 6) Copy the path of bin folder which is installed in JDK folder.

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

PATH = <JDK installation directory>\bin;%PATH%;

Here, %PATH% appends the existing path variable to our new value

Step 8) You can follow a similar process to set CLASSPATH.

Note: In case you java installation does not work after installation, change classpath to
CLASSPATH = <JDK installation directory>\lib\tools.jar;

Step 9) Click on OK button

Step 10) Go to command prompt and type javac commands.

If you see a screen like below, Java is installed.


Having trouble installing Java? Check our Online Java Compiler

How to Download & Install Java in


Linux(Ubuntu)
Following is a step by step guide to install Java on Linux. In this training, we will install Java
on Ubuntu. The process and install commands remain the same, for different flavors of
Linux, or versions of Java.

How to Install Oracle Java on Ubuntu Linux


Step 1) Before you start with the setting up Java, you will need to remove the OpenJDK/JRE
from the system.

To do this, use below command-

sudo apt-get purge openjdk-\*

You will be asked for a confirmation. Enter Y


Wait for sometime for uninstall to complete

Step 2) Check whether Ubuntu OS architecture is 32-bit or 64-bit.

You can do it using the following command-

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

Accept the License Agreement.

 Select x86 for 32-bit system


 Select x64 for 64-bit system
 Select the tar.gz for Ubuntu
 Select rpm for Debian based system
Once a download is complete you will see

Step 4) In the terminal, navigate to the location of your zip file

Extract contents of java zip file in a directory of your choice.

In this tutorial, we will extract in the Downloads folder itself –

sudo tar -xvf jdk-8u5-linux-i586.tar.gz


Enter your password, if asked. The unzip will continue. Once done you will see the following
folder -

Step 5) Add the following system variables to the end of /etc/profile file

JAVA_HOME=<Directory where JAVA has been extracted>/jdk1.8.0


PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

To do so –

Enter command

"sudo nano /etc/profile"

Enter the system variables


Press Control+C to enter into command mode
Press ^+X to Exit

Press "Y" to save the changes

Step 6) Now reload environment using a command,

. /etc/profile

Step 7) Its time to let Ubuntu know where our JDK/JRE is located.

Copy-paste below lines into a command prompt.

sudo update-alternatives --install "/usr/bin/java" "java" "<Directory where


JAVA has been extracted>/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "<Directory
where JAVA has been extracted>/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "<Directory
where JAVA has been extracted>/bin/javaws" 1
Step 8) Tell Ubuntu that our installation i.e., jdk1.8.0_05 must be the default Java.

Use below commands to do this-

sudo update-alternatives --set java <Directory where JAVA has been


extracted>/bin/java
sudo update-alternatives --set javac <Directory where JAVA has been
extracted>/bin/javac
sudo update-alternatives --set javaws <Directory where JAVA has been
extracted>/bin/javaws

Step 9) Test the installation using below command,

java -version

A successful installation will show information as seen in the above screenshot.

Hello World: How to Create Your First


Java Program
You need the following 2 software to create your first Java Program

1. The Java SE Development Kit

Please refer our last tutorial to download and install JDK

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.

Click here if the video is not accessible


Steps to Compile and Run first Java program
Step 1) Open Notepad from Start menu by selecting Programs > Accessories > Notepad.

Step 2) Create a Source Code for your Program

 Declare a class with name A.


 Declare the main method public static void main(String args[]){
 Now Type the System.out.println("Hello World"); which displays the text Hello World.
class A {
public static void main(String args[]){
System.out.println("Hello World");
}
}

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.
 

You might also like