You are on page 1of 250

IO N

V A T
N O
Core Java

R IN
Programming

T C
TCR Innovation Core Java
IO N
V A T
O
What is the
Understanding What exactly is Java

N N
Programming and how it works.

I
goal of this Learning how to create basic applications

C R
with Java.

T
Course Understanding how to work with basic
and more advanced Java features

TCR Innovation Core Java


O N
What is Java programming language

A T I
O V
•A programming language used by developers

N N
to create various applications

I
•How hard is this programming language to

C R
learn

T
•Where exactly Java is used.

TCR Innovation Core Java


N
JAVA

T IO
V A
Java is an object-oriented, platform-independent

O
programming language.

N N
High level

I
Robust

R
Java codes are compiled into byte code r machine

T C
independent code. This byte code is run on JVM (Java
Virtual Machine)

Object oriented – mimic real-life objects

TCR Innovation Core Java


O N
JAVA- OBJECT ORIENTED

A T I
O V
Objects have properties and behaviours

IN N
T C R
TCR Innovation Core Java
JAVA – OBJECT

IO N
T
ORIENTED

V A
N O
R IN
T C
TCR Innovation Core Java
N
JAVA

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
IO N
T
IN WHICH AREAS IT IS USED?

V A
N O
R IN
T C
TCR Innovation Core Java
O N
ADVANTAGES

A T I
O V
IN N
T CR
TCR Innovation Core Java
O N
ADVANTAGES

A T I
O V
IN N
T CR
TCR Innovation Core Java
O N
Tools You’ll Need

A T I
O V
IN N
T CR
TCR Innovation Core Java
N
JDK Vendors

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
O N
IDE

A T I
O V
INN
T CR
TCR Innovation Core Java
O N
JDK install for Windows

A T I
O V
IN N
Install here:

T C R
Java Development Kit (JDK)
https://www.oracle.com/technetwork/java/javas
e/downloads/index.html

TCR Innovation Core Java


O N
IntelliJ install for Windows

A T I
O V
IN N
T CR
TCR Innovation Core Java
What is JVM?

IO N
V A T
O
JVM (Java Virtual Machine) is an abstract machine that enables your

N
computer to run a Java program.

N
When you run the Java program, Java compiler first compiles your Java

I
code to bytecode. Then, the JVM translates bytecode into native machine

