Java Unit 1
Java Unit 1
The history of Java is very interesting. Java was originally designed for interactive television,
but it was too advanced technology for the digital cable television industry at the time. The
history of Java starts with the Green Team. Java team members (also known as Green Team),
initiated this project to develop a language for digital devices such as set-top boxes,
televisions, etc. However, it was best suited for internet programming. Later, Java technology
was incorporated by Netscape.
The principles for creating Java programming were "Simple, Robust, Portable, Platform-
independent, Secured, High Performance, Multithreaded, Architecture Neutral, Object-
Oriented, Interpreted, and Dynamic". 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.
Currently, Java is used in internet programming, mobile devices, games, e-business solutions,
etc. Following are given significant points that describe the history of Java.
1) James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project
in June 1991. The small team of sun engineers called Green Team.
2) Initially it was designed for small, embedded systems in electronic appliances like set-top
boxes.
3) Firstly, it was called "Greentalk" by James Gosling, and the file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
5) Why Oak? Oak is a symbol of strength and chosen as a national tree of many countries
like the U.S.A., France, Germany, Romania, etc.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.
According to James Gosling, "Java was one of the top choices along with Silk". Since Java
was so unique, most of the team members preferred Java than other names.
8) Java is an island in Indonesia where the first coffee was produced (called Java coffee). It is
a kind of espresso bean. Java name was chosen by James Gosling while having a cup of
coffee nearby his office.
10) Initially developed by James Gosling at Sun Microsystems (which is now a subsidiary of
Oracle Corporation) and released in 1995.
11) In 1995, Time magazine called Java one of the Ten Best Products of 1995.
12) JDK 1.0 was released on January 23, 1996. After the first release of Java, there have been
many additional features added to the language. Now Java is being used in Windows
applications, Web applications, enterprise applications, mobile applications, cards, etc. Each
new version adds new features in Java.
Java Version History
Many java versions have been released till now. The current stable release of Java is Java SE
10.
A list of the most important features of the Java language is given below.
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic
Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to
Sun Microsystem, Java language is a simple programming language because:
Java syntax is based on C++ (so easier for programmers to learn it after C++).
Java has removed many complicated and rarely
rarely-used
used features, for example, explicit
pointers, operator overloading, etc.
There is no need to remove unreferenced objects because there is an Automatic
Garbage Collection in Java.
Object-oriented
1. Object
2. Class
3. Inheritance-
4. Polymorphism-
5. Abstraction-
6. Encapsulation-
Platform Independent
Java is platform independent because it is different from other languages like C, C++, etc.
which are compiled into platform specific machines while Java is a write once, run anywhere
language. A platform is the hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides a
software-based platform.
The Java platform differs from most other platforms in the sense that it is a software-based
platform that runs on top of other hardware-based platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris,
Mac/OS, etc. Java code is compiled by the compiler and converted into bytecode. This
bytecode is a platform-independent code because it can be run on multiple platforms, i.e.,
Write Once and Run Anywhere (WORA).
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 virtual machine sandbox
Java language provides these securities by default. Some security can also be provided by an
application developer explicitly through SSL, JAAS, Cryptography, etc.
Robust
Architecture-neutral
Java is architecture neutral because there are no implementation dependent features, for
example, the size of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4
bytes of memory for 64-bit architecture. However, it occupies 4 bytes of memory for both 32
and 64-bit architectures in Java.
Portable
Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't
require any implementation.
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.
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.
Multi-threaded
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.
Dynamic
Java is a dynamic language. It supports the dynamic loading of classes. It means classes are
loaded on demand. It also supports functions from its native languages, i.e., C and C++.
Comparison between C++ and Java
Comparison C++ Java
Index
Platform- C++ is platform-dependent. Java is platform-independent.
independent
Mainly used for C++ is mainly used for system Java is mainly used for application programming. It
programming. is widely used in Windows-based, web-based,
enterprise, and mobile applications.
Design Goal C++ was designed for systems Java was designed and created as an interpreter for
and applications programming. It printing systems but later extended as a support
was an extension of the C network computing. It was designed to be easy to
programming language. use and accessible to a broader audience.
Goto C++ supports the goto statement. Java doesn't support the goto statement.
Multiple C++ supports multiple Java doesn't support multiple inheritance through
inheritance inheritance. class. It can be achieved by using interfaces in java.
Operator C++ supports operator Java doesn't support operator overloading.
Overloading overloading.
Pointers C++ supports pointers. You can We can't write the pointer program in java.
write a pointer program in C++.
Compiler and C++ uses compiler only. C++ is Java uses both compiler and interpreter. Java source
Interpreter compiled and run using the code is converted into bytecode at compilation time.
compiler which converts source The interpreter executes this bytecode at runtime and
code into machine code so, C++ produces output. Java is interpreted that is why it is
is platform dependent. platform-independent.
Structure and C++ supports structures and Java doesn't support structures and unions.
Union unions.
Thread Support C++ doesn't have built-in support Java has built-in thread support.
for threads. It relies on third-
party libraries for thread support.
Documentation C++ doesn't support Java supports documentation comment (/** ... */) to
comment documentation comments. create documentation for java source code.
Virtual C++ supports virtual keyword so Java has no virtual keyword. We can override all
Keyword that we can decide whether or not non-static methods by default. In other words, non-
to override a function. static methods are virtual by default.
unsigned right C++ doesn't support >>> Java supports unsigned right shift >>> operator that
shift >>> operator. fills zero at the top for the negative numbers. For
positive numbers, it works same like >> operator.
Inheritance C++ always creates a new Java always uses a single inheritance tree because all
Tree inheritance tree. classes are the child of the Object class in Java. The
Object class is the root of the inheritance tree in
java.
Hardware C++ is nearer to hardware. Java is not so interactive with hardware.
Object-oriented C++ is an object-oriented Java is also an object-oriented language. However,
language. However, in the C everything (except fundamental types) is an object in
language, a single root hierarchy Java. It is a single root hierarchy as everything gets
is not possible. derived from java.lang.Object.
JVM
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it
doesn't physically exist. It is a specification that provides a runtime environment in which
Java bytecode can be executed. It can also run those programs which ar aree written in other
languages and compiled to Java bytecode.
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..
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java
Runtime Environment
ronment is a set of software tools which are used for developing Java
applications. It is used to provide the runtime environment. It is the implementation of 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.
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software
development environment which is used to develop Java applica
applications and applets.
applets It
physically exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released by Oracle
Corporation:
The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator
(Javadoc),
avadoc), etc. to complete the development of a Java Application.
1. Keywords
2. Identifiers
3. Constants
4. Special Symbols
5. Operators
3. Literals in Java
In Java, literals are the constant values that appear directly in the program. It can be
assigned directly to a variable. Java has various types of literals. The following figure
represents a literal.
1. Integer Literal
2. Character Literal
3. Boolean Literal
4. String Literal
Integer Literals
Integer literals are sequences of digits. There are three types of integer literals:
Decimal Integer: These are the set of numbers that consist of digits from 0 to 9. It
may have a positive (+) or negative (-) Note that between numbers commas and non-
digit characters are not permitted. For example, 5678, +657, -89, etc.
Binary Integer: Base 2, whose digits consists of the numbers 0 and 1 (you can create
binary literals in Java SE 7 and later). Prefix 0b represents the Binary system. For
example, 0b11010.
Real Literals
The numbers that contain fractional parts are known as real literals. We can also represent
real literals in exponent form. For example, 879.90, 99E-3, etc.
Character Literals
String Literals
String literal is a sequence of characters that is enclosed between double quotes ("") marks. It
may be alphabet, numbers, special characters, blank space, etc. For example, "Jack",
"12345", "\n", etc.
Floating Point Literals
The vales that contain decimal are floating literals. In Java, float and double primitive types
fall into floating-point literals. Keep in mind while dealing with floating-point literals.
Floating-point literals for float type end with F or f. For example, 6f, 8.354F, etc. It is
a 32-bit float literal.
Floating-point literals for double type end with D or d. It is optional to write D or d.
For example, 6d, 8.354D, etc. It is a 64-bit double literal.
It can also be represented in the form of the exponent.
Floating:
Decimal:
Boolean Literals
Boolean literals are the value that is either true or false. It may also have values 0 and 1. For
example, true, 0, etc.
Null Literals
Null literal is often used in programs as a marker to indicate that reference type object is
unavailable. The value null may be assigned to any variable, except variables of primitive
types.
4.Special Symbols:
The following special symbols are used in Java having some special meaning and
thus, cannot be used for some other purpose.
[] () {}, ; * =
5.Operators
Operators:
Java provides
es many types of operators which can be used according to the need.
They are classified based on the functionality they provide. Some of the types are
are-
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator
10. Precedence and Associativity
Java Variables
A variable is a container
tainer which holds the value while the Java program is executed. A
variable is assigned with a data type.
Variable is a name of memory location. There are three types of variables in java: loc
local,
instance and static.
Variable
A variable is the name of a reserved area allocated in memory. In other words, it is a name of
the memory location. It is a combination of "vary + able" which means its value can be
changed.
Types of Variables
local variable
instance variable
static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware that the
variable exists.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an instance
variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared among
instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You can
create a single copy of the static variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when the class is loaded in the
memory.
Symbolic Constants : Constants are also like normal variables. But, the only
difference is, their values cannot be modified by the program once they are defined. Constants refer
to fixed values. They are also called as literals.
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long,
float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and
Arrays.
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
Example:
Example:
The short data type can also be used to save memory just like byte data type. A short data type is 2
times smaller than an integer.
Example:
The int data type is generally used as a default data type for integral values unless if there is no
problem about memory.
Example:
Example:
Example:
1. float f1 = 234.5f
Double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is
unlimited. The double data type is generally used for decimal values just like float. The double data
type also should never be used for precise values, such as currency. Its default value is 0.0d.
Example:
1. double d1 = 12.3
Example:
Operators –
1) Arithmetic operator – (+ - * / %)
2. Relational Operator –
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
3. Logical Operator
4. Bitwise operator
Operators Symbol Example
Bitwise AND & op1 & op2
Bitwise exclusive OR ^ op1 ^ op2
Bitwise inclusive OR | op1 | op2
Bitwise Compliment ~ ~ op
Bitwise left shift << op1 << op2
Bitwise right shift >> op1 >> op2
Unsigned Right Shift Operator >>> op >>> number of places to shift
5. Assignment Operator
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Escape Sequence –
Escape Characters Description
\t It is used to insert a tab in the text at this point.
\' It is used to insert a single quote character in the text at this point.
\" It is used to insert a double quote character in the text at this point.
\r It is used to insert a carriage return in the text at this point.
\\ It is used to insert a backslash character in the text at this point.
\n It is used to insert a new line in the text at this point.
\f It is used to insert a form feed in the text at this point.
\b It is used to insert a backspace in the text at this point.
Widening Casting
Widening casting is done automatically when passing a smaller size type to a larger size type:
Example
public class Main {
public static void main(String[] args) {
int myInt = 9;