You are on page 1of 56

JAVA

Basics
• JAVA is a high level programming language
• It was developed by James Gosling. It was initially termed Oak.
• It is platform independent language
• It can be used for developing Object Oriented Programs/Applications.
• Java can be used for Procedure-Oriented Programming as well as
Object Oriented Programming.
• Java syntax is a mix-up of C, C++ Languages.
• Java has several new features which help any new programmers to
develop Real World Applications with ease.
Features
• Simple
• Object-Oriented
• Portable
• Platform-Independent
• Secured
• Multi-Threaded
• Memory Management
• Robust
• Architecture Neutral
• High-level
• Multi-Threading
Variables
• Variables are Named storage spaces to store data and access data
during the program/Application development

• Variables: Syntax:

• Datatype Variable_name = Value_to_be_assigned_to_variable

• In the above, Variable_name is the name given to the memory


location where the value of the variable is stored, to access during the
program/Application development.
• Example:
• int Age = ….. (Here Age is the variable name)
Identifiers
• Identifiers are names with a sequence of Alphabets, Numbers, Dollar
sign, or Underscore characters used to provide a meaningful name to
a variable, in a program.
• Identifiers can start with an Alphabet or Dollar sign, or Underscore
characters, but not with a number or any other special character.

• Syntax:
• Datatype identifier = value 1
Class_name identifier = new Class_name() 2

In the above, 1 is example for primitive data types, 2 is example for


reference datatypes.
Keywords
• Keywords are special words which have a specific meaning and use in
a programming language.
• Most of the programming languages have a set of words which they
label as “keywords” and allow the same to be used in
program/application development.

• Keywords of a programming language cannot be used as a name for a


variable or as a value for a variable during programming.
• JAVA, like other programming languages has its own set of KEYWORDs
• JAVA keywords has new words like “const”, etc apart from the general
keywords found commonly in other programming languages.
Data Types
• There are 8 data types in JAVA.
• They are grouped as follows:
• Numeric
• Decimals
• Floating points
• Char
• Boolean

• Numeric
• Byte
• Short
• Int
• Long

• Float
• Double
Operators
• Arithmetic
• Relational
• Logical
• Ternary
• Bitwise
• Assignment
Operators……
• +, ++
• -, --
• /,*,%
• +=, /=
• -=, %=
• *=
• &,|,&&, ||
•!
• <<, >>
• <=, >=
• ==, !=
• <,>
•=
• ?, :
Statements
• Conditional Statements
• Iteration Statements
• Assignment Statements

ASSIGNMENT STATEMENTS -- Helps to assign a value to a variable of


any data type.

Int a = 100;
String name = “COUNTRY”
Boolean flag = true
Statements…
• CONDITIONAL STATEMENTS:

• Statements which help in finding a result of condition to direct the flow of


program or application accordingly is known as Conditional Statements
OR Control Statements.
• JAVA uses the following CONDITIONAL STATEMENTS

• If..else
• Do… while
• While

• Three above Conditional Statements help to direct the flow of the


programs or applications in JAVA.
Statements…
• If… else

• This is the first of the Conditional Statements in JAVA. There are three
variations of the If.. Else

• If.. Else

• If… Else if.. Else

• Nested if
Statements…
• Do.. While

• This is the second of the Conditional Statements in JAVA. The syntax is:

• Do {
• Statement1
• Statement2
• ..
• ..
•}
• While (conditions)
Statements….
• While….

• This is the last of the conditional statements. The syntax is:

• While (condition)
•{
• Statement1
• Statement2
•…
•…
•}
Statements…
• ITERATION STATEMENTS:

• There are two Iteration Statements provided in JAVA for Iteration


purposes.

• For Statement

• Switch Statement
Statements
• FOR Statement

• Syntax:

• for (I = 0; i<15;i++)
•{
• Statement1
• Statement2
• ….
•…
•}
Statements….
• SWITCH Statement

• Syntax:

• Switch (condition) {

• Case value:
• Statement1

• Case value:
• Statement1
• Statement2

• Default:
• Statement1
}
Arrays
• Arrays are user defined data types.
• Arrays are used to store more than 1 value in a single variable
• Array values are accessed using an Array Index.
• Array Index starts from 0.
• Arrays are treated as Objects in JAVA.
• Arrays are of two types

• Single dimensional
• Multi-dimensional
Arrays…
• Syntax:

• Single dimension Arrays:

• Multi dimension Arrays:


Class
• Class is a blueprint of an object. It is a template for defining the
attributes and functionalities of an object.
• Any object consists of attributes and functionalities which use the
attributes.
• A class basically consists of variables, which are the attributes and
methods which are the behavior/functions of the object.
• A JAVA Class can have:

• Variables
• Constructors
• Methods
• Inner Class/Classes
• Constants/Static variables
Class….
• Syntax:

Class student {
String Name
String Id
int class

Attendance() {


}

Examresults() {


}
}
Objects
• Objects are the physical representation of the Classes.

• All the real-world entities are treated as Objects, since it has been
found that any entity exhibits all/most of the other characteristics
found in most commonly found or existing Objects in the world.

• JAVA supports Object-Oriented-Programming also, to develop


applications for Objects in the real-world.

• Any object in the real-time can be represented using a Class in JAVA.



Objects…

• Objects are created in JAVA using the NEW keyword in the memory.
• For example, for the class Student, which we created previously, we
can create an object as below
• Class_name Reference_type = New Class_name(); --- (1)

• Student Aswath = New Student(); --- (2)

• (1) is the Syntax for the Object creation.


• (2) is the example for Object of Student class.
Functions
• Functions are what is known as methods in many other languages.
• Java follows the terminology without adapting to the new ways as the
important concepts were taken from the C, C++ basically.
• Functions is the place where the logic of the program is used to
perform the process to solve the problem for which the
program/application is designed.
• In java the functions are used for the same purpose as was in previous
languages such as C.
Functions…..
Syntax:

Return-type Function-name(Parameter1, Parameter2…) {


Statement1….
Function body
Statement2…
}
Functions - Types
• Call by Value:
This is the way by which the value of the variable is passed to the
function in which it needs to be used for some process.
The function only uses the variable as a copy and not as a reference.
The value of the variable if changed or updated, doesn’t change in the
statements outside the function where the variable is used to display or
referred.
Functions
• Call by Reference:

• In this type the function doesn’t use a copy of the value of the variable.
• It refers the variable directly and uses the value within the function
statements.
• Any change in the value or updation of values within the function is
reflected in the variable directly and visible outside the function and
the statements following the function-call where the variable is
displayed or referred.
RESPONSIBILITY --EMPTY -- SLIDE
Object Oriented Programming
• Java used all the same OOP principles that were used in its previous
programming language.

• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
OOPs…
• Abstraction
Principles of OOPs
• Abstraction
Principles of OOPs…..
• Encapsulation
Principles of OOPs….
• Inheritance
Principles of OOPs…..
• Polymorphism
Principles of OOPs…
• Polymorphism
Method Overloading
• This is static binding.
Method Overriding
• This is dynamic binding.
Abstract Class
Abstract Class….
• Using abstraction java programs implement only 80% of Abstraction.
Interface
• This concept is used by JAVA to implement 100% abstraction in
programs and applications developed using JAVA.
Interface….
• Syntax:
Interface….
• Syntax:
Interface….
• Example of the syntax:
Multi-threading
What is multi-tasking?
It is performing more than one process simultaneously.
It is used when there is a need to perform two or more processes
and if one process shouldn’t affect the other or all remaining processes
in the program or application.

What is multi-threading?
It is a subset of Multi-threading. It is used not in process. But in
small tasks in a program or application.
Multi-threading…
• Definition:
Multi-Threading…
• Syntax:
Multi-Threading…
• Example:
Multi-Threading…
• Interfaces or classes used for Multi-threading in JAVA.
Multi-Threading….
• Thread class and its methods used in implementing Multi-threading.
Multi-Threading…..
• Interface Runnable and its methods used in Multi-threading.
Multi-Threading….
• Synchronization:
Multi-threading
• Synchronization implementation in JAVA using as a block, method.
Packages….
Packages….
Packages….
Packages….

You might also like