R
code (set of instructions that a computer's CPU executes directly).

T C
Java is a platform-independent language. It's because when you write
Java code, it's ultimately written for JVM but not your physical machine
(computer). Since JVM executes the Java bytecode which is platform-
independent, Java is platform-independent.

TCR Innovation Core Java


O N
What is JVM?

A T I
O V
IN N
T CR
TCR Innovation Core Java
N
What is JRE?

T IO
V A
O
JRE (Java Runtime Environment) is a software

N N
package that provides Java class libraries, Java

I
Virtual Machine (JVM), and other components that

C R
are required to run Java applications.

T
JRE is the superset of JVM.
If you need to run Java programs, but not develop
them, JRE is what you need.
TCR Innovation Core Java
What is JDK?

IO N
V A T
O
JDK (Java Development Kit) is a software development

N N
kit required to develop applications in Java. When you

I
download JDK, JRE is also downloaded with it.

C R
In addition to JRE, JDK also contains a number of

T
development tools (compilers, JavaDoc, Java Debugger,
etc).

TCR Innovation Core Java


O N
Relationship between JVM, JRE,

T I
and JDK

V A
N O
R IN
T C
N
Your first Java program

T IO
V A
N O
class Hello

IN
{

R
public static void main(String[] args)

C
{

T
System.out.println ("Hello World program");
}
}

TCR Innovation Core Java


what above program consists

IO N
of and its keypoints.

V A T
class : class keyword is used to declare classes in Java

O
public : It is an access specifier. Public means this function is visible to

N
all.

N
static : static is again a keyword used to make a function static. To

I
execute a static function you do not have to create an Object of the

R
class. The main() method here is called by JVM, without creating any

C
object for class.

T
void : It is the return type, meaning this function will not return
anything.
main : main() method is the most important method in a Java program.
This is the method which is executed, hence all the logic must be inside
the main() method. If a java class is not having a main() method, it
causes compilation error.
String[] args : This represents an array whose type is String and name
is args. We will discuss more about array in Java Array section.
System.out.println : This is used to print anything on the console like
printf in C language.
TCR Innovation Core Java
N
Variables in Java

T IO
V A
O
Variable is a container which holds a value

N
Variable assigned with a data type

IN
It’s name of a memory location.

R
Three types of variables available

T C
Local, Instance and static

TCR Innovation Core Java


N
To declare the variable in
Java
T IO
V A
N O
datatype variableName;

R IN
T C
datatype refers to type of variable which can any
like: int, float etc. and variableName can be any like:
empId, amount, price etc.

int age=30 ;
double interestRate=8.5d;
TCR Innovation Core Java
N
Naming Convention

T IO
V A
O
No space allowed in variable name

N
Variables must start with either a letter or _ or $ sign.

IN
other special.

R
Variable names cannot start with numbers.

T C
In java everything case sensitive (keywords and names)
age and AGE are two different variables.

TCR Innovation Core Java


O N
Three types of variables available

A T I
O V
IN N
T C R
TCR Innovation Core Java
N
Local variables in Java

T IO
V A
Local variables are declared in method,

N O
constructor or block.

IN
Local variables are initialized when method,

R
constructor or block start and will be destroyed

T C
once its end.
Class not aware of any variable scope inside a
method
A local variable cannot be defines with “static”
keyword.
TCR Innovation Core Java
N
Local variables in Java

T IO
V A
O
float getDiscount(int price)

N N
{

I
float discount;

C R
discount=price*(20/100);

T
return discount;
}

TCR Innovation Core Java


N
Instance variables in Java

T IO
V A
•Instance variables are variables that are declare

O
inside a class but outside any method, constructor or

N N
block.

I
•Instance variable are also variable of object

C R
commonly known as field or property.

T
•They are referred as object variable. Each object
has its own copy of each variable and thus, it doesn't
effect the instance variable if one object changes the
value of the variable.
TCR Innovation Core Java
N
Instance variables in Java

T IO
V A
class Student

O
{

N N
String name;

I
int age;

C R
}

T
TCR Innovation Core Java
O N
Static variables in Java

A T I
Static are class variables declared with static

O V
keyword. Static variables are initialized only once.

N
Create single copy and share with all instances of

IN
class.

C R
class Student

T
{
String name;
int age;
static int instituteCode=1101;
}
TCR Innovation Core Java
O N
Data Types in Java

A T I
V
•Java language has a rich implementation of data types.

O
Data types specify size and the type of values that can be

N
stored in an identifier.

IN
•In java, data types are classified into two categories :

T C R
TCR Innovation Core Java
O N
1. Primitive Data type

A T I
O V
IN N
T CR
TCR Innovation Core Java
O N
These eight primitive type can be put

A T I
into four groups

O V
IN N
T C R
TCR Innovation Core Java
O N
Integer

A T I
O V
INN
T CR
TCR Innovation Core Java
O N
Floating-Point Number

A T I
O V
IN N
T CR
TCR Innovation Core Java
O N
Characters

A T I
V
This group represent char, which represent symbols in

O
a character set, like letters and numbers.

N
char ch = 'S';

R IN
T C
TCR Innovation Core Java
O N
Boolean

A T I
V
•This group represent Boolean, which is a special type for

O
representing true/false values. They are defined constant

N
of the language.

R IN
boolean b=true;

T C
TCR Innovation Core Java
IO N
Java Operators

V A T
O
•Operators in Java can be classified into 5 types:

IN N
1.Arithmetic Operators

R
2.Assignment Operators

C
3.Relational Operators

T
4.Logical Operators
5.Unary Operators
6.Bitwise Operators

TCR Innovation Core Java


IO N
1. Java Arithmetic Operators

V A T
O
•Arithmetic operators are used to perform arithmetic

N
operations on variables and data. For example,

R IN
T C
TCR Innovation Core Java
N
2. Java Assignment Operators

T IO
V A
•Assignment operators are used in Java to assign values

O
to variables.

IN N
T C R
TCR Innovation Core Java
IO N
2. Java Assignment Operators

V A T
O
•Relational operators are used to check the relationship

N
between two operands. ,

R IN
T C
O N
4. Java Logical Operators

A T I
•Logical operators are used to check whether an

O V
expression is true or false. They are used in decision

N
making.

R IN
T C
TCR Innovation Core Java
O N
Increment and Decrement Operators

A T I
•Java also provides increment and decrement operators:

O V
++ and -- respectively. ++ increases the value of the

N
operand by 1, while -- decrease it by 1. For example,

R IN
T C
TCR Innovation Core Java
O N
Java Ternary Operator

A T I
•The ternary operator (conditional operator) is shorthand

O V
for the if-then-else statement.

IN N
variable = Expression ? expression1 : expression2

C R
• If the Expression is true, expression1 is assigned to the

T
variable.
• If the Expression is false, expression2 is assigned to the
variable.

TCR Innovation Core Java


O N
Java Input and Output

A T I
O V
•Java Output

N
System.out.println() : It prints string inside the quotes.

R IN
System.out.print() : It prints string inside the quotes

C
similar like print() method. Then the cursor moves to the

T
beginning of the next line.

TCR Innovation Core Java


O N
Java Input

A T I
O V
•Java provides different ways to get input from the user

N
using the object of Scanner class.

R IN
•In order to use the object of Scanner, we need to

C
import java.util.Scanner package.

T
TCR Innovation Core Java
O N
Java Input

A T I
O V
// create an object of Scanner

N
Scanner input = new Scanner(System.in);

R IN
// take input from the user

C
int number = input.nextInt();

T
TCR Innovation Core Java
N
Java Comments

T IO
A
•comments are a portion of the program that are

V
completely ignored by Java compilers. They are mainly

N O
used to help programmers to understand the code. For

N
example,

I

C R

T
// declare and initialize two variables
int a =1;
int b = 3;

// print the output


System.out.println("This is output");
TCR Innovation Core Java
N
Types of Comments in Java

T IO
V A
O
•In Java, there are two types of comments:

N
1.single-line comment

IN
2.multi-line comment

T C R
TCR Innovation Core Java
N
Single-line Comment

T IO
•A single-line comment starts and ends in the same line.

V A
To write a single-line comment, we can use the // symbol.

O

N
// "Hello, World!" program example

R IN
class Main {

T C
public static void main(String[] args) {
{
// prints "Hello, World!"
System.out.println("Hello, World!");
}
}
TCR Innovation Core Java
N
Multi-line Comment

T IO
•When we want to write comments in multiple lines, we

V A
can use the multi-line comment. To write multi-line

O
comments, we can use the /*....*/ symbol.

N N
/* This is an example of multi-line comment.

I
* The program prints "Hello, World!" to the standard

R
output.

T C
*/
class HelloWorld {
public static void main(String[] args) {
{
System.out.println("Hello, World!");
}
}
TCR Innovation Core Java
N
Type Casting in Java

T IO
A
•Casting is a process of changing one type value to

V
another type. In Java, we can cast one type of value to

O
another type. It is known as type casting.

N

IN
int x = 10;

R
byte y = (byte)x;

T C
TCR Innovation Core Java
N
In Java, type casting is classified into

IO
two types

V A T
N O
R IN
T C
TCR Innovation Core Java
O N
Widening or Automatic type converion

A T I
O V
•Automatic Type casting take place when,

N
•the two types are compatible

IN
•the target type is larger than the source type

T C R
TCR Innovation Core Java
O N
Widening or Automatic type converion

A T I
V
public class Test

O
{
public static void main(String[] args)

N
{

IN
int i = 100;

R
long l = i; //no explicit type casting

C
required

T
float f = l; //no explicit type casting
required
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}

}
TCR Innovation Core Java
O N
Narrowing or Explicit type conversion

A T I
O V
•When you are assigning a larger type value to a

N
variable of smaller type, then you need to perform explicit

IN
type casting. If we don't perform casting then compiler

R
reports compile time error.

T C
TCR Innovation Core Java
Narrowing or Explicit type conversion

IO N
T
public class Test

A
{

V
public static void main(String[] args)

O
{

N
double d = 100.04;

IN
long l = (long)d; //explicit type casting
required

C R
int i = (int)l; //explicit type casting

T
required

System.out.println("Double value "+d);


System.out.println("Long value "+l);
System.out.println("Int value "+i);
}

}
TCR Innovation Core Java
O N
Java Decision Making

A T I
O V
•Java decision-making statements allows you to make

N
decision, based upon the result of a condition.

R IN
•All the programs in Java have set of statements, which

C
are executed sequentially in the order in which they

T
appear. This happens when jumping of statements or
repetition of certain calculations is not necessary.

TCR Innovation Core Java


O N
Java supports following decision-making

T I
statements:

V A
O
•if statement

N
•if-else statement

IN
•else-if statement

R
•Conditional Operator

C
•switch statement

T
TCR Innovation Core Java
O N
if Statements

A T I
O V
•In Java, if statement is used for testing the conditions.

N
The condition matches the statement it returns true else it

IN
returns false. There are four types of If statement they

R
are:

C
•There are four types of if statement in Java:

T
i.if statement
ii.if-else statement
iii.if-else-if ladder
iv.nested if statement

TCR Innovation Core Java


O N
if Statements

A T I
O V
•The if statement is a single conditional based statement

N
that executes only if the provided condition is true.

IN
•If Statement Syntax:

C R
if(condition)

T
{
//code
}

TCR Innovation Core Java


O N
if Statements

A T I
O V
public class Sample{

IN N
public static void main(String args[]){

R
int a=20, b=30;

T C
if(b>a)
System.out.println("b is greater");
}
}

TCR Innovation Core Java


O N
if Statements

A T I
O V
public class IfDemo1 {

N
public static void main(String[] args)

IN
{

R
int marks=70;

C
if(marks > 65)

T
{
System.out.print("First division");
}
}
}
TCR Innovation Core Java
O N
if-else Statement

A T I
O V
•The if-else statement is used for testing condition. If the

N
condition is true, if block executes otherwise else block

IN
executes.

R
•It is useful in the scenario when we want to perform

C
some operation based on the false result.

T
•The else block execute only when condition is false.

TCR Innovation Core Java


O N
if-else Statement

A T I
O V
if(condition)

N
{

IN
//code for true

R
}

C
else

T
{
//code for false
}

TCR Innovation Core Java


N
if-else Statement

T IO
public class Sample {

V A
O
public static void main(String args[]) {

N
int a = 80, b = 30;

IN
if (b > a) {

R
System.out.println("b is greater");

T C
} else {
System.out.println("a is greater");
}
}
}

TCR Innovation Core Java


N
if-else Statement

T IO
public class IfElseDemo1 {

V A
public static void main(String[] args)

O
{

N
int marks=50;

N
if(marks > 65)

I
{

R
System.out.print("First division");

T C
}
else
{
System.out.print("Second division");
}
}
}
TCR Innovation Core Java
N
if-else-if ladder Statement

T IO
V A
•In Java, the if-else-if ladder statement is used for testing

O
conditions. It is used for testing one condition from multiple

N
statements.

IN
•When we have multiple conditions to execute then it is

R
recommend to use if-else-if ladder.

T C
TCR Innovation Core Java
if-else-if ladder Statement

IO N
if(condition1)

T
{

A
//code for if condition1 is true

V
}

O
else if(condition2)

N
{

IN
//code for if condition2 is true
}

C R
else if(condition3)

T
{
//code for if condition3 is true
}
...
else
{
//code for all the false conditions
}
TCR Innovation Core Java
if-else-if ladder Statement

IO N
public class Sample {

A T
public static void main(String args[]) {

V
int a = 30, b = 30;

N O
if (b > a) {

IN
System.out.println("b is greater");
}

C R
else if(a > b){

T
System.out.println("a is greater");
}
else {
System.out.println("Both are equal");
}
}
}

TCR Innovation Core Java


public class IfElseIfDemo1 {

N
public static void main(String[] args) {

O
int marks=75;

T I
if(marks<50){
System.out.println("fail");

A
}

O V
else if(marks>=50 && marks<60){
System.out.println("D grade");

N
}

IN
else if(marks>=60 && marks<70){
System.out.println("C grade");

R
}

T C
else if(marks>=70 && marks<80){
System.out.println("B grade");
}

else if(marks>=80 && marks<90){


System.out.println("A grade");
}
else if(marks>=90 && marks<100){
System.out.println("A+ grade");

}else{
System.out.println("Invalid!");
}} TCR Innovation Core Java
}
N
Nested if statement

T IO
A
•In Java, the Nested if statement is a if inside

V
another if. In this, one if block is created inside

O
another if block when the outer block is true then

N
only the inner block is executed.

IN
if(condition)

R
{

C
//statement

T
if(condition)
{
//statement
}
}

TCR Innovation Core Java


N
Nested if statement

TIO
A
public class NestedIfDemo1 {

V
public static void main(String[] args)

O
{

N
int age=25;

N
int weight=70;

I
if(age>=18)

R
{

C
if(weight>50)

T
{
System.out.println("You are eligible");
}
}
}
}
N
Java switch Statements

T IO
V A
•The Java switch statement executes one statement

O
from multiple conditions. It is like if-else-if ladder

N
statement.

R IN
T C
TCR Innovation Core Java
N
Java switch Statements

T IO
switch(variable)

A
{

V
case 1:

O
//execute your code

N
break;

IN
case n:

R
//execute your code

T C
break;

default:
//execute your code
break;
}

TCR Innovation Core Java


public class Sample {

N
public static void main(String args[]) {
Java switch Statements

IO
int a = 5;

T
switch (a) {

A
case 1:
System.out.println("You chose One");

V
break;

O
case 2:

N
System.out.println("You chose Two");

N
break;

I
case 3:

R
System.out.println("You chose Three");
break;

T C
case 4:
System.out.println("You chose Four");
break;

case 5:
System.out.println("You chose Five");
break;

default:
System.out.println("Invalid Choice. Enter a no
between 1 and 5");
break;
}
} TCR Innovation Core Java
}
N
Java Loops

T IO
A
•Sometimes it is necessary in the program to

O V
execute the statement several times, and Java loops

N
execute a block of commands a specified number of

IN
times

T C R
TCR Innovation Core Java
N
What is Loop?

T IO
A
•A computer is the most suitable machine to perform

O V
repetitive tasks and can tirelessly do a task tens of thousands

N
of times. Every programming language has the feature to

IN
instruct to do such repetitive tasks with the help of certain
form of statements. The process of repeatedly executing a

C R
collection of statement is called looping. The statements gets

T
executed many number of times based on the condition. But if
the condition is given in such a logic that the repetition
continues any number of times with no fixed condition to stop
looping those statements, then this type of looping is called
infinite looping.

TCR Innovation Core Java


N
Java supports following types of loops:

T IO
A
•while loops

O V
•do while loops

N
•for loops

R IN
T C
TCR Innovation Core Java
N
Java while loops

T IO
A
•Java while loops statement allows to repeatedly run the

O V
same block of code, until a condition is met.

IN N
•while loop is most basic loop in Java. It has one control

R
condition, and executes as long the condition is true. The

T C
condition of the loop is tested before the body of the loop
executed, hence it is called an entry-controlled loop.

TCR Innovation Core Java


N
The basic format of while loop statement is:

T IO
A
while (condition)

O V
{

N
statement(s);

IN
Incrementation;

R
}

T C
TCR Innovation Core Java
N
Java do while loops

T IO
A
•Java do while loops is very similar to the while

O V
loops, but it always executes the code block at

N
least once and further more as long as the

IN
condition remains true. This is exit-controlled

R
loop

T C
TCR Innovation Core Java
N
Java do while loops

T IO
A
do

O V
{

N
statement(s);

R IN
}while( condition );

T C
TCR Innovation Core Java
N
Java for loops

T IO
A
•Java for loops is very similar to Java while loops

O V
in that it continues to process a block of code

N
until a statement becomes false, and everything

IN
is defined in a single line.

T C R
TCR Innovation Core Java
N
Java for loops

T IO
A
for ( init; condition; increment )

O V
{

N
statement(s);

IN
}

T C R
TCR Innovation Core Java
N
Java for loops

T IO
A
public class Sample {

O V
N
public static void main(String args[]) {

IN
int n = 1 ;

T C R
for (n = 1; n <= 5; n++) {
System.out.println("Java for loops:" + n);
}
}
}
TCR Innovation Core Java
N
Java Break Statement

T IO
A
•When a break statement is encountered inside

O V
a loop, the loop is immediately terminated and

N
the program control resumes at the next

IN
statement following the loop.

R
•The Java break statement is used to break loop

T C
or switch statement. It breaks the current flow of
the program at specified condition.

TCR Innovation Core Java


Java Break Statement

IO N
T
public class BreakExample {

A
public static void main(String[] args) {

V
//using for loop

N O
for(int i=1;i<=10;i++){

IN
if(i==5){

R
//breaking the loop

T C
break;
}
System.out.println(i);
}
}
}
TCR Innovation Core Java
Java Continue Statement

IO N
T
•The continue statement is used in loop control

V A
structure when you need to jump to the next

O
iteration of the loop immediately. It can be used

N
with for loop or while loop.

IN
•The Java continue statement is used to

C R
continue the loop. It continues the current flow

T
of the program and skips the remaining code at
the specified condition. In case of an inner loop,
it continues the inner loop only.

TCR Innovation Core Java


Java Continue Statement

IO N
T
public class ContinueExample {

V A
public static void main(String[] args) {

O
//for loop

N
for(int i=1;i<=10;i++){

IN
if(i==5){

C R
//using continue statement

T
continue;//it will skip the rest statement
}
System.out.println(i);
}
}
} TCR Innovation Core Java
Method in Java

IO N
T
•A method is a block of code or collection of

V A
statements or a set of code grouped together to

O
perform a certain task or operation. It is used to

N
achieve the reusability of code. We write a

IN
method once and use it many times. We do not

C R
require to write code again and again. It also

T
provides the easy modification and readability of
code, just by adding or removing a chunk of
code. The method is executed only when we call
or invoke it.

TCR Innovation Core Java


Method Declaration

IO N
T
•The method declaration provides information about

V A
method attributes, such as visibility, return-type, name,

O
and arguments. It has six components that are known as

N
method header, as we have shown in the following

IN
figure.

T C R
TCR Innovation Core Java
Method Declaration

IO N
T
•Method Signature: Every method has a method signature. It is a part of

A
the method declaration. It includes the method name and parameter list.

V
•Access Specifier: Access specifier or modifier is the access type of the

O
method. It specifies the visibility of the method. Java provides four types

N
of access specifier:

IN
•Public: The method is accessible by all classes when we use public

R
specifier in our application.

C
•Private: When we use a private access specifier, the method is

T
accessible only in the classes in which it is defined.
•Protected: When we use protected access specifier, the method is
accessible within the same package or subclasses in a different package.
•Default: When we do not use any access specifier in the method
declaration, Java uses default access specifier by default. It is visible only
from the same package only.
TCR Innovation Core Java
Method Declaration

IO N
T
•Return Type: Return type is a data type that the method returns. It may

A
have a primitive data type, object, collection, void, etc. If the method does

V
not return anything, we use void keyword.

O
•Method Name: It is a unique name that is used to define the name of a

N
method. It must be corresponding to the functionality of the method.

IN
Suppose, if we are creating a method for subtraction of two numbers,

R
the method name must be subtraction(). A method is invoked by its

C
name.

T
•Parameter List: It is the list of parameters separated by a comma and
enclosed in the pair of parentheses. It contains the data type and
variable name. If the method has no parameter, left the parentheses
blank.
•Method Body: It is a part of the method declaration. It contains all the
actions to be performed. It is enclosed within the pair of curly braces.
TCR Innovation Core Java
N
Calling a Method in Java

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
Java Method Return Type

IO N
V A T
O
•A Java method may or may not return a value to

N
the function call. We use the return statement to

N
return any value. For example,

R I
T C
TCR Innovation Core Java
Static Method

IO N
A T
•A method that has static keyword is known as static method.

V
In other words, a method that belongs to a class rather than an

O
instance of a class is known as a static method. Create a static

N N
method by using the keyword static before the method name.

I
•The main advantage of a static method is that we can call it

R
without creating an object. It can access static data members

C
and also change the value of it. It is invoked by using the class

T
name. The best example of a static method is the main()
method.

TCR Innovation Core Java


N
Instance Method

T IO
V A
O
•The method of the class is known as an instance method. It is

N
a non-static method defined in the class. Before calling or

IN
invoking the instance method, it is necessary to create an object

R
of its class. Let's see an example of an instance method.

T C
TCR Innovation Core Java
N
Method Overloading

T IO
V A
O
• we have more than one method with the same name i.e. can

N
we define multiple methods with the name add() in the same

IN
class MethodsDemoClass ?

T C R
TCR Innovation Core Java
O N
Java Arrays

A T I
V
An array is a collection of similar types of data.

O
For example, if we want to store the names of 100

N
people then we can create an array of the string type

IN
that can store 100 names.

T C R
TCR Innovation Core Java
O N
How to declare an array in Java?

A T I
O V
dataType[] arrayName;

IN N
R
double[] data;

T C
TCR Innovation Core Java
N
Array

T IO
// declare an array

V A
double[] data;

N O
// allocate memory

N
data = new Double[10] ;

R I
Or

T C
Int [] data = new int[10];

TCR Innovation Core Java


N
How to Initialize Arrays in Java?

T IO
//declare and initialize and array

V A
int[] age = {12, 4, 5, 2, 5};

N O
Or

N
// declare an array

I
int[] age = new int[5];

T C R
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;

TCR Innovation Core Java


N
How to Initialize Arrays in Java?

T IO
V A
O
• Array indices always start from 0. That is, the first element of an

N
array is at index 0.

IN
•If the size of an array is n, then the last element of the array will be at

R
index n-1.

T C
TCR Innovation Core Java
N
How to Access Elements of an Array in Java?

T IO
V A
O
•We can access the element of an array using the index number. Here

N
is the syntax for accessing elements of an array,

IN

R
// access array elements

C
array[index]

T
TCR Innovation Core Java
N
How to Access Elements of an Array in Java?

T IO
Access Array Elements

V A
class Main {

O
public static void main(String[] args) {

N N
// create an array

I
int[] age = {12, 4, 5, 2, 5};

C R
// access each array elements

T
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}
TCR Innovation Core Java
N
Looping Through Array Elements

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Using the for-each Loop

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Multidimensional Arrays

T IO
A
•A multidimensional array is an array containing one or more arrays.

V
•Two-dimensional arrays store the data in rows and columns:

N O
R IN
T C
TCR Innovation Core Java
N
Multidimensional Arrays

T IO
A
•int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };

O V
IN N
T C R
TCR Innovation Core Java
N
Multidimensional Arrays

T IO
A
•int[][] a = new int[3][4];

O V
IN N
T C R
TCR Innovation Core Java
N
Object-oriented programming

T IO
•Reaching this milestone courtesy of its object-oriented nature. Java is

V A
organized in such a way that everything you program in it becomes

O
either a class or an object.

N N
•Object-oriented programming is about creating objects that contain

I
both data and methods.

T C R
TCR Innovation Core Java
N
Advantages of Object-oriented programming

T IO
V A
OOP is faster and easier to execute

O
OOP provides a clear structure for the programs

N
OOP helps to keep the Java code DRY "Don't Repeat Yourself", and

IN
makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less

C R
code and shorter development time

T
TCR Innovation Core Java
N
Advantages of Object-oriented programming

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
3) Inheritance

IO N
V A T
Inheritance is one of the Basic Concepts of OOPs in which one object

N O
acquires the properties and behaviors of the parent object. It’s creating

N
a parent-child relationship between two classes. It offers robust and

I
natural mechanism for organizing and structure of any software.

T C R
TCR Innovation Core Java
N
4) Polymorphism

T IO
V A
•Polymorphism refers to one of the OOPs concepts in Java which is the

N O
ability of a variable, object or function to take on multiple forms. For

N
example, in English, the verb run has a different meaning if you use it

I
with a laptop, a foot race, and business. Here, we understand the

R
meaning of run based on the other words used along with it. The same

T C
also applied to Polymorphism.

TCR Innovation Core Java


N
5) Abstraction

T IO
V A
•Abstraction is one of the OOP Concepts in Java which is an act of

N O
representing essential features without including background details. It

N
is a technique of creating a new data type that is suited for a specific

I
application. Lets understand this one of the OOPs Concepts with

R
example, while driving a car, you do not have to be concerned with its

T C
internal working. Here you just need to concern about parts like
steering wheel, Gears, accelerator, etc.

TCR Innovation Core Java


N
6)Encapsulation

