Java_UNIT 1
Java_UNIT 1
Java
UNIT -1
Java Platform and
Programming Elements
Name of Description
Language
Basic Beginner’s All purpose Symbolic instruction Code. It was
designed to be learned and used easily by beginners.
C Developed at Bell Laboratories. C Combines the power of an
assembly language with the ease of use and portability of a
high level language. Popular
C++ C++ is an object oriented language, based on C.
C# It is a hybrid of Java and C++ & was developed by Microsoft.
High-Level
COBOL Common business oriented language , it’s use for business Languages
application.
Fortran Formula Translation. It’s popular for
Mathematical
Scientificand application.
Java Full Form “Just Another Virtual Accelerator”, Developed by
Sun Microsystems, now it is part of Oracle. It is widely used
for developing platform independent Internet Application.
❖ The Creation of Java
• James Gosling, Mike Sheridan,
and Patrick Naughton initiated the Java language
project in June 1991. The small team of sun engineers
called Green Team.
• Firstly, it was called "Greentalk" 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
• Oak is a symbol of strength and chosen as a national
tree of many countries like the U.S.A., France,
Germany, Romania, etc.
The Creation of Java
• Java was developed by James
Gosling, who is known as the father
of Java, in 1995. James
Gosling and his team members
started the project in the early
'90s.
Why Use Java?
• Java works on different platforms (Windows, Mac, Linux,
Raspberry Pi, etc.)
• It is one of the most popular programming language in the world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support
• Java is an object-oriented language which gives a clear structure
to programs and allows code to be reused, lowering development
costs
Java Program
Structure
with simple
I/O
Operation
Java Program Structure with
simple I/O Operation
Package Declaration
For example:
• package java //where java is the
package name.
Java Program Structure
with simple I/O
Operation
Import Statements
For example:
• import java.util.Scanner; //it imports the Scanner
class only
Java Program Structure
with simple I/O
Operation
Interface Statements
• An interface is a completely
"abstract class" that is used to group
related methods with empty bodies:
Java Program Structure
with simple I/O
Operation
Class Definition
For example:
• class Student //class definition
{
}
Java Program Structure
with simple I/O
Operation
Class Variables and Constants
• We can also decide and define the scope of
variables by using the modifiers. It defines the
life of the variables.
For example:
• class Student //class definition
{
String sname; //variable
int id;
double percentage;
}
Java Program Structure with simple I/O
Operation
For example:
• public class Student //class definition
•{
• public static void main(String args[])
•{
• //statements
•}
•}
Java main() method:
Java Program Structure • The main() is the starting point for JVM to start
with simple I/O execution of a Java program. Without the main()
Operation method, JVM will not execute the program. The syntax
of the main() method is:
Methods and behavior
Create a Method
• Example:
❖ Java Program Structure with simple I/O Operation
Print() Method
The only difference is that it does not insert a new line at the end of the
output: public class Main {
public static void main(String[] args)
{
System.out.print("Hello World!
"); System.out.print("I will
print");
}
}
Output: Hello World! I will print
Java Program Structure with simple
I/O Operation
Stream:
• A stream is a sequence of data. In Java, a stream is composed of bytes.
It's called a stream because it is like a stream of water that continues
to flow.
• In Java, 3 streams are created for us automatically. All these streams
are attached with the console.
Output Stream
• Java application uses an output stream to write data to a
destination; it may be a file, an array, peripheral device
or socket.
Input Stream
• Java application uses an input stream to read data from a
source; it may be a file, an array, peripheral device or
socket.
• The working of Java Output Stream and Input Stream by the figure given
below.
class Simple
{
public static void main(String args[])
Java Program
{
Structure with System.out.println("Hello Java");
simple I/O }
Operation }
To javac Simple.java
compile:
To java Simple
execute:
❖ Java’s Magic: The Byte Code
❖ Java’s Magic: The Byte Code
Advantage of Java Bytecode:
1. It helps in achieving platform-independence.
2. The set of instructions for a JVM may differ from system to system but
can all interpret Bytecode.
3. Bytecodes are non-runnable codes that rely on the availability of
an interpreter, this is where JVM comes into play.
4. It is a machine-level language code that runs on the JVM.
5. It adds portability to Java which resonates with the saying, “write once,
read anywhere”.
❖ Java’s Magic: The Byte Code
3. Secure:
Java supports the distributed
environment of the internet; it also
provides multiple security features.
Security problems like tampering,
impersonation, and virus threats can be
eliminated or reduced by using Java on
Internet.
Features
of Java
4. Platform
Independent:
Features of
Java
5. Robust:
• The English meaning of Robust is strong. Java is robust
because:
• It uses strong memory management.
• There is a lack of pointers that avoids security problems.
• Java provides automatic garbage collection 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.
Features of Java
6. Portable:
9. Interpreted:
• During compilation, Java compiler converts the
source code of the program into bytecode. This
byte code can be executed on any system
machine with the help of Java interpreter in
JVM.
• If we take any other programming language,
only a compiler or an interpreter is used to run
programs. But in Java, we use both compiler
and interpreter for the execution of the
program.
10. 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 Features of Java
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.
• The speed of interpreter inside JVM to execute a
program is slow. To overcome this problem, JavaSoft
team has introduced JIT (Just In Time) compiler
which improves the performance of interpreting byte
code by caching interpretations.
• Due to which the speed of execution of java
program is enhanced. So, both interpreter and JIT
compiler in JVM work together to run the program.
11. Multi-
threaded:
Java supports JVM utilizes
A thread is like a
separate program,
Features of Java
multi-threading multiple executing
programming threads to concurrently.
that allows to execute We can write Java
programs that deal
write programs different with many tasks at
to do several blocks of once by defining
works code. multiple threads. The
simultaneously. Creating main advantage of
multiple multi-threading is that
A thread is an it doesn't occupy
threads is
individual memory for each
called thread. It shares a
process to
Features of Java
12. 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
JVM
and
JRE
❖ JVM and JRE
✔ JVM:
• JVMs are available for many hardware and software platforms. JVM, JRE, and
JDK are platform dependent because the configuration of each OS is different
from each other. However, Java is platform independent.
• There are three notions of the JVM: specification, implementation, and instance.
Features of JVM :
• JVM can also execute a Java program line by line. It is thus also known as an
interpreter.
• JVM is also independent of the OS and hardware. It means that once a user
writes a Java program, they can easily run it anywhere.
JRE:
• JRE is an acronym for Java Runtime
Environment. It is also written as Java RTE.
The Java Runtime Environment is a
set of software tools which are used for
JVM and used to
developing Java applications. It is
provide the runtime
environment. It is the implementation of
JRE JVM. It physically exists. It contains a set of
libraries + other files that JVM uses at
runtime.
• The implementation of JVM is also actively
released by other companies
besides Sun Micro Systems.
JVM
and
JRE
JVM and JRE
Features of JRE :
and
Development Kit (JDK) is a
software development
environment which is used to
JRE develop Java applications and
applets. It physically exists. It
contains JRE + development tools.
JVM and JRE
JDK:
Definition The JDK (Java The Java Runtime The Java Virtual
Development Kit) is a Environment (JRE) is an Machine (JVM) is a
software development kit implementation of JVM. It is platform-independent
that develops applications a type of software package abstract machine that has
in Java. Along with JRE, that provides class libraries three notions in the form of
the JDK also consists of of Java, JVM, and various specifications. This
various development other components for document describes the
tools (Java Debugger, running the applications requirement of JVM
JavaDoc, compilers, etc.) written in Java implementation.
programming.
Difference Between JDK, JRE, and JVM
Parameter JDK JRE JVM
Functionality The JDK primarily JRE has a major JVM specifies all of the
assists in executing responsibility for creating implementations. It is
codes. It primarily an environment for the responsible for
functions in execution of code. providing all of these
development. implementations to the
JRE.
Platform The JDK is JRE, just like JDK, is also The JVM is
Dependency platform-dependent. It platform-dependent. It platform-independent. It
means that for every means that for every means that you won’t
different platform, you different platform, you require a different JVM
require a different require a different JRE. for every different
JDK. platform.
Difference Between JDK, JRE, and JVM
• The first character set used in the computer system was US-ASCII
(American Standard Code for Information
Interchange (ASCII pronounced as ass-kee)). It is limited to
represent only American English.
Escape Sequence
Escape Description
Characters
Escape
\t It is used to insert a tab in
Sequence the text at this point.
\r It is used to insert a
carriage return in the text at this
point.
Escape Description
Characters
Escape \\ It is used to insert a backslash character in
Sequence the text at this point.
In such a case, the compiler needs to be told that quotation marks do not signal
the start or end of a string, but instead are to be printed. The following
statement prints statements with quotation marks.
List of Unicode
U+000D \u000D Carriage
Character or Return /
Escape Enter
Sequence
U+00A0 \u00A0 Non-Breakin
g Space
Escape • List of Unicode Character or Escape Sequence
Sequence
CHAR UNICODE ESCAPE SEQUENCE DESCRIPTION
Symbols Codes
s U+0026 \u0026 Ampersand
• U+2022 \u2022 Bullet
? U+25E6 \u25E6 White Bullet
∙ U+2219 \u2219 Bullet Operator
‣ U+2023 \u2023 Triangular Bullet
- U+2043 \u2043 Hyphen Bullet
° U+00B0 \u00B0 Degree
∞ U+221E \u221E Infinity
Escape Sequence
Unicode Character Sequence Example
• Java keywords are also known as reserved words. Keywords are particular
words that act as a key to a code. These are predefined words by Java
so they cannot be used as a variable or object name or class name.
List of Reserved Keywords
8) Miscellaneous keywords:
❖ Keywords • return
• const (*)
• instanceof
• void
• assert
• default
• enum
Data
Types
1. Primitive data types (also called intrinsic or built-in types)
Types methods.
There are eight primitive data types in Java:
int x; // valid
x = 10; // valid because "x" store only one value at a time because it is
the primitive type variable.
For example, we can declare a variable of type byte by using byte keyword
as:
• The byte data type is used to save memory in large arrays where the
memory savings is most required. It saves space because a byte is
4 times smaller than an integer. It can also be used in place of "int"
data type.
❖ Data Types
Example:
Output: 110
❖ Data Types
Short Data type Example:
package
datatypePrograms; class
ShortExample
{
public static void main(String[] args)
{
short num = 350; // Here, we have stored a value 350 into a variable
num of short data type.
System.out.println(num);
}
}
Output: 350
❖ Data Types
int Data type
class IntegerExample
{
public static void main(String[] args)
{
int a = 100; // Here, we have stored 100 into a which is declared as int
type. int b = 200; // 200 is stored into b which is declared as int type.
System.out.println(a+b);
}
}
Output: 300
Long Data Type
1. We mostly use this data type for a huge number where int
type is not large enough to store the desired value.
2. A long data type is a 64-bit signed two’s complement integer.
3. Default memory size allocated to this data type is 64 bits, i.e.
8 bytes.
Example:
long num = -2334456L;
Here, we have stored -2334456 into a variable num of type
long. L represents JVM will consider it as a long value and will
allot 8 bytes to it.
❖ Data Types
long Data type
Output:
In 1000 days,distance traveled by light: 16094764800000 miles
❖ Data Float Data Type
Example:
boolean b = false;
Character Data type
Example:
char ch = ‘D’;
❖ Data Types
Character Data type
ch3 = 'A';
ch3++;
System.out.println(ch1)
;
System.out.println(ch2)
;
System.out.println(ch3)
;
}
}
In the above program, we have assigned a value 88 (which is an ASCII value) in
Output:
aXvariable
R B ch1, and specifies a letter X. ch3 is assigned value A and then it is
incremented. So, ch3 will now store B, the next character in the ASCII sequence.
Data Types
Exercise:
• Num = 10;
• FloatNum = 9.88;
• Letter = ‘B’;
• Bool = false;
• Text = “Hello”;