1. Introduction to Java - Walys (10)
1. Introduction to Java - Walys (10)
Introduction to Java
JAVA
Java was introduced in 23rd January 1996. But it was started by Sun
Microsystems in 1991. At that time Sun Microsystems had a requirement to
prepare software for consumer electronic devices like remote controllers,
cable TV switch boxes etc… They create 30 members team with James
Gosling and Patrck Naughtan starts "Green" project.
The language was initially called Oak after an oak tree that stood
outside Gosling's office. Later the project went by the name Green and was
finally renamed Java, from Java coffee, the coffee from Indonesia.
1
WALYS Life Skills International Pvt. Ltd. Introduction to Java
JAVA introduction:-
Author: James Gosling
Vendor: Sun Micro System(merged into Oracle Corporation)
Objective: To prepare simple electronic consumer goods.
Project: Green
Initial Name: OAK language
Present Name: Java
First Version: JDK1.0 [1996, Jan-23rd]
Type: open source & free software
Symbol: coffee cup with saucer
SUN: Stanford Universally Network
Slogan/Motto: WORA (write once run anywhere)
2
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Importance of java: - According to the SUN, 3 billion devices run on the java
language only.
1) Java is used to develop Desktop Applications such as MediaPlayer,
Antivirus etc.
2) Java is Used to Develop Web Applications such as sravyajobs.com,
irctc.co.in etc.
3) Java is used to Develop Enterprise Application such as Banking
applications.
4) Java is used to Develop Mobile Applications.
5) Java is used to Develop Embedded System.
6) Java is used to Develop SmartCards.
7) Java is used to Develop Robotics.
8) Java is used to Develop Games ………………………..etc.
Java Evolutions:
Version Name
Code Release Date
Name
This was the initial release and was originally
JDK 1.0 Oak January 1996
called Oak. This had very unstable APIs and
one java web browser named WebRunner.
The first stable version, JDK 1.0.2, was called
Java 1.
3
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Regular expressions
Exception chaining
Internet Protocol version 6 (IPv6)
support
New I/O; NIO
Logging API
Image I/O API
Integrated XML parser and XSLT
processor (JAXP)
Integrated security and cryptography
extensions (JCE, JSSE, JAAS)
Java Web Start
Preferences API (java.util.prefs)
4
WALYS Life Skills International Pvt. Ltd. Introduction to Java
5
WALYS Life Skills International Pvt. Ltd. Introduction to Java
ForkJoin Framework
NIO 2.0 having support for multiple file
systems, file metadata and symbolic
links
WatchService
Timsort is used to sort collections and
arrays of objects instead of merge sort
APIs for the graphics features
Support for new network protocols,
including SCTP and Sockets Direct
Protocol
6
WALYS Life Skills International Pvt. Ltd. Introduction to Java
JAVA 10 March, 20th 2018 JEP 286: Local Variable Type Inference
JEP 322: Time-Based Release Versioning
JEP 304: Garbage-Collector Interface
JEP 307: Parallel Full GC for G1
JEP 316: Heap Allocation on Alternative
Memory Devices
JEP 296: Consolidate the JDK Forest into
a Single Repository
JEP 310: Application Class-Data Sharing
JEP 314: Additional Unicode Language-
Tag Extensions
JEP 319: Root Certificates
JEP 317: Experimental Java-Based JIT
Compiler
JEP 312: Thread-Local Handshakes
JEP 313: Remove the Native-Header
Generation Tool
New Added APIs and Options
Removed APIs and Options
7
WALYS Life Skills International Pvt. Ltd. Introduction to Java
8
WALYS Life Skills International Pvt. Ltd. Introduction to Java
G1 – JEP 345
JFR Event Streaming – JEP 349
Non-Volatile Mapped Byte Buffers –
JEP 352
ZGC on macOS – JEP 364
ZGC on Windows – JEP 365
Foreign-Memory Access API
(Incubator) – JEP 370
9
WALYS Life Skills International Pvt. Ltd. Introduction to Java
10
WALYS Life Skills International Pvt. Ltd. Introduction to Java
JAVA 21 September
2023
Up to Java6 version, Java managed by Sun Microsystems and from Java7
version onwards Java managed by Oracle Corporation.
Parts of the java:-
Core java & Advanced Java are not official words, as per the Sun Micro
System standard the java language is divided into three parts.
1) J2SE
2) J2EE
3) J2ME
11
WALYS Life Skills International Pvt. Ltd. Introduction to Java
12
WALYS Life Skills International Pvt. Ltd. Introduction to Java
1. Simple
Java is simple programming language, because,
1.Java applications will take less memory and less execution time.
2.Java has removed almost all the confusion oriented features like
pointers, multiple inheritance,.....
3.Java is using all the simplified syntaxes from C and C++.
2. Object Oriented
Java is an object-oriented programming language, because, JAVA is able to
store data in the form of Objects only.
13
WALYS Life Skills International Pvt. Ltd. Introduction to Java
3. Platform Independent
Java is platform independent programming Language, because, Java allows
its applications to compile on one operating system and to execute on another
operating system.
4. Architectural Neutral
Java is an Arch Nuetral Programming language, because, Java allows its
applications to compile on one H/W Arch and to execute on another H/W
Arch.
5. Portable
Java is a portable programming language, because, JAVA is able to run its
applications under all the operating systems and under all the H/W Systems.
6. Robust
Java is Robust programming language, because,
1.Java is having very good memory management system in the form of heap
memory Management System, it is a dynamic memory management system,
it allocates and deallocates memory for the objects at runtime.
2.JAVA is having very good Exception Handling mechanisms, because, Java
has provided very good predefined library to represent and handle almost all
the frequently generated exceptions in java applications.
7. Secure
Java is very good Secure programming language, because,
1.JAVA has provided an implicit component inside JVM in the form of
"Security Manager" to provide implicit security.
2.JAVA has provided a seperate middleware service in the form of JAAS
[Java Authetication And Autherization Service] inorder to provide web security.
3.Java has provided very good predefined implementations for almost all well
known network security algfor the Java applications java.security package.
8. Dynamic
If any programming language allows memory allocation for primitive data
types at RUNTIME then that programming language is called as Dynamic
Programming Language.
14
WALYS Life Skills International Pvt. Ltd. Introduction to Java
15
WALYS Life Skills International Pvt. Ltd. Introduction to Java
16
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Comment Section:
Comments are used to write the detailed description about application logics
to understand the logics easily.
When we write the comments the application maintenance will become easy.
Comments are non-executable code these are ignored during compilation.
There are 3 types of comments.
1) Single line Comment: -Possible to write the description in single line.
Syntax:- //description
2) Multi line Comment: -To write the description in more than one line. Starts
/* ends */
Syntax: - /* satement-1
Statement-2
17
WALYS Life Skills International Pvt. Ltd. Introduction to Java
;;;;;;;;;;;;;;
Statement-n
*/
3)Documentation Comments: - To write the description in more than one
page.
Syntax: - /*
*statement-1
*statement-2
*/
Note: We will use documentation comments to prepare API kind of
documentations, but it is not suggestible.
2. Package Section:
Package is a collection of related classes and interfaces as a single unit.
There are two types of packages in java:
Predefined packages
These packages are provided by Java along with software.
Ex: java.io
java.util
java.awt
User-defined Package
These packages are provided by the developers as per their application
requirements.
To declare user defined packages in java program we have to use
"Package section".
If we want to declare user defined packages in java programmer then we have
to use the following
Syntax.
package package_Name ;
Ex:
package satish;
18
WALYS Life Skills International Pvt. Ltd. Introduction to Java
3. import Section:
We want to use a particular package classes and interfaces in the present
java file, we can use import statement.
The default import package is java.lang.*;
Syntax:
import package_Name.*;
It will import all the classes and interfaces from the specified package.
Ex:
import java.io.*;
import package_Name.Member_Name;
It will import only the specified class or interface from the specified package.
Ex:
import java.io.BufferedReader
In java files, we are able to provide more than one import statement.
A java program with import statement:
import java.io.*;
---
---
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
----
----
A Java program without import statement:
java.io.BufferedReader br=new java.io.BufferedReader(new
java.io.InputStreamReader(System.in));
classes/interfaces section:
In Java files, we are not having any restrictions about the number of classes
and interfaces, it is possible to provide any no of classes and interfaces with in
a single java file.
19
WALYS Life Skills International Pvt. Ltd. Introduction to Java
20
WALYS Life Skills International Pvt. Ltd. Introduction to Java
21
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Compiler check the syntax errors, If the application contains syntax errors
then compiler will generate error message in the form of compilation error.
If the application does not contain syntax, errors then compiler translate
.java to .class file.
Note: - In java compiler generate .class files based on number of classes
present in source file.
If the source file contains 100 classes after compilation compiler generates
100 .class files
5. Execution process.
Run /execute the java application by using java command.
Syntax:- java classname
java Test
D:\satish>java Test
Hi Satish
Note:
1. JVM will take Main_Class name from command prompt.
2. JVM will search for Main_Class at current location,at Java predefined
library and at the locations referred by "classpath" environment variable.
3. If the required Main_Class .class file is not available at all the above
locations then JVM will provide the following.
JAVA6: java.lang.NoClassDefFoundError:Test
JAVA7: Error:Could not find or load main class Test
4. If the required main class .class file is available at either of the above
locations then JVM will load main class bytecode to the memory by using
"Class Loaders".
5. After loading main class bytecode to the memory, JVM will search for
main() method.
6. If main() method is not available at main class byteCode then JVM will
provide the following.
JAVA6: java.lang.NoSuchMethodError:main
22
WALYS Life Skills International Pvt. Ltd. Introduction to Java
JAVA7: Error:Main method not found in class Test, please define main
method as:
public static void main(String args[])
7. If main() method is available at main class bytecode then JVM will access
main() method by creating a thread called as "Main thread".
8. JVM will access main() method to start application execution by using main
thread, when main thread reached to the ending point of main() method then
main thread will be in destroyed/dead state.
9. When main Thread is in dead state then JVM will stop all of its internal
processes and JVM will go to ShutDown mode.
23
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Main Method
Whether the class contains main() method or not and whether it is properly
declared or not these checking‟s are not responsibilities of the compiler, at
runtime JVM is responsible for this.
Example:
class Test
{ }
Output:
javac Test.java
java Test
Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)
· JVM always searches for the main() method with the following signature.
· If we are performing any changes to the above signature then the code won‟t
run and will get Runtime exception saying Main method not found in class
Test.
24
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Example:
main(String... args)
5) main() method can be declared with the following modifiers.
· final, synchronized, strictfp.
25
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Language Fundamentals:
To prepare java applications, we need some fundamentals provided by Java
programming language.
1. Tokens
2. Data Types
3. Type Casting
4. Java Statements
5. Arrays
1. Tokens:
Smallest logical unit in java programming is called as "Lexeme".
The Collection of Lexemes come under a particular group is called as "Token"
int a=b+c*d;
Lexemes: int, a, =, b,+, c, *, d, ;-----> 9
Tokens:
1) Data Types: int
2) Identifiers: a, b, c, d
3) Operators: =, +, *
4) Special Symbol: ;
Types of tokens: 4
To prepare java applications, java has provided the following list of tokens.
1. Identifiers
2. Literals
3. Keywords/ Reserved Words
4. Operators
26
WALYS Life Skills International Pvt. Ltd. Introduction to Java
27
WALYS Life Skills International Pvt. Ltd. Introduction to Java
2. Literals:
Literal is a constant assigned to the variables.
EX:
int a=10;
int ----> data type
a ------> variables/ identifier
= ------> Operator
10 -----> constant[Literal].
; ------> Special symbol.
To prepare java programs, JAVA has provioded the following set of literals.
1. Integer / Integral Literals:
byte, short, int, long ----> 10, 20, 30,.... char -----> 'A','B',.....
28
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Reserved Words: 53
In Java some words are reserved to represent some meaning or
functionality, such type of words are called reserved words.
Conclusions:
• All 53 reserved words in Java contain lower case alphabets.
• In Java, we have only new keyword, and there is no delete keyword
because destruction of useless objects is the responsibility of garbage
collector.
• The following new keywords in Java.
strictfp(1.2), assert(1.4), enum(1.5)
29
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Operators:-
Operator is a symbol used to perform the operation. There are different types
of operators in Java
1. Arithmetic Operators:-
+,-,*,/,%
Ex:
class Test
int a=10,b=20;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
System.out.println(a%b);
System.out.println(10*10/5+3-1*4/2);
<,>,<=,>=,==,!=
Ex:
class ComparisonDemo
30
WALYS Life Skills International Pvt. Ltd. Introduction to Java
int value1 = 1;
int value2 = 2;
if(value1 == value2)
System.out.println("value1 == value2");
if(value1 != value2)
System.out.println("value1 != value2");
++,--
31
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Example :-
class Test
int a=10;
System.out.println(a++);
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
Example :
class Test
byte b1=-10;
byte b2=+10;
System.out.println(~b1);
System.out.println(~b2);
boolean b=true;
System.out.println(!b);
32
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Example :-
class Test
int a=10;
System.out.println(a++ + ++a );
System.out.println(a++ - ++a );
System.out.println(a-- + --a );
System.out.println(a-- - --a );
4.Logical operator:-
&&, ||, !
33
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Ex:
class Test
int a=20,b=10,c=30;
5.Bitwise operators:-
&, |, ^,~,<<,>>,>>>
Ex:
int a=12,b=5;
34
WALYS Life Skills International Pvt. Ltd. Introduction to Java
System.out.println(a&b);
System.out.println(a|b);
System.out.println(a^b);
System.out.println(~a);
The logical && operator does not check second condition if the first condition
is false. It checks the second condition if the first condition is true.
The bitwise & always checking both conditions if the condition is true or false.
Ex:
class Test
int a=10,b=20,c=30;
Ex:
35
WALYS Life Skills International Pvt. Ltd. Introduction to Java
class Test
int a=10,b=20,c=30;
System.out.println(a);
System.out.println(a);
The logical || operator does not check the second condition if the first condition
true. It will check the second condition if the first condition false.
The bitwise | always checking both conditions if the condition is true or false.
Ex:
class Test
int a=10,b=20,c=30;
System.out.println(a<b || a++<c);
System.out.println(a);
36
WALYS Life Skills International Pvt. Ltd. Introduction to Java
System.out.println(a<b | a++<c);
System.out.println(a);
Note :
shift Operators
The left shift operator << is used to shift all of the bits in a value to the left side
of a specified number of times.
Example
class OperatorExample
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
37
WALYS Life Skills International Pvt. Ltd. Introduction to Java
The right shift operator >> is used to move left operands value to right by the
number of bits specified by the right operand.
Example
class OperatorExample
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
class OperatorExample
System.out.println(20>>2);
System.out.println(20>>>2);
38
WALYS Life Skills International Pvt. Ltd. Introduction to Java
System.out.println(-20>>2);
System.out.println(-20>>>2);
class ConditionalDemo2
int value1 = 1;
int value2 = 2;
System.out.println(result);
Assignment operator :
Simple assignment:
Chained assignment:
Example:
int a,b,c,d;
a=b=c=d=20;
39
WALYS Life Skills International Pvt. Ltd. Introduction to Java
System.out.println(a+"---"+b+"---"+c+"---"+d);//20---20---20---20
Compound assignment:
The following is the list of all possible compound assignment operators in java.
a+=5; //a=a+5;
Data types:-
1. Data types are used to represent type of the variable & expressions.
2. Representing how much memory is allocated for variable.
3. Specifies range value of the variable.
Note: Java is strictly a typed programming language.
40
WALYS Life Skills International Pvt. Ltd. Introduction to Java
byte: - This data type is suitable for file stream classes. Java file streams are
read and write data in byte form.
Size: 1byte (8bits)
Range:-128to 127[-27 to 27-1]
41
WALYS Life Skills International Pvt. Ltd. Introduction to Java
42
WALYS Life Skills International Pvt. Ltd. Introduction to Java
String: -
String is not a data type & it is a class present in java.lang package to
represent group of characters or character array enclosed with in double
quotes.
The default value of the String is null
43
WALYS Life Skills International Pvt. Ltd. Introduction to Java
44
WALYS Life Skills International Pvt. Ltd. Introduction to Java
System.out.println ( );
• This statement is used for displaying the data or messages on to the console
(monitor).
• • The object of print stream class is called out is created as a static data
member in system class (system is a predefined class).
• Hence to call the println method we must use the following statement:
print():-This method used to print the data after printing control present in
same line.
println():-This method used to print the data after printing control present in
new line.
Ex:
class Test
System.out.print("satish");
System.out.print("lokesh");
System.out.println("rajesh");
System.out.println("gopi");
45
WALYS Life Skills International Pvt. Ltd. Introduction to Java
class Test
46
WALYS Life Skills International Pvt. Ltd. Introduction to Java
class Sum
int a,b;
a=10;
b=20;
int s=a+b;
System.out.println("sum="+s);
double x = 9;
double y = 25;
47
WALYS Life Skills International Pvt. Ltd. Introduction to Java
class Average
int a,b,c;
a=20;
b=30;
c=50
float s=(a+b+c)/3;
System.out.println("Average="+s);
Type casting: Type casting is a method for converting one data type to
another data type.
48
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Ex:
int x=‟a‟;
System.out.println(x); 97
double d=10;
System.out.println(d); 10.0
EX:
class Test
{
public static void main(String[] args)
{
byte b=10;
byte i=b;
System.out.println(b+" "+i);
}
}
Status: No Compilation Error
10 10
49
WALYS Life Skills International Pvt. Ltd. Introduction to Java
type is compatible with left side variable data type then compiler will not rise
any error and compiler will not perform any type casting.
When we execute the above code, when JVM encounter the above
assignment statement then JVM will perform the following two actions.
1.JVM will convert right side variable data type to left side variable data type
implicitly [Implicit Type Casting]
2.JVM will copy the value from right side variable to left side variable.
Note: Type Checking is the responsibility of compiler and Type Casting is the
responsibility of JVM.
EX:
class Test
{
public static void main(String[] args)
{
int i=10;
byte b=i;
System.out.println(i+" "+b);
}
}
Status: Compilation Error, Possible loss of precision.
EX:
class Test
{
public static void main(String[] args)
{
byte b1=30;
byte b2=30;
byte b=b1+b2;
50
WALYS Life Skills International Pvt. Ltd. Introduction to Java
System.out.println(b);
}
}
Status: Compilation Error, Possible loss of precision.
Reason: X,Y and Z are three primitive data types.
X+Y=Z
1.If X and Y belongs to {byte, short, int} then Z should be int.
2.If either X or Y or both X and Y belongs to {long, float, double} then Z should
be higher(X,Y).
byte+byte=int
byte+short=int
short+int=int
byte+long=long
long+float=float
float+double=double
EX:
class Test
{
public static void main(String[] args)
{
long l=10;
float f=l;
System.out.println(l+" "+f);
}
}
Status: No Compilation Error
OP: 10 10.0
51
WALYS Life Skills International Pvt. Ltd. Introduction to Java
EX:
class Test
{
public static void main(String[] args)
{
float f=22.22f;
long l=f;
System.out.println(f+" "+l);
}
}
Status: Compilation Error, possible loss of precision.
Reason:
two class rooms
Class Room A: 25[Size] banches---> 3 members per bench---> 75
Class Room B: 50[Size] banches---> 1 member per bench----> 50
long---> 8 bytes--> less data as per its internal data arrangement.
float--> 4 bytes--> more data as per its internal data arrangement.
Due to the above reason, float data type is higher when compared with long
data type so that, we are able to assign long variable4 to float variable directly,
but, we are unable to assign float variable to long variable directly.
The process of converting data from higher data type to lower data type is
called as Explicit Type Casting.
To perform explicit type casting we have to use the following pattern.
P a = (Q) b;
(Q)----> Cast operator
Where P and Q are two primitive data types, where Q must be either same as
P or lower than P as per implicit type casting chart.
52
WALYS Life Skills International Pvt. Ltd. Introduction to Java
EX:
class Test
{
public static void main(String[] args)
{
int i=10;
byte b=(byte)i;
System.out.println(i+" "+b);
}
}
Status: No Compilation Error
OP: 10 10
When we compile the above code, when compiler encounter the above
assignment statement, compiler will check whether cast operator provided
data type is compatible with left side variable data type or not, if not, compiler
will rise an error like "Possible loss of precision". If cast operator provided data
type is compatible with left side variable data type then compiler will not rise
any error and compiler will not perform type casting.
When we execute the above program, when JVM encounter the above
assignment statement then JVM will perform two actions.
1.JVM will convert right side variable data type to cast operator provided data
type.
2.JVM will copy value from right side variable to left side variable.
Example:
int x=130;
byte b=(byte)x;
System.out.println(b);//-126
53
WALYS Life Skills International Pvt. Ltd. Introduction to Java
Analysis:
Java.util.Scanner(Dynamic Input):-
import java.util.*;
class Sum
54
WALYS Life Skills International Pvt. Ltd. Introduction to Java
int a,b;
a=s.nextInt();
b=s.nextInt();
int s=a+b;
System.out.println("sum="+s);
Ex:
import java.util.*;
class Test
int eno=s.nextInt();
55
WALYS Life Skills International Pvt. Ltd. Introduction to Java
String ename=s.next();
float esal=s.nextFloat();
System.out.println("*****emp details*****");
System.out.println("emp no----->"+eno);
System.out.println("emp name---->"+ename);
System.out.println("emp sal------>"+esal);
System.out.println("emp hobbies------>"+ehobbies);
56