T IO
V A
•Encapsulation is one of the best Java OOPs concepts of wrapping the

N O
data and code. In this OOPs concept, the variables of a class are

N
always hidden from other classes. It can only be accessed using the

I
methods of their current class. For example - in school, a student

R
cannot exist without a class.

T C
TCR Innovation Core Java
N
Java Class

T IO
V A
•A class is a blueprint or template for the object. Before we create an

N O
object, we first need to define the class.

N
•It is a logical entity. It can't be physical.

R I
T C
TCR Innovation Core Java
N
Create a class in Java

T IO
V A
class ClassName {

O
// fields

N
// methods

IN
}

T C R
TCR Innovation Core Java
N
A class in Java can contain:

T IO
V A
•Fields

O
•Methods

N N
•Constructors

I
•Blocks

C R
•Nested class and interface

T
TCR Innovation Core Java
N
Java Class

T IO
V A
class Bicycle {

N O
N
// state or field

I
private int gear = 5;

T C R
// behavior or method
public void braking() {
System.out.println("Working of Braking");
}
}
TCR Innovation Core Java
N
Java Objects

T IO
A
•An object is called an instance of a class. For example,

V
suppose Bicycle is a class then MountainBicycle,

O
SportsBicycle, TouringBicycle, etc can be considered as

