Part 1
Part 1
1. Introduction to Java
2. OOP Concepts / Principles
3. Implementing java program
4. Java Buzzwords
5. JVM
6. Data types
7. Variables
8. Type conversion & Type casting
9. Operators
10. Control statements
11. Arrays
12. Class, Object and Methods
13. Constructors
14. This keyword
15. Static keyword
16. String class
17. StringBuffer class
18. String Tokenizer
Note:
1. If you use Mobile – Dcoder Android
2. If you use Laptop/Desktop with internet – onlinegdb.com(online compiler)
3. If you use Laptop/Desktop without internet- Install Jdk 8 or above version.
Introduction to java
1.JAVA introduction:-
Where it is used?
1. Desktop Applications such as acrobat reader, media player, antivirus etc.(Window
Applications)
2. Web Applications such as irctc.co.in, cleartrip.com etc.
3. Enterprise Applications such as banking, e-commerce applications.
4. Mobile Applications (J2ME)
5. Embedded System
6. Smart Card
7. Robotics
8. Games etc.
Types of Java Applications
1) Standalone Application
It is also known as desktop application or window-based application. An application that
we need to install on every machine such as media player, antivirus etc. AWT and Swing
are used in java for creating standalone applications. (J2SE)
2) Web Application
An application that runs on the server side and creates dynamic page, is called web
application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating web
applications in java. (J2SE)
3) Enterprise Application
An application that is distributed in nature, such as banking applications etc. It has the
advantage of high level security, load balancing and clustering. In java, EJB is used for
creating enterprise applications. (J2EE)
4) Mobile Application
An application that is created for mobile devices. Currently Android and Java ME are used
for creating mobile applications. (J2ME)
Evolution of Java
Currently, Java is used in internet programming, mobile devices, games, e-business
solutions etc.
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) Originally designed for small, embedded systems in electronic appliances like set-top
boxes.
3) Firstly, it was called "Green talk" by James Gosling and file extension was .gt.
4) After that, it was called Oak and was developed as a part of the Green project.
Why sun choosed "Oak" name?
5) Why Oak?
Oak is a symbol of strength and choosen as a national
tree of many countries like U.S.A., France, Germany,
Romania etc.
6) In 1995, Oak was renamed as "Java" because it was
already a trademark by Oak Technologies.
Why sun choose "Java" name?
7) Why they choosed java name for java language?
The team gathered to choose a 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.
8) Java is an island of Indonesia where first coffee was produced (called java coffee).
9) Notice that Java is just a name not an acronym.
10) Originally 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 released in(January 23, 1996).
Java Version History
There are many java versions that have been
released. Current stable release of Java is Java
SE 8.
1. JDK Alpha and Beta (1995) 12. Java SE 10(March, 20th 2018)
2. JDK 1.0 (23rd Jan, 1996) 13. Java SE 11 (18th March, 2014)
3. JDK 1.1 (19th Feb, 1997) 14. Java SE 12(March, 19th 2019)
4. J2SE 1.2 (8th Dec, 1998) 15. Java SE 13(September, 17th 2019)
5. J2SE 1.3 (8th May, 2000) 16. Java SE 14(March, 17th 2020)
6. J2SE 1.4 (6th Feb, 2002) 17. Java SE 15(September 2020)
7. J2SE 5.0 (30th Sep, 2004) 18. Java SE 20(March 2023)
8. Java SE 6 (11th Dec, 2006) 19. Java SE 21.0.3 (April 16, 2024)
9. Java SE 7 (28th July, 2011)
10. Java SE 8 (18th March, 2014)
11. Java SE 9 (September, 21st 2017)
Oops concept/principles
2. OOPs Concepts / OOPs Principles
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Dynamic
9. Interpreted
10. High Performance &
Multithreaded
11. Distributed
1. Simple
• According to Sun, Java language is simple because:
• Syntax is based on C & C++.
• removed many confusing and/or rarely-used features e.g., explicit pointers, operator
overloading, multiple inheritance etc.
• No need to remove unreferenced objects because java has Automatic Garbage Collection.
• Java takes less time to compile and execute the program.
2. Object-oriented
• Java is object oriented technology because to represent total data in the form of object.
• By using object reference we are calling all the methods, variables which is present in that
class.
• the total java language is dependent on object only hence we can say java is a object
oriented technology.
Basic concepts of OOPs are:
1. Object 4. Polymorphism
2. Class 5. Abstraction
3. Inheritance 6.Encapsulation
3. Platform independent
• Classloader adds security by separating the package for the classes of the local file system
from those that are imported from network sources with predefined library in the form of
java.Security.package.
• Bytecode Verifier: checks the code fragments for illegal code that can violate access right to
objects.
• Security Manager: determines what resources a class can access such as reading and writing
to the local disk.
• These security are provided by java language. Some security can also be provided by
application developer through SSL, JAAS (Java Authentication and Authorization Services),
Cryptography etc.
5. Robust
JAVA is Robust because
• JAVA is having very good predefined Exception Handling mechanism.
• JAVA is having very good memory management system that is Dynamic Memory
10. Multi-threaded
• A thread is like a separate program, executing concurrently.
• Thread is a light weight process and a small task in large program.
• 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, gaming etc.
11. Dynamic
Java is dynamic technology it follows dynamic memory allocation(at runtime the memory is
allocated).
Implementation of java program
3. BASIC STRUCTURE OF A JAVA PROGRAM
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome To the World of Java");
}
}
class keyword is used to declare a class in java.
public keyword is an access modifier which represents visibility, it means it is visible to all.
static is a keyword, if we declare any method as static, it is known as static method. The core advantage of
static method is that there is no need to create object to invoke the static method. The main method is
executed by the JVM, so it doesn't require to create object to invoke the main method. it saves memory.
void is the return type of the method which doesn't return any value.
main represents startup of the program.
String[] args is used for command line argument.
System.out.println() is used print statement.
Once the run-time package exists for a given system, any Java program can run on it. Remember, although
the details of the JVM will differ from platform to platform, all understand the same Java byte code.
JVM Architecture
- JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime
environment in which java byte code can be executed.
- JVMs are available for many hardware and software platforms.
- Java is platform independent.
import java.util.*;
public class Grade
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Entera the Marks in 3 Subjects");
double m1 = sc.nextDouble();
double m2 = sc.nextDouble();
double m3 = sc.nextDouble();
double avg = (m1+m2+m3)/3;
System.out.println("The average is "+avg);
}
}
7. Java Variables
Variable is a name given to memory location.
A Variable has
1. NAME (VALID IDENTIFIER)
2. VALUE
3. TYPE
4. SCOPE & LIFETIME
Types of Variable
There are three types of variables in java:
1) Local Variable 3) Static variable
The variables which are declare inside a method or a block or a The instance variable that is declared as static is called static
constructor is called local variables. variable. It cannot be local. Scope is within a class. (Stored in
The scope of local variables are inside a method or inside a Class Area of Memory).
constructor or inside a block. (Stored in Stack Memory) Able to access static variables within the class any number of
Ex: class Test { times.
public static void main(String[] args) {
int a=10; Ex: class Test {
int b=20; static int a=10, b=20;
System.out.println(a+b); public static void main(String[] args) {
}} System.out.println(a); //1-way(directly possible)
2) Instance Variable System.out.println(Test.a); //2-way(By using class name)
A variable which is declared inside the class but outside the Test t=new Test();
method is called instance variable. (Stored in Heap Memory). System.out.println(t.a); //3-way(By using reference variable)
Ex: class Test { System.out.println(a+b);
int a=10; }
int b=20; void add()
void add() { {
System.out.println(a+b); } System.out.printl(a+b);
public static void main(String[] args) { }
Test t=new Test(); 4) Reference variable
System.out.println(t.a+t.b); A variable that refers to object of a class. (Stored in Stack Area
t.add(); of Memory)
}}
8. Type Conversions and Type Casting
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.
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.
//example program for Widening Type Casting
Output:
hyd
bag
che
9. Operators in Java
a) Arithmetic Operators (Arithmetic Operation)
byte a=10;
System.out.println(a<<2); //40
byte b=-15;
System.out.println(b<<3); //-120
byte c=10;
System.out.println(c>>2); //2
byte d=-15;
System.out.println(d>>1); //-8
}
}
Java bitwise Operators Example: >> , >>>
class Oper3 public class OperatorExample
{ {
public static void main(String args[]) public static void main(String args[])
{ {
//For positive number, >> and >>> works same int a=10;
System.out.println(20>>2); int b=20;
System.out.println(20>>>2); a+=4;//a=a+4 (a=10+4)
//For nagative number, >>> changes MSB to 0 b-=4;//b=b-4 (b=20-4)
System.out.println(-20>>2); System.out.println(a);
System.out.println(-20>>>2); System.out.println(b);
} }
} }
public class OperatorExample Java Assignment Operators Example
{ class OperatorExample
public static void main(String args[]) {
{ public static void main(String[] args)
System.out.println(10*10/5+3-1*4/2); //21 {
} } int a=10;
a+=3; //10+3
System.out.println(a);
a-=4; //13-4
System.out.println(a);
a*=2; //9*2
System.out.println(a);
a/=2; //18/2
System.out.println(a);
}
}
10. Control Statements
Conditional Statements
If Statement If else Statement
In Java if else statement is used to test the
The Java if statement is used to test
condition. If condition is true it executes true
the condition. If condition is true it
block, otherwise executes false block.
executes true block.
o if-else statement (two way)
o if statement (one way) if(condition)
if(condition) {
{ //code to be executed
//code to be executed }
} else
class Test { {
public static void main(String[] args) //code to be executed
{ }
int a=10;
if (a>5) Program to find whether given number is
System.out.println(“this is if block"); leap year or not.
}
}
nested if statement ( if inside another if-else-if ladder (Multi way selection)
if) if(condition1)
if(condition1) {
{ //code to be executed
if(condition2) }
{ else if(condition2)
//code to be executed {
} //code to be executed
//code to be executed }
} -----------------------
else
{
//code to be executed
}
Program to find ascending order of three Program to read a number and display day.
numbers
Switch Statement
switch statement is used to take multiple selections.
It is like if-else-if ladder statement.
Inside the switch It is possible to declare any number of cases but is possible to declare only
one default.
switch(expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
// code to be executed if all cases are not matched;
}
***case values can be integer constant, character constant and string constant .
public class Example
{
public static void main(String[] args)
{
int number=20;
switch(number)
{
case 10: System.out.println("CSE");
break;
case 20: System.out.println("EEE");
break;
case 30: System.out.println("ECE");
break;
default: System.out.println("Invalid input");
Break;
}
}
}
Program to implement arithmetic operations using
switch statement.
Loop / Repetitive / Iterative Statements
if we want to execute group of statements repeatedly
or more number of times then we should go for class Test
iteration statements. {
Three types of iteration statements present in the public static void main (String[] args)
java language {
1) For int i=0;
2) while while (i<10)
3) do-while {
System.out.println(“CSM”);
while Loop i++;
The Java while loop is used to iterate a part of the }
program several times. If the number of iteration is }
not fixed, it is recommended to use while loop. }
Syntax:
while(condition)
{
//code to be executed
}
do-while Loop
• If we want to execute the loop body at least one time them we should go for do-while
statement.
• In the do-while first body will be executed then only condition will be checked.
• In the do-while the while must be ends with semicolon otherwise we are getting compilation
error.
• do is taking the body and while is taking the condition and the condition must be Boolean
condition. class Test
{
Syntax:
public static void main(String[] args)
do
{
{
int i=0;
//code to be executed do
}while(condition); {
System.out.println(“CSM");
i++;
}while (i<10);
}
}
for Loop
The Java for loop is used to iterate a part of the program several times.
If the number of iteration is fixed, it is recommended to use for loop.
example
for(int i=1;i<=10;i++)
{
if(i==5)
{
break;
}
}
System.out.println(i);
Continue Statement
• The Java continue statement is used to continue loop.
• We might want to continue running the loop but stop processing the remainder of the code in
its body for this particular iteration.
• It continues the current flow of the program and skips the remaining code at specified
condition.
• In case of inner loop, it continues only inner loop.
Example:
for(int i=1;i<=6;i++)
{
if(i==3)
{
continue;
}
System.out.println(i);
}
return Statement