0% found this document useful (0 votes)
3 views19 pages

1 22CA026_Advance Java Programming_Java Basics

Java is a powerful, object-oriented programming language used for developing standalone and web applications. It features simplicity, security, robustness, portability, and high performance, making it suitable for a variety of applications across different platforms. The Java Runtime Environment (JRE) and Java Development Kit (JDK) facilitate the execution and development of Java programs, while the Just In Time (JIT) compiler enhances performance by converting bytecode to native machine code.

Uploaded by

Tanya Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
3 views19 pages

1 22CA026_Advance Java Programming_Java Basics

Java is a powerful, object-oriented programming language used for developing standalone and web applications. It features simplicity, security, robustness, portability, and high performance, making it suitable for a variety of applications across different platforms. The Java Runtime Environment (JRE) and Java Development Kit (JDK) facilitate the execution and development of Java programs, while the Just In Time (JIT) compiler enhances performance by converting bytecode to native machine code.

Uploaded by

Tanya Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 19

Java : As a Language

• Java is a simple yet powerful object oriented language that is


used for developing robust mission critical applications

• Java is used for developing two types of applications:


• Standalone applications for servers , desktops
and mobile devices

• Web applications called applets


Features of Java
• Simple
• Object-Oriented
• Platform independent & Architecture neutral
• Secured
• Robust
• Portable
• Dynamic
• Interpreted
• High Performance
• Multithreaded
• Distributed
Features of Java

• Simple-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.

• Object-Oriented- It is truly object oriented. Everything in java is a class. It supports all the
features of object oriented languages which include: Encapsulation, Data hiding, Data abstraction,
Polymorphism Inheritance. Being an object oriented language, Java is data centric language as
compared to procedural languages which are function/procedure centric.

• Platform independent & Architecture neutral - Java is platform independent and architecture
neutral language. The size of primitives is not dependent on platform or architecture on which
code is written for example size of int in C is dependent on compiler and architecture used by the
machine on which code is written but in java size of int will remain 32 bit whatever be the platform
or architecture of the machine.
• 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 Java Runtime Environment.
How does JRE work?

The JDK and JRE interact with one another to create a sustainable runtime environment that enables
the seamless execution of Java-based applications in virtually any operating system. The following
make up the JRE architecture:

• ClassLoader: The Java ClassLoader dynamically loads all classes necessary to run a Java program.
Since Java classes are only loaded into memory when they're required, the JRE uses ClassLoaders to
automate this process on demand.

• Bytecode verifier: The bytecode verifier ensures the format and accuracy of Java code before it
passes to the interpreter. In the event that code violates system integrity or access rights, the class
will be considered corrupted and won't be loaded.

• Security Manager: It determines what resources a class can access such as reading and writing to
the local disk

• Interpreter: After the bytecode successfully loads, the Java interpreter creates an instance of the
JVM that allows the Java program to be executed natively on the underlying machine.
• 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.

• Portable-The code after compilation of java source code can run on any architecture and any
platform with the condition that JVM exits there.

• 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++.

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

• Multithreaded- 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.
Java Language Specifications: API ,JDK
and IDEs
 API
stands for Application Program Interface. It contains
predefined classes and interfaces for developing Java Programs

 JavaSE,EE and ME- Java is full fledged and powerful language that
can be used in many ways. It comes in three editions:

 JavaSE (Standard Edition)- Used to design standalone client-side


applications and applets

 JavaEE(Enterprise Edition)- Used for developing server side


applications such as Java Servlets and Java Server pages

 Java
ME( Micro Edition)- Used for developing applications for
mobile devices such as cell phones
 JDK- There are many versions of Java Standard
Edition (SE). Sun releases each version with a
Java development Toolkit (JDK).
 JDK consists of a set of programs for developing
and testing Java programs each of which is
invoked from a command line.
 JDK tools
◦ javac---java Compiler
◦ java---java interpreter
◦ javadoc---java documentation
◦ jdb----java debugger
 Besides JDK, Java development tools can also be
used ( like NetBeans, Eclipse, Jbuilder,
intelliJIDEA and TextPad) that provide an
integrated development Environment (IDEs) for
rapidly developing Java programs.
Creating , Compiling and Executing a Java
Program
A java program can be written using any
of the editors like notepad. The extension
of a java program is .java
Once the source code is created, it is
compiled using java compiler (javac)
The compiled java code is called the
Bytecode and is platform independent
and architecture neutral code. The
extension of Bytecode file is .class
Bytecode is interpreted by JVM to create
java executable file (no extension)
Create/Modify Source
Code

Source Code
e.g abc.java

Compile Source Code


e.g javac abc.java
If error

Byte Code

JVM interpreting the Bytecode


Run ByteCode
e.g java abc

If runtime errors
JIT (Just In Time Compiler)
Bytecode is one of the most important features of
java that aids in cross-platform execution. Way of
converting bytecode to native machine language
for execution has a huge impact on the speed of
it. These Bytecode have to be interpreted or
compiled to proper machine instructions
depending on the instruction set architecture.
Moreover these can be directly executed if the
instruction architecture is bytecode based.
Interpreting the bytecode affects the speed of
execution.
In order to improve performance, JIT compilers
interact with the Java Virtual Machine (JVM) at run
time and compile suitable bytecode sequences
into native machine code.
While using a JIT compiler, the hardware is able to
JIT compiler
The JIT compiler is able to perform certain simple
optimizations while compiling a series of bytecode
to native machine language. Some of these
optimizations performed by JIT compilers are
data-analysis, reduction of memory
accesses by register allocation, translation
from stack operations to register
operations, elimination of common sub-
expressions etc.
A simple Java Program
package package_name //optional
import java.io.* //optional
class Example
{
/* the variables or methods declared outside main and inside the
class are called member variables and member methods */
public static void main(String ar[])
{
System.out.println(“hello”);
}
}
The program will be saved as Example.java
(the name of the saved file should be exactly similar to the
class name containing main())
 The source code compiled using the command:
 javac Example.java (given at coomand
prompt)
 Once the file is successfully compiled, a .class
file with the same name(Example.class) will be
created on the system
 To run this file, interpreter is called that
interprets the Bytecode (.class file) created after
compilation. The command for this is:
 java Example
public static void main(String ar[]){}

public: this is an access specifier that indicates that main()


function is accessible outside the class. Anything that is
declared public is accessible outside the class

static: this modifier specifies that main() function can be


called without instantiating the class. This is essential as
the main() is called by interpreter before any objects are
made

void: this specifies that main() does not return a value

main() function takes as argument an array of String class


objects . This array takes values from the command line
called command line
arguments
System.out.println(“hello”);

Classname Method of PrintStream class


used for displaying on console

PrintStream class
object
 Program to swap the values of two variables
class Swap{
public static void main(String ar[])
{
int a, b,temp;
a=12; b=13;
temp=a; a=b; b=temp;
System.out.println(“a= “+a+”b=“+b);
}
}
Concatenation operator
Save this file as Swap.java that combines a string
with a simple variable

You might also like