N
objects of the class.

R IN
T C
TCR Innovation Core Java
N
Creating an Object in Java

T IO
V A
className object = new className();

N O
// for Bicycle class

IN
Bicycle sportsBicycle = new Bicycle();

C R
Bicycle touringBicycle = new Bicycle();

T
TCR Innovation Core Java
IO N
V A T
N O
R IN
T C
N
Key Differences Between Java Classes and

IO
Objects

V A T
•Class:

O
•A class is a blueprint for creating objects

N
•A class is a logical entity

N
•The keyword used is "class"

I
•A class is designed or declared only once

R
•The computer does not allocate memory when you declare

C
a class

T
•Objects:
•An object is a copy of a class
•An object is a physical entity
•The keyword used is "new"
•You can create any number of objects using one single
class
•The computer allocates memory when you declare a class

TCR Innovation Core Java


N
Access Members of a Class

T IO
V A
O
•// access field and method

N
•sportsBicycle.gear;

N
•sportsBicycle.braking();

R I
T C
TCR Innovation Core Java
N
Constructors in Java

T IO
V A
•A constructor in Java is similar to a method that is invoked

O
when an object of the class is created.

N
•A constructor is a special method that is used to initialize

IN
an object. Every class has a constructor either implicitly or

R
explicitly.

T C
•A constructor has same name as the class name in which
it is declared.
•Constructor must have no explicit return type.
•Constructor in Java can not be abstract, static, final .

TCR Innovation Core Java


N
Constructor Example

T IO
V A
class Car

O
{

N
String name ;

IN
String model;

T C R
Car( )//Constructor
{
name ="";
model="";
}
}
TCR Innovation Core Java
N
Types of Constructor

T IO
•Java Supports two types of constructors:

V A
1.Default Constructor

O
2.Parameterized constructor

IN N
T C R
TCR Innovation Core Java
N
Default Constructor

T IO
V A
In Java, a constructor is said to be default constructor if

O
it does not have any parameter. Default constructor can

N
be either user defined or provided by JVM.

IN
If a class does not contain any constructor then during

R
runtime JVM generates a default constructor which is

T C
known as system define default constructor.
If a class contain a constructor with no parameter then
it is known as default constructor defined by user. In
this case JVM does not create default constructor.

TCR Innovation Core Java


N
Default Constructor

T IO
V A
public class Demo1

O
{

IN N
public static void main (String args[] )

R
{

T C
Demo1 obj = new Demo1 () ;

}
TCR Innovation Core Java
N
User Define Default Constructor

T IO
V A
•Constructor which is defined in the class by the

O
programmer is known as user-defined default constructor.

IN N
T C R
TCR Innovation Core Java
class AddDemo1

N
{

IO
AddDemo1()

T
{

A
int a=10;

V
int b=5;

O
int c;

N
c=a+b;

IN
System.out.println("*****Default Constructor*****");
System.out.println("Total of 10 + 5 = "+c);

R
}

T C
public static void main(String args[])
{
AddDemo1 obj=new AddDemo1();
}
}

TCR Innovation Core Java


IO N
V A T
N O
R IN
T C
TCR Innovation Core Java
N
Static in Java

T IO
V A
The static keyword in Java is used for memory

O
management mainly.

N
Static is a keyword in Java which is used to declare

IN
static stuffs. It can be used to create variable, method

R
block or a class.

T C
Static variable or method can be accessed without
instance of a class because it belongs to class.
Static members are common for all the instances of the
class but non-static members are different for each
instance of the class

TCR Innovation Core Java


N
this keyword

T IO
V A
•In Java, this is a keyword which is used to refer current

N O
object of a class. we can it to refer any member of the class.

N
It means we can access any instance variable and method

I
by using this keyword.

C R
•The main purpose of using this keyword is to solve the

T
confusion when we have same variable name for instance
and local variables.

TCR Innovation Core Java


N
Inheritance in Java

T IO
V A
The process by which one class acquires the

N O
properties(data members) and functionalities(methods)

N
of another class is called inheritance. The aim of

I
inheritance is to provide the reusability of code so that a

C R
class has to write only the unique features and rest of

T
the common properties and functionalities can be
extended from the another class.

TCR Innovation Core Java


N
Child Class:

T IO
V A
The class that extends the features of another class is

N O
known as child class, sub class or derived class.

R IN
T C
TCR Innovation Core Java
N
Parent Class:

T IO
V A
The class whose properties and functionalities are

N O
used(inherited) by another class is known as parent

N
class, super class or Base class.

R I
T C
TCR Innovation Core Java
N
OOP

T IO
Inheritance is a process of defining a new class based

V A
on an existing class by extending its common data

O
members and methods.

N
Inheritance allows us to reuse of code, it improves

IN
reusability in your java application.

R
Note: The biggest advantage of Inheritance is that the

C
code that is already present in base class need not be

T
rewritten in the child class.

This means that the data members(instance variables)


and methods of the parent class can be used in the child
class as.

TCR Innovation Core Java


N
Why use inheritance in java

T IO
For Method Overriding (so runtime polymorphism can

V A
be achieved).

O
For Code Reusability.

IN N
T C R
TCR Innovation Core Java
N
Syntax of Java Inheritance

T IO
V A
class Subclass-name extends Superclass-name

N O
{

N
//methods and fields

I
}

T C R
TCR Innovation Core Java
N
Java Inheritance

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Types of Inheritance in Java

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Single Inheritance

T IO
A
•In single inheritance, one class inherits the properties of

V
another. It enables a derived class to inherit the properties

N O
and behavior from a single parent class. This will, in turn,

N
enable code reusability as well as add new features to the

I
existing code.

T C R
TCR Innovation Core Java
N
Multi-level Inheritance

T IO
A
•When a class is derived from a class which is also derived

V
from another class, i.e. a class having more than one parent

N O
class but at different levels, such type of inheritance is

N
called Multilevel Inheritance.

R I
T C
TCR Innovation Core Java
N
Hierarchical Inheritance

T IO
A
•When a class has more than one child classes (subclasses)

V
or in other words, more than one child classes have the

N O
same parent class, then such kind of inheritance is known

N
as hierarchical.

R I
T C
TCR Innovation Core Java
N
Hybrid Inheritance

T IO
A
•Hybrid inheritance is a combination of two or more types

V
of inheritance.

N O
R IN
T C
TCR Innovation Core Java
N
Super Keyword

T IO
A
•The super keyword in Java is a reference variable which is

V
used to refer immediate parent class object.

N O
•Whenever you create the instance of subclass, an instance

N
of parent class is created implicitly which is referred by

I
super reference variable.

T C R
TCR Innovation Core Java
N
Usage of Java super Keyword

T IO
A
1.super can be used to refer immediate parent class

V
instance variable.

N O
2.super can be used to invoke immediate parent class

N
method.

I
3.super() can be used to invoke immediate parent class

R
constructor.

T C
TCR Innovation Core Java
N
Final Keyword In Java

T IO
A
1.super can be used to refer immediate parent class

V
instance variable.

N O
2.super can be used to invoke immediate parent class

N
method.

I
3.super() can be used to invoke immediate parent class

R
constructor.

T C
TCR Innovation Core Java
N
1) Java final variable

T IO
A
If you make any variable as final, you cannot change the

V
value of final variable(It will be constant)

N O
R IN
T C
TCR Innovation Core Java
N
Java Polymorphism

T IO
A
If one task is performed in different ways, it is known as

V
polymorphism. For example: to convince the customer

N O
differently, to draw something, for example, shape,

IN
triangle, rectangle, etc.

R
In Java, we use method overloading and method

C
overriding to achieve polymorphism.

T
Another example can be to speak something; for
example, a cat speaks meow, dog barks woof, etc.

TCR Innovation Core Java


N
Method Overloading in Java

T IO
A
If a class has multiple methods having same name but different

V
in parameters, it is known as Method Overloading.

O
If we have to perform only one operation, having same name of

N
the methods increases the readability of the program.

IN
Suppose you have to perform addition of the given numbers

R
but there can be any number of arguments, if you write the

C
method such as a(int,int) for two parameters, and b(int,int,int)

T
for three parameters then it may be difficult for you as well as
other programmers to understand the behavior of the method
because its name differs.

TCR Innovation Core Java


N
Method Overloading in Java

T IO
A
Method overloading increases the readability of the program.

V
Different ways to overload the method

O
1. By changing the no. of arguments

N
2. By changing the datatype

R IN
T C
TCR Innovation Core Java
N
1) Method Overloading: changing no. of

IO
arguments

V A T
In this example, we have created two methods, first add() method

O
performs addition of two numbers and second add method

N
performs addition of three numbers.

IN
class Adder{

R
static int add(int a,int b){return a+b;}

T C
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
TCR Innovation Core Java
N
Method Overriding in Java

T IO
V A
•If subclass (child class) has the same method as declared in the

O
parent class, it is known as method overriding in Java.

N
•In other words, If a subclass provides the specific implementation

N
of the method that has been declared by one of its parent class, it

I
is known as method overriding.

C R
•Usage of Java Method Overriding

T
•Method overriding is used to provide the specific implementation
of a method which is already provided by its superclass.
•Method overriding is used for runtime polymorphism

TCR Innovation Core Java


N
Rules for Java Method Overriding

T IO
V A
1. The method must have the same name as in the parent class

O
2. The method must have the same parameter as in the parent

N
class.

R IN
T C
TCR Innovation Core Java
IO N
V A T
N O
R IN
T C
N
Runtime Polymorphism in Java

T IO
V A
1. Runtime polymorphism or Dynamic Method Dispatch is a

O
process in which a call to an overridden method is resolved at

N
runtime rather than compile-time.

N
2. In this process, an overridden method is called through the

I
reference variable of a superclass. The determination of the

C R
method to be called is based on the object being referred to by

T
the reference variable.

TCR Innovation Core Java


N
Abstraction in Java

T IO
V A
1. Abstraction is a process of hiding the implementation details

O
and showing only functionality to the user.

IN N
T C R
TCR Innovation Core Java
N
Abstract class in Java

T IO
V A
1. A class which is declared as abstract is known as an abstract

O
class. It can have abstract and non-abstract methods. It needs

N
to be extended and its method implemented. It cannot be

N
instantiated.

R I
T C
TCR Innovation Core Java
N
Interface

T IO
V A
O
1. An interface in Java is a blueprint of a class. It has static

N
constants and abstract methods.

N
2. The interface in Java is a mechanism to achieve abstraction.

I
There can be only abstract methods in the Java interface, not

C R
method body. It is used to achieve abstraction and multiple

T
inheritance in Java.
3. In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.

TCR Innovation Core Java


N
Why use Java interface?

T IO
V A
O
•There are mainly two reasons to use interface.

N
•It is used to achieve abstraction.

N
•By interface, we can support the functionality of multiple

I
inheritance.

T C R
TCR Innovation Core Java
Difference between abstract class an interface

IO N
V A T
N O
R IN
T C
TCR Innovation Core Java
N
Java Package

T IO
V A
O
A java package is a group of similar types of classes, interfaces

N
and sub-packages.

N
Package in java can be categorized in two form, built-in

I
package and user-defined package.

R
There are many built-in packages such as java, lang, awt,

T C
javax, swing, net, io, util, sql etc.
Here, we will have the detailed learning of creating and using
user-defined packages.

TCR Innovation Core Java


N
Java Package

T IO
V A
O
Advantage of Java Package

N
1) Java package is used to categorize the classes and interfaces so

IN
that they can be easily maintained.

R
2) Java package provides access protection.

T C
TCR Innovation Core Java
Java Package

IO N
V A T
N O
R IN
T C
TCR Innovation Core Java
Java Package

O N
package pack;

T I
public class A {

A
public void msg(){

V
System.out.println("Hello");

O
}

N
}

R IN
C
import pack.*;

T
class B {
public static void main(String args[]) {
A obj = new A();
obj.msg();
}
}
TCR Innovation Core Java
Access Modifiers in Java

IO N
V A T
The access modifiers in Java specifies the accessibility or scope

O
of a field, method, constructor, or class. We can change the

N
access level of fields, constructors, methods, and class by

IN
applying the access modifier on it.

T C R
TCR Innovation Core Java
N
There are four types of Java access modifiers:

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Understanding Java Access Modifiers

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
1) Private

T IO
The private access modifier is accessible only within the class.

V A
E.g

O
Simple example of private access modifier

IN N
T C R
TCR Innovation Core Java
N
2)Default

T IO
V A
1. If you don't use any modifier, it is treated as default by default.

O
The default modifier is accessible only within package. It

N
cannot be accessed from outside the package. It provides more

IN
accessibility than private. But, it is more restrictive than

R
protected, and public.

T C
TCR Innovation Core Java
N
3) Protected

T IO
V A
1. The protected access modifier is accessible within package and

O
outside the package but through inheritance only.

N
2. The protected access modifier can be applied on the data

IN
member, method and constructor. It can't be applied on the

R
class.

C
3. It provides more accessibility than the default modifer.

T
TCR Innovation Core Java
N
4) Public

T IO
V A
1. The public access modifier is accessible everywhere. It has the

O
widest scope among all other modifiers.

IN N
T C R
TCR Innovation Core Java
N
Encapsulation in Java

T IO
V A
1. Encapsulation in Java is a process of wrapping code and data

O
together into a single unit, for example, a capsule which is

N
mixed of several medicines.

IN
2. We can create a fully encapsulated class in Java by making all

R
the data members of the class private. Now we can use setter

C
and getter methods to set and get the data in it.

T
TCR Innovation Core Java
N
Advantage of Encapsulation in Java

T IO
V A
1. By providing only a setter or getter method, you can make the

O
class read-only or write-only. In other words, you can skip the

N
getter or setter methods.

IN
2. It provides you the control over the data. Suppose you want to

R
set the value of id which should be greater than 100 only, you

C
can write the logic inside the setter method. You can write the

T
logic not to store the negative numbers in the setter methods.
3. It is a way to achieve data hiding in Java because other class
will not be able to access the data through the private data
members.

TCR Innovation Core Java


N
String Handling

T IO
V A
1. Strings Class

N O
•A combination of characters is a string.

IN
•String s = new string () ;

T C R
TCR Innovation Core Java
N
String Arrays

T IO
V A
We can also create and use string arrays.

N O
String myarray[] = new String [3] ;

R IN
T C
TCR Innovation Core Java
N
Commonly used string methods

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
IO N
V A T
N O
R IN
T C
TCR Innovation Core Java
N
Java StringBuffer class

T IO
V A
Java StringBuffer class is used to create mutable (modifiable)

O
string. The StringBuffer class in java is same as String class except

N
it is mutable i.e. it can be changed.

R IN
T C
TCR Innovation Core Java
N
Example: difference between String and

IO
StringBuffer

V A T
N O
R IN
T C
TCR Innovation Core Java
N
append()

T IO
V A
This method will concatenate the string representation of any type

O
of data to the end of the StringBuffer object. append() method has

N
several overloaded forms.

R IN
T C
TCR Innovation Core Java
N
insert()

T IO
V A
•This method inserts one string into another. Here are few forms of

O
insert() method.

IN N
T C R
TCR Innovation Core Java
N
reverse()

T IO
V A
•This method reverses the characters within a StringBuffer object.

N O
R IN
T C
TCR Innovation Core Java
N
replace()

T IO
V A
• This method replaces the string from specified start index to the

O
end index.

IN N
T C R
TCR Innovation Core Java
N
capacity()

T IO
V A
•This method returns the current capacity of StringBuffer object.

N O
R IN
T C
TCR Innovation Core Java
N
Exception Handling in Java

T IO
V A
The Exception Handling in Java is one of the powerful

O
mechanism to handle the runtime errors so that normal flow of

N
the application can be maintained.

R IN
When executing Java code, different errors can occur: coding

C
errors made by the programmer, errors due to wrong input, or

T
other unforeseeable things.
When an error occurs, Java will normally stop and generate an
error message. The technical term for this is: Java will throw an
exception (throw an error).

TCR Innovation Core Java


IO N
T
Java try and catch

V A
O
The try statement allows you to define a block of code to be

N
tested for errors while it is being executed.

IN
The catch statement allows you to define a block of code to be

C R
executed, if an error occurs in the try block.

T
The try and catch keywords come in pairs:

TCR Innovation Core Java


N
Try block

T IO
V A
The try block contains set of statements where an exception

O
can occur. A try block is always followed by a catch block,

N
which handles the exception that occurs in associated try block.

IN
A try block must be followed by catch blocks or finally block or

R
both.

T C
try{
//statements that may cause an exception
}

TCR Innovation Core Java


N
Catch block

T IO
V A
A catch block is where you handle the exceptions, this block

O
must follow the try block. A single try block can have several

N
catch blocks associated with it. You can catch different

IN
exceptions in different catch blocks. When an exception occurs

R
in try block, the corresponding catch block that handles that

C
particular exception executes. For example if an arithmetic

T
exception occurs in try block then the statements enclosed in
catch block for arithmetic exception executes.

TCR Innovation Core Java


N
Catch block

T IO
V A
try

O
{

N
//statements that may cause an exception

IN
}

R
catch (exception(type) e(object))

C
{

T
//error handling code
}

TCR Innovation Core Java


N
Java Finally block – Exception handling

T IO
V A
A finally block contains all the statements that must be

O
executed whether exception occurs or not. The statements

N
present in this block will always execute regardless of whether

IN
exception occurs in try block or not such as closing a

R
connection, stream etc.

T C
TCR Innovation Core Java
N
Java Finally block – Exception handling

T IO
V A
try {

O
//Statements that may cause an exception

N
}

IN
catch {

R
//Handling exception

C
}

T
finally {
//Statements to be executed
}

TCR Innovation Core Java


N
How to throw exception in java

T IO
V A
We can define our own set of conditions or rules and throw an

O
exception explicitly using throw keyword. For example, we can

N
throw ArithmeticException when we divide number by 5, or any

IN
other numbers, what we need to do is just set the condition and

R
throw any exception using throw keyword.

T C
TCR Innovation Core Java
N
Java FileOutputStream Class

T IO
V A
Java FileOutputStream is an output stream used for writing

O
data to a File

N
If you have to write primitive values into a file, use

IN
FileOutputStream class.

R
You can write byte-oriented as well as character-oriented data

C
through FileOutputStream class.

T
TCR Innovation Core Java
O N
FileOutputStream class methods

A T I
V
void write(byte[] ary):Description

O
It is used to write ary.length bytes from the byte array to the file

N
output stream.

R IN
void write(int b):It is used to write the specified byte to the file

C
output stream.

T
void close():It is used to closes the file output stream.

TCR Innovation Core Java


O N
Java FileInputStream Class

A T I
•Java FileInputStream class obtains input bytes from a

V
file.

N O
R IN
T C
TCR Innovation Core Java
N
Array

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Multithreading

T IO
A
•Multithreading is a conceptual programming concept whre a program

V
(process) is divided into two or more subprograms (process), which

O
can be implemented at the same time in paraller. A multihreaded

N
program contains two or more parts that can run concurrently. Each

IN
part of such a program is called a thread, and each thread define a
separate path of execution.

T C R
TCR Innovation Core Java
N
MultiThreading

T IO
V A
O
•A Process consist of the memory spac allocated by the operating

N
system that can contain one more threads . A thread canot exist on its

IN
own, it must be part of a process.

T C R
TCR Innovation Core Java
N
Benefits of Multithreading

T IO
V A
O
•Enable progrrammers to do multiple things at one time.

N
•Programmes can divide a long program into threads and execute

IN
them in parallel which eventually increases there speed of the program

R
execution.

C
•Improved performance and concurrency.

T
•Simultaneous access to multiple applications.

TCR Innovation Core Java


N
Life cycle of a Thread (Thread States)

T IO
A
•A thread can be in one of the five states. According to sun, there is

V
only 4 states in thread life cycle in java new, runnable, non-runnable

O
and terminated. There is no running state.

N
•But for better understanding the threads, we are explaining it in the 5

N
states.

I
•The life cycle of the thread in java is controlled by JVM. The java

R
thread states are as follows:

C
1.New

T
2.Runnable
3.Running
4.Non-Runnable (Blocked)
5.Terminated

TCR Innovation Core Java


IO N
V A T
N O
R IN
T C
TCR Innovation Core Java
IO N
1) New

T
The thread is in new state if you create an instance of Thread class but

A
before the invocation of start() method.

V
2) Runnable

O
The thread is in runnable state after invocation of start() method, but

N
the thread scheduler has not selected it to be the running thread.

IN
3) Running

R
The thread is in running state if the thread scheduler has selected it.

C
4) Non-Runnable (Blocked)

T
This is the state when the thread is still alive, but is currently not
eligible to run.
5) Terminated
A thread is in terminated or dead state when its run() method exits.

TCR Innovation Core Java


N
How to create thread

T IO
V A
There are two ways to create a thread:

N O
N
1.By extending Thread class

I
2.By implementing Runnable interface.

T C R
TCR Innovation Core Java
N
Thread class:

T IO
A
Thread class provide constructors and methods to create and perform

V
operations on a thread.Thread class extends Object class and

O
implements Runnable interface.

N
Commonly used Constructors of Thread class:

R IN
Thread()

C
Thread(String name)

T
Thread(Runnable r)
Thread(Runnable r,String name)

TCR Innovation Core Java


N
Java Thread Example by extending Thread class

T IO
A
Class Multi extends Thread{

V
public void run(){

O
System.out.println("thread is running...");

N
}

IN
public static void main(String args[]){

R
Multi t1=new Multi();

C
t1.start();

T
}
}

TCR Innovation Core Java


N
Sleep method in java

T IO
V A
O
The sleep() method of Thread class is used to sleep a thread for the

N
specified amount of time.

N
Syntax of sleep() method in java

R I
C
public static void sleep(long miliseconds)

T
TCR Innovation Core Java
N
Join method in java

T IO
V A
O
The join() method

N
The join() method waits for a thread to die. In other words, it

N
causes the currently running threads to stop executing until the

I
thread it joins with completes its task.

T C R
TCR Innovation Core Java
O N
getName(),setName(String) and getId() method:

A T I
O V
public String getName()

N
public void setName(String name)

N
public long getId()

R I
C
Swing in Java

T
TCR Innovation Core Java
N
The currentThread() method:

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Swing in Java

T IO
V A
O
Java swing consists of a set of components or widgets that are

N
used to develop Graphical User Interface Applications.

N
Java Swing is Lightweight GUI Toolkit that includes Buttons,

I
Textboxes, etc…

C R
Java Swing components are platform-independent. Swing are

T
pluggable look and feel

TCR Innovation Core Java


N
Swing Components in Java

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Swing in Java

T IO
V A
O
Swing components are the basic building blocks of an application.

N
Every application has some basic interactive interface for the user.

N
For example, a button, check-box, radio-button, text-field, etc.

I
These together form the components in Swing.

T C R
TCR Innovation Core Java
N
Introduction to Swing Classes

T IO
JPanel : JPanel is Swing's version of AWT class Panel and uses the

A
same default layout, FlowLayout. JPanel is descended directly from

V
JComponent.

O
JFrame : JFrame is Swing's version of Frame and is descended

N
directly from Frame class. The component which is added to the

IN
Frame, is refered as its Content.

R
JWindow : This is Swing's version of Window and has descended

C
directly from Window class. Like Window it uses BorderLayout by

T
default.
JLabel : JLabel has descended from JComponent, and is used to
create text labels.
JButton : JButton class provides the functioning of push button.
JButton allows an icon, string or both associated with a button.
JTextField : JTextFields allow editing of a single line of text.

TCR Innovation Core Java


N
Creating a JFrame

T IO
V A
There are two ways to create a JFrame Window.

O
1)By instantiating JFrame class.

N N
2)By extending JFrame class.

R I
T C
TCR Innovation Core Java
N
JButton

T IO
V A
JButton class is used to create a push-button on the UI. The

O
button can contain some display text or image. It generates an

N N
event when clicked and double-clicked.

I
JButton okBtn = new JButton(“Ok”);

T C R
TCR Innovation Core Java
N
JButton

T IO
V A
Constructor Description

N O
IN
JButton() It creates a button with no text and icon.

T C R
It creates a button with the specified
JButton(String s)
text.

JButton(Icon i) It creates a button with the specified


icon object.

TCR Innovation Core Java


IO N
V A T
void setText(String s) It is used to set specified text on button

N O
N
String getText() It is used to return the text of the button.

R I
C
void setEnabled(boolean b) It is used to enable or disable the button.

T
void setEnabled(boolean b) It is used to add the action listener to this
object.

TCR Innovation Core Java


N
JLabel

T IO
V A
JLabel class is used to render a read-only text label or

O
images on the UI. It does not generate any event.

N N
JLabel textLbl = new JLabel(“This is a text label.”);

R I
T C
TCR Innovation Core Java
N
JLabel

T IO
A
JLabel() Creates a JLabel instance with no image and with an

V
empty string for the title.

N O
N
JLabel(String s) Creates a JLabel instance with the specified text.

R I
T C
Creates a JLabel instance with the specified image.
JLabel(Icon i)

JLabel(String s, Creates a JLabel instance with the specified text, image,


Icon i, int and horizontal alignment.
horizontalAlign
ment)
TCR Innovation Core Java
N
JLabel

T IO
A
String getText():t returns the text string that a label

O V
displays.

IN N
void setText(String text):It defines the single line of

R
text this component will display.

T C
void setHorizontalAlignment(int alignment):It sets
the alignment of the label's contents along the X
axis.

TCR Innovation Core Java


N
JTextField

T IO
A
JTextField renders an editable single-line text box. A

O V
user can input non-formatted text in the box. To

N
initialize the text field, call its constructor and pass

IN
an optional integer parameter to it. This parameter

R
sets the width of the box measured by the number of

C
columns. It does not limit the number of characters

T
that can be input in the box.

JTextArea txtArea = new JTextArea(“This text is


default text for text area.”, 5, 20);

TCR Innovation Core Java


N
Commonly used Constructors:

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Commonly used Methods:

T IO
V A
Method Description

N O
N
setText(): To set text to TextField

R I
T C
getText(): To get text from TextField

TCR Innovation Core Java


N
JTextField

T IO
A
JTextArea class is a multi line region that displays

O V
text. It allows the editing of multiple line text.

IN N
T C R
TCR Innovation Core Java
IO N
V A T
N O
R IN
T C
TCR Innovation Core Java
N
Commonly used Methods:

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
JPasswordField

T IO
V A
JPasswordField is a subclass of JTextField class. It

O
renders a text-box that masks the user input text

N N
with bullet points. This is used for inserting

I
passwords into the application.

T C R
JPasswordField pwdField = new JPasswordField(15);
var pwdValue = pwdField.getPassword();

TCR Innovation Core Java


N
Commonly used Constructors:

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
JCheckBox

T IO
V A
JCheckBox renders a check-box with a label. The

O
check-box has two states – on/off. When selected,

N N
the state is on and a small tick is displayed in the

I
box.

C R
CheckBox chkBox = new JCheckBox(“Show Help”,

T
true);

TCR Innovation Core Java


N
Commonly used Constructors:

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
JRadioButton

T IO
V A
•JRadioButton is used to render a group of radio

O
buttons in the UI. A user can select one choice from the

N N
group.

R I
ButtonGroup radioGroup = new ButtonGroup();

T C
JRadioButton rb1 = new JRadioButton(“Male”, true);
JRadioButton rb2 = new JRadioButton(“Female”);
radioGroup.add(rb1);
radioGroup.add(rb2);

TCR Innovation Core Java


N
Commonly used Constructors:

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
Commonly used Methods:

T IO
V A
N O
R IN
T C
TCR Innovation Core Java
N
JComboBox

T IO
V A
JComboBox class is used to render a dropdown of

O
the list of options.

IN N
String[] cityStrings = { "Mumbai", "London", "New

R
York", "Sydney", "Tokyo" };

T C
JComboBox cities = new JComboBox(cityList);
cities.setSelectedIndex(3);

TCR Innovation Core Java


O N
Commonly used Constructors:

A T I
O V
IN N
T CR
TCR Innovation Core Java
O N
Commonly used Methods:

A T I
O V
IN N
T CR
TCR Innovation Core Java
O N
JList

A T I
•JList component renders a scrollable list of elements. A

V
user can select a value or multiple values from the list.

O
This select behavior is defined in the code by the

N
developer.

IN

R
DefaultListItem cityList = new DefaultListItem();

C
cityList.addElement(“Mumbai”):

T
cityList.addElement(“London”):
cityList.addElement(“New York”):
cityList.addElement(“Sydney”):
cityList.addElement(“Tokyo”):
JList cities = new JList(cityList);
cities.setSelectionModel(ListSelectionModel.SINGLE_SEL
ECTION);
TCR Innovation Core Java
O N
JoptionPane

A T I
O V
JOptionPane class. It is used for creating dialog boxes

N
for displaying a message, confirm box or input dialog

N
box.

R I
C
1. JOptionPane()

T
2. JOptionPane(Object message)
3. JOptionPane(Object message, intmessageType)

TCR Innovation Core Java


O N
JScrollBar

A T I
O V
In Java, Swing toolkit contains a JScrollBar class. It is

N
used for adding horizontal and vertical scrollbar.

IN
The JScrollBarContains 3 constructors. They are as

C R
following:

T
1. JScrollBar()
2. JScrollBar(int orientation)
3. JScrollBar(int orientation, int value, int extent, int
min_, intmax_)

TCR Innovation Core Java


O N
JMenuBar, JMenu and JMenuItem

A T I
O V
•Swing provides you with very easy-to-use APIs to help

N
create a flexible menu.

R IN
T C
TCR Innovation Core Java
O N
JMenuBar, JMenu and JMenuItem

A T I
O V
N
•public class JMenuBar extends JComponent implements

N
MenuElement, Accessible

R I
C
•public class JMenu extends JMenuItem implements

T
MenuElement, Accessible

•public class JMenuItem extends AbstractButton


implements Accessible, MenuElement

TCR Innovation Core Java


O N
JProgressBar

A T I
O V
•In Java, Swing toolkit contains a JProgressBar Class. It

N
is used for creating a progress bar of a task.

R IN
C
1. JProgressBar()

T
2. JProgressBar(int min, int max)
3. JProgressBar(int orient)
4. JProgressBar(int orient, int min, int max)

TCR Innovation Core Java


IO N
V A T
N O
R IN
T C
TCR Innovation Core Java

You might also like