Java Unit I 2022
Java Unit I 2022
History of Java :–
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
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.
6) In 1995, Oak was renamed as "Java" because it was already a trademark by Oak
Technologies.
new name. The suggested words were "dynamic", "revolutionary", "Silk", "jolt", "DNA", etc.
They wanted something that reflected the essence of the technology: revolutionary,
dynamic, lively, cool, unique, and easy to spell, and fun to say.
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.
Features of Java Programming
Following are the notable features of Java:
Object Oriented
In Java, everything is an Object. Java can be easily extended since it is based
on the Object model.
Platform Independent
Unlike many other programming languages including C and C++, when Java is
compiled, it is not compiled into platform specific machine, rather into platform-
independent byte code. This byte code is distributed over the web and interpreted by the
Virtual Machine (JVM) on whichever platform it is being run on.
Simple
Java is designed to be easy to learn. If you understand the basic concept of OOP
Java, it would be easy to master.
Secure
With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.
Architecture-neutral
Java compiler generates an architecture-neutral object file format, which makes
the compiled code executable on many processors, with the presence of Java runtime
system.
Portable
Being architecture-neutral and having no implementation dependent aspects of
the specification makes Java portable. The compiler in Java is written in ANSI C with a
clean portability boundary, which is a POSIX subset.
Robust
Java makes an effort to eliminate error-prone situations by emphasizing mainly on
compile time error checking and runtime checking.
Multithreaded
With Java's multithreaded feature it is possible to write programs that can perform
many tasks simultaneously. This design feature allows the developers to construct
interactive applications that can run smoothly.
Interpreted
Java byte code is translated on the fly to native machine instructions and is not
stored anywhere. The development process is more rapid and analytical since the linking
is an incremental and light-weight process.
High Performance
Distributed
Java is designed for the distributed environment of the internet.
Dynamic
Java is considered to be more dynamic than C or C++ since it is designed to adapt
to an evolving environment. Java programs can carry an extensive amount of run-time
information that can be used to verify and resolve accesses to objects at run-time
#3) Portability
C++ Java
C++ code is not portable. Java, however, translates the code into byte
It must be compiled for each platform. code.This byte code is portable and can be
executed on any platform.
#6) Overloading
C++ Java
In C++, methods and operators can be In Java, only method overloading is
overloaded. This is static polymorphism. allowed.
It does not allow operator overloading.
#8) Pointers
C++ Java
C++ is all about pointers.As seen in tutorials Java has limited support for pointers.
earlier, C++ has strong support for pointers and Initially, Java was completely without
we can do a lot of useful programming using pointers but later versions started
pointers. providing limited support for pointers.
We cannot use pointers in Java as leisurely
as we can use in C++.
C++ Java
C++ doesn’t have in-built thread support. It Java is in-built thread support with a class
mostly relies on third-party threading libraries. ―thread‖. We can inherit the thread class
and then override the run method.
Java Virtual Machine :-
Loads code
Verifies code
Executes code
Java Tokens
In Java, the program contains classes and methods. Further, the
methods contain the expressions and statements required to perform a specific operation.
These statements and expressions are made up of tokens. In other words, we can say that
the expression and statement is a set of tokens. The tokens are the small building blocks
of a Java program that are meaningful to the Java compiler. Further, these two
components contain variables, constants, and operators. In this section, we will
discuss what is tokens in Java.
What is token in Java
The Java compiler breaks the line of code into text (words) is called Java tokens.
These are the smallest element of the Java program. The Java compiler identified these
words as tokens. These tokens are separated by the delimiters. It is useful for compilers to
detect errors. Remember that the delimiters are not part of the Java tokens.
In the above code snippet, public, class, Demo, {, static, void, main,
(, String, args, [, ], ), System, ., out, println, javatpoint, etc. are the Java tokens.The Java
compiler translates these tokens into Java bytecode. Further, these bytecodes are
executed inside the interpreted Java environment.
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable. There
are two types of data types in Java:
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.
o The main difference between primitive and non-primitive data types are:
o Primitive types are predefined (already defined) in Java. Non-primitive types are
created by the programmer and are not defined by Java (except for String).
o A primitive type has always a value, while non-primitive types can be null.
o A primitive type starts with a lowercase letter, while non-primitive types start with
an uppercase letter.
o The size of a primitive type depends on the data type, while non-primitive types
have all the same size.
Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Java Operator Precedence
multiplicative */%
Arithmetic
additive +-
bitwise inclusive OR |
Ternary ternary ?:
1) If statement
If statements in Java is used to control the program flow based on
some condition, it's used to execute some statement code block if the expression
evaluated to true; otherwise, it will get skipped. This statement is the simplest way to
modify the control flow of the program.
If (test expression)
{
Statement 1;
Statement 2;
...
}
Example-
if(b>a)
{
System.out.println("b is greater");
}
}
If else statements
If else statements in Java is also used to control the program flow based on
some condition, only the difference is: it's used to execute some statement code block if
the expression is evaluated to true, otherwise executes else statement code block.
Syntax
If (test expression)
{
//execute your code
}
else
{
//execute your code
}
Example
Else if statements
else if statements in Java is like another if condition, it's used in the program
when if statement having multiple decisions.
Syntax
If (test expression)
{
//execute your code
}
else if (test expression n)
{
//execute your code
}
else
{
//execute your code
}
Example
public class Sample
{
public static void main(String args[]) {
int a = 30, b = 30;
if (b > a)
{
System.out.println("b is greater");
}
else if(a > b)
{
System.out.println("a is greater");
}
else
{
System.out.println("Both are equal");
}
}
}
Java switch statement is used when you have multiple possibilities for the if statement.
Syntax
switch (variable)
{
case 1:
//execute your code
break;
case n:
//execute your code
break;
default:
//execute your code
break;
}
Example
O/P
while loops
do while loops
for loops
1) while loop
Java while loops statement allows to repeatedly run the same block of
code until a condition is met.while loop is the most basic loop in Java. It has one control
condition and executes as long the condition is true. The condition of the loop is tested
before the body of the loop is executed; hence it is called an entry-controlled loop.
Syntax
While (condition)
{
statement(s);
Incrementation;
}
Example :-
Example -
public class Sample
{
Syntax
Example –
In Java, type casting is a method or process that converts a data type into
another data type in both ways manually and automatically. The automatic conversion is
done by the compiler and manual conversion performed by the programmer. In this
section, we will discuss type casting and its types with proper examples.
Type casting
Convert a value from one data type to another data type is known as type casting.
Converting a lower data type into a higher one is called widening type casting. It is also
known as implicit conversion or casting down. It is done automatically. It is safe because
there is no chance to lose data. It takes place when:
byte -> short -> char -> int -> long -> float -> double
For example, the conversion between numeric data type to char or Boolean is not done
automatically. Also, the char and Boolean data types are not compatible with each other.
Let's see an example.
WideningTypeCastingExample.java
Output
In the above example, we have taken a variable x and converted it into a long type. After
that, the long type is converted into the float type.
Converting a higher data type into a lower one is called narrowing type casting. It is also
known as explicit conversion or casting up. It is done manually by the programmer. If we
do not perform casting then the compiler reports a compile-time error.
double -> float -> long -> int -> char -> short -> byte
In the following example, we have performed the narrowing type casting two times. First,
we have converted the double type into long data type after that long data type is
converted into int type.
NarrowingTypeCastingExample.java
Output
Command Line Argument in Java is the information that is passed to the program when
it is executed. The information passed is stored in the string array passed to the main() method
and it is stored as a string. It is the information that directly follows the program’s name on the
Command Line Arguments can be used to specify configuration information while launching your
application.
There is no restriction on the number of java command line arguments. You can specify any
number of arguments
class CommandLineExample
{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}
-----------------------------------------------------------------------------------------------------
compile by > javac CommandLineExample.java
In this example, we are printing all the arguments passed from the command-line. For this
purpose, we have traversed the array using for loop.class A{
public static void main(String args[])
{
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
compile by > javac A.java
run by > java A sonoo jaiswal 1 3 abc
Output: sonoo
jaiswal
1
3
abc
install the JDK if you don't have installed it, download the JDK and install it.
class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
Output:Hello Java