Prepared By : Uday Shah - (HOD-IT)
I.M.C.A Semester – 2 OOP
using JAVA Unit – 1
UNIT – 1 Introduction To Java
Contents
Compilation and
interpretation:
Java Platform Standard
Edition:
Java Platform Enterprise
Edition:
The requirement for Java Hello
World Example:
Let's create the hello
java program:
Parameters used in First
Java Program
13. Explain Decision Statements in Java:
14. Explain Looping Statements in Java:
15. Explain Jumping Statements in Java:
17. Explain Command Line Argument:
·
Java is a powerful and general-purpose programming language. It
is one of the most widely used high-level programming language in the world.
·
Java
language is used for developing platform-independent software (applications)
that running on desktop computers, mobile devices, and servers.
·
Java
language is developed by James Gosling, who is known as the father of Java.
·
Java was
started as a project called "Oak" by James Gosling at Sun
Microsystems in June 1991, It made the promise of "Write Once, Run
Anywhere", on popular platforms.
·
The
First publication of Java 1.0 was released by Sun Microsystems in 1995.
·
In
2006-2007 Sun released java as open source and platforms independent software.
·
Over
time new enhanced versions of Java have been released. Like Java 1.7, 1.8, 10,
11 etc.
·
Currently,
Java is used in internet programming, mobile devices, games, e-business solutions,
etc.
·
Java is
an object-oriented and platform-independent programming language.
·
Java has
many advanced features, a list of key features is known as Java Buzz Words.
·
Following
are java Buzzwords that represents the Features of Java:
Simple
·
Java
programming language is very simple and easy to learn, understand, and code.
·
Most of
the syntaxes in java follow basic programming language C and object-oriented
programming concepts are similar to C++.
·
In a
java programming language, many complicated features like pointers, operator
overloading, structures, unions, etc. have been removed.
·
One of
the most useful features is the garbage collector it makes java more simple.
Object-oriented:
·
Java is
a purely object-oriented programming language (OOP). This means that Java
program is developed by using classes and objects.
·
In other
words, to write a program in java, we need at least a class or an object.
·
Object-oriented
programming (OOPs) is a methodology that simplifies software development and
maintenance by providing some rules.
·
Basic
concepts of OOPs are Object, Class, Inheritance, Polymorphism, Abstraction and
Encapsulation.
Platform Independent:
·
Java’s
platform independence means that Java programs compiled on one machine or
operating system and can be executed on any other machine or operating system
without modifications. It is also called an Architecture Neutral Language.
·
Java
program is compiled into bytecode. This bytecode is platform independent and
can be run on any machine and this bytecode format also provide security.
Secure:
·
When it
comes to security, Java is always the first choice.
·
Java
enable us to develop virus free, temper free system.
·
Java
program always runs in Java runtime environment (JRE) with so it is more
secure.
Robust:
·
Java is
a robust language that can handle run-time errors.
·
It
checks the code during the compilation and runtime. If any runtime error is
identified by the JVM, it will not be passed directly to the particular system.
·
It will
immediately terminate the program and stop it from causing any harm to the
system.
High Performance:
·
Java is
an interpreted language, so it will never be as fast as a compiled language
like C language.
·
But,
Java enables high performance with the use of (JIT) just-in-time compiler.
·
The
Just-In-Time (JIT) compiler is a component of the runtime environment that
improves the performance of Java programs by compiling bytecodes to
machine-readable code at run time.
·
JIT
compiler only compiles that method which is being called.
Multi-Threading:
·
In
simple words, when multiple tasks are running simultaneously, then it is called
multi-threading.
·
Java
multithreading feature makes it possible to write program that can do many
tasks simultaneously.
·
For
example, In Google Docs where we typing text, the spellings and grammatical
errors are checked and autocorrect task are running.
Portable
·
The WORA
(Write Once Run Anywhere) concept and platform independent feature make Java
portable.
·
Developers
can get the same result on any machine, by writing code only once.
Dynamic flexibility:
·
Java
provide the feature of Dynamic flexibility.
·
Java is
completely object-oriented programing language that provides flexibility to add
classes, new methods to existing classes and even create new classes through
sub-classes.
Compilation and interpretation:
·
Most
languages are designed as either they are compiled language or they are
interpreted language.
·
But java
provides both functionality:
i.
Java
compiler for compiles the source code to bytecode and
ii.
JVM
for executes this bytecode to machine readable code.
·
Java is
a platform independent language it support multi-threaded and Secure
programming.
·
Java
programming language is high level object oriented programming language.
·
Java is
distributed in three different types of editions:
1.
Java
Platform standard edition(SE)
2.
Java
Platform Enterprise edition(EE)
3.
Java
Platform micro edition(ME)
Java Platform Standard Edition:
·
Java
standard Edition is also known as Java J2SE in short term.
·
Java SE
provide basic functionalities such as defining types and objects, SE is a
standard Java specification.
·
Java SE
provides high-level classes used for networking, security, database access, GUI
(Graphical User Interface) development, and XML parsing.
·
It is
mostly used to develop APIs for Desktop Applications like antivirus software,
game, etc.
·
It is
suitable for beginning Java developers.
Java Platform Enterprise Edition:
·
Java
Enterprise Edition is also known as Java J2EE in short term.
·
Java
Enterprise edition platform provide an API and Runtime environment for
developing and running a large scale and secure network application.
·
Java EE
is mainly used for developing web applications.
Java Platform Micro Edition:
·
Java Micro
Edition is also known as Java J2ME in short term.
·
Java ME
facilitates the development of applications for small computing devices such as
embedded systems, sensors, etc.
·
It has
features which make applications portable and which can run on various devices.
It deals with many constraints, such as a small battery, small display, etc.
·
It is
mostly used to develop mobile applications.
·
Java is
high-level Object-Oriented Programming language.
·
Bytecode
is a low-level code that is the result of the compilation of a source code,
which is written, in a high-level language.
·
Java
bytecode is the instruction set for the (JVM) Java Virtual Machine.
·
Bytecode
is also one of the java's magic because this along with java virtual machine (JVM), which makes java platform independent and a secure
language.
·
Bytecode
also makes java secure because it can be run by java virtual machine only.
·
In java
program (.java file) is compiled first using java compiler (javac). After successful compilation, the bytecode (.class file) of
that program is generated.
·
Bytecode
is the code, which is generated after compiling the java program. In java (.class files) are known as byte codes.
·
Following
Diagram shows how the byte code is used :
·
It makes
java portable which helps to achieve "Write
once, run anywhere"
feature. It simply means you need to write the program once, and execute it at
any OS.
·
Generally,
the size of bytecode is less than the source code, therefore it is
easy and fast to transport them over the network/internet.
·
JDK in
Java is stands for Java Development Kit.
·
JDK
provide the collection of tools and different components that are used to
develop and run java programs.
·
It is a
bundle of software development tools and supporting libraries combined with the
Java Runtime Environment (JRE) and Java Virtual Machine (JVM).
·
In Java
JVM stands for Java Virtual Machine.
·
JVM is
the one that actually calls the main method present in a java
code.
·
JVM is
the main component of Java architecture, and it is the part of the JRE (Java Runtime Environment).
·
JVM is
Operating System dependent, So it is responsible for allocating the necessary
memory needed by the Java program and also responsible for
deallocating memory space.
·
following
is a diagram that represent the process of JVM
·
According
to above diagram .java source file convert to .class file using javac compiler.
·
Java
compiler is based on operating system, once it converts .class file then it can
be run anywhere any time in any operating system.
·
And last
class file run with java command to get its output using JVM means Java Virtual
Machine.
·
JRE
stands for Java Run-time Environment.
·
Java
Run-time Environment (JRE) is the part of the Java Development Kit (JDK).
·
Basically
The Java source code gets compiled and converted to Java bytecode. If you wish
to run this bytecode on any platform, you require JRE.
·
Java
Runtime Environment contains core classes, supporting files, and Java Virtual
Machine (JVM).
·
It
provides the runtime environment.
In Java
language JDK tools can be categorized under the different category like:
·
Basic tools
In basic
tool contain tool used to create build application like Java, Javajdb,
appletviewer, JAR etc.
Following
are some basic JDK tools:
·
Java:
It acts
as the deployment launcher in the older SUN java. It loads the class files and
interprets the source code compiled by the javac compiler.
·
Javac
The
javac specifies the java compiler to convert the source code into
bytecode.
·
Javadoc
·
The
Javadoc generates documentation for the comments added in the source code.
·
Jar
·
The jar
helps the archives to manage the jar files in the package library.
·
Appletviewer
·
The
applet viewer is designed to run and debug Java applets without the help of an
internet browser.
Ø RMI tools
§ IMEI tools help to create application that interacts over the web
or the network which consist rmi and rmi registry.
Ø Security tools
§ Security tools help you to set security policy on your system and
create application that can work within the scope of the security policy which
consists of the CI tools Jaya signature policy tools extra.
Ø Java development tools
§ Development tools used for development of the JAVA application and
web application it provides a complete UI for development environment which
consists NetBeans eclipse etc.
Ø Java Web service tools
§ Web service tools provide Java architecture for building web
application and parsing XML data from one site to another site.
Ø Java scripting tools
§ Java scripting tools enable user to run external programming on
the Java Platform it might not be available in the JDK version means it install
separately.
Ø Java troubleshooting tools
§ Troubleshooting can be used for understand a task that may
generate error at runtime as well as in a future.
·
Java is
an Object Oriented and Platform Independent Programming Language.
·
Basically
a "Hello, World!" is a simple program in any programming language
that simply prints Hello, World!
on the
output screen.
The requirement for Java Hello
World Example:
ü For executing any Java program, the following software or
application must be properly installed: -
·
Install
the JDK if you don't have installed it, than first download the JDK and
install it.
·
Set path
of the jdk/bin directory.
·
Create
the Java program.
·
Compile
and run the Java program.
Let's create the hello java program:
class Simple
{
public static void main (String args [])
{
System.out.println("Hello World");
}
}
1. Now Save the above file as Simple.java
2. Compile file with following command: -
>>javac
Simple.java
3. Execute file with following command: -
>>java
Simple
Ø The Output will be:
Hello World
·
When we
compile Java program using javac tool, the Java compiler converts the source
code into byte code.
Parameters used in First Java Program
·
class keyword is used to declare a class in Java.
·
public keyword
is an access modifier that represents visibility. It means it is visible to
all.
·
static is
a keyword. If we declare any method as static, it is known as the static
method. The core advantage of the static method is that there is no need to
create an object to invoke the static method. The main() method is executed by
the JVM, so it doesn't require creating an object to invoke the main() method.
So, it saves memory.
·
void is
the return type of the method. It means it doesn't return any value.
·
main represents
the starting point of the program.
·
String[]
args or String args[] is
used for command line argument.
System.out.println() is used to print statement. Here, System is a
class, out is an object of the PrintStream class and println () is a method of
the PrintStream class.
·
Every
variable has data type.
·
Data
type define that which type of value are stored in variable and it also define
the size of a different data type.
1.
Primitive data types: The primitive data
types include boolean, char, byte, short, int, long, float and double.
2.
Non-primitive data types: The non-primitive
data types include Classes, Interfaces, and Arrays.
Primitive
data types:
·
Primitive data types specify the size and type
of variable values. Following are primitive data types:
1.
Boolean
type – Boolean:
·
A boolean data type can store either True or
False. They can be used to check whether two values are equal or not
(basically in conditional statements to return True or False).
·
Typically, it is used as a flag variable to
track true or false conditions.
·
The default boolean value is
False.
·
The default boolean size is 1 bit.
2.
Character
type – char
·
The char data type stores a single
character.
·
It stores both lower case and upper case
characters which must be enclosed in single quotes.
·
It takes memory space of 16 bits or 2
bytes. The values stored range between 0 to 65536.
3.
Integer type – int
·
An integer type stores an integer number with no
decimal value.
·
There are four integer types in Java – byte,
short, int, and long.
·
The default Integer value is 0.
·
The default Integer size is 4
bytes.
4.
Byte
·
The byte data type is used to save memory in large arrays where the
memory savings is most required.
·
It saves space because a byte is 4 times smaller than an integer.
·
It can also be used in place of "int" data type.
·
It is an 8-bit signed and value-range lies between -128 to 127 . Its
minimum value is -128 and maximum value is 127. Its default value is 0.
5.
Short
·
Short is a 16-bit signed two’s complement integer. It stores whole
numbers with values ranging from -32768 to 32767.
·
Its default value is 0.
·
The short data type can also be used to save memory just like byte data
type.
·
A short data type is 2 times smaller than an integer.
6.
Float
·
It is a floating-point data type that stores the values including their
decimal precision.
·
It is not used for precise data such as currency or research data.
Ø A Float value:
·
is a single-precision 32-bit or 4 bytes IEEE 754
floating-point.
·
can have a 7-digit decimal precision.
·
ends with an ‘f’ or ‘F’.
·
default value = 0.0f.
·
stores fractional numbers ranging from 3.4e-038 to 3.4e+038.
7.
Double
·
The double data type is similar to float.
·
The difference between the two is that is double twice the float in the
case of decimal precision.
·
It is used for decimal values just like float and should not be used for
precise values.
Ø A double value:
·
is a double-precision 64-bit or 8 bytes IEEE 754
floating-point.
·
can have a 15-digit decimal precision.
·
default value = 0.0d.
·
stores fractional numbers ranging from 1.7e-308 to 1.7e+308.
Non-Primitive
Data Types
·
Non-Primitive datatype cannot store the value of
a variable directly in memory.
·
They store a memory address of the variable.
Unlike primitive data types, which are defined by Java, non-primitive data
types are user-defined.
·
They are created by programmers and can be
assigned with null. All non-primitive data types are of equal size.
1. Array
·
An array is used to hold elements of the same
type.
·
It is an object in java, and the array name
(used for declaration) is a reference value that carries the base address of
the continuous location of elements of an array.
2. Class
·
A class is a user-defined data type from which
objects are created.
·
It describes the set of properties or methods
that are common to all objects of the same type.
·
It contains fields and methods that represent
the behaviour of an object.
·
A class gets invoked by the creation of the
respective object.
3. Interface
·
An interface is declared like a class.
·
The key difference is that the interface
contains methods that are abstract by default;
·
It cannot have a method body.
·
The tokens are the small building blocks of a
Java program that are meaningful to the Java compiler.
·
This token defines the structure of Java
language when you submit the Java program to Java compiler.
·
Tokens can be classified as follows:
1. Keyword
2. Literal
3. Identifier
4. Whitespace
5. Separators
6. Comments
7. Operators
Keywords
·
These are the pre-defined reserved
words of any programming language.
·
Each keyword has a special
meaning.
·
It is always written in lower case.
·
Java provides the following keywords:
Literal
·
Constant in Java are call literal entity they do
not change their value in a program during its execution are call literal
integer.
·
Literal in a java roll number there are three
types of integer literal that is a decimal octal and hexadecimal.
·
A number with decimal point and fractional value
are call floating-point literal floating point literal are represented in one
of the two forms that are standard and scientific.
·
Character literal in Java are single character
literal Java support a set of characters’ literal are also known as escape
sequence which cannot be printed like \n \t etc...
·
It sends a sequence of character in a paragraph
or double Kota is a continue as string literal.
Identifier
·
Identifiers are used to name a variable,
constant, function, class, and array.
·
It usually defined by the user.
·
It uses letters, underscores, or a dollar sign
as the first character.
·
Remember that the identifier name must be
different from the reserved keywords. There are some rules to declare
identifiers are:
·
The first letter of an identifier must be a
letter, underscore or a dollar sign. It cannot start with digits but may
contain digits.
·
The whitespace cannot be included in the
identifier.
·
Identifiers are case sensitive.
White space
·
Java is a free from language this means that you
do not need to follow any specially rules for example if you return a Java
Program in a single line not in a structure there after you tell how to execute
and run as a console application in a java whitespaces space tab and newline
Separators
·
The separators in Java is also known
as punctuators.
·
There are nine separators in Java, are as
follows:
·
separator <= ; | , . ( ) { } [ ]
Comments
·
Comments allow us to specify information about the
program inside our Java code.
·
Java compiler cannot read the portion to be commented.
·
The Java compiler treats comments as whitespaces.
·
Java provides the following two types of comments:
·
Line Oriented: It begins with a pair of forwarding
slashes (//).
·
Block-Oriented: It begins with /* and continues until it
founds */.
Operators
·
In programming, operators are the special symbol that tells the compiler
to perform a special operation.
·
Java provides different types of operators that can be classified
according to the functionality they provide.
·
There are eight types of operators in Java, are as follows:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Unary Operators
5. Logical Operators
6. Ternary Operators
7. Bitwise Operators
8. Shift Operators
Operator |
Symbols |
Arithmetic |
+ , - , / , * , % |
Assignment |
= , += , -= , *= , /= , %= , ^= |
Relational |
==, != , < , >, <= , >= |
Unary |
++ , - - , ! |
Logical |
&& , || |
Ternary |
(Condition) ? (Statement1) : (Statement2); |
Bitwise |
& , | , ^ , ~ |
Shift |
<< , >> |
Arithmetic Operators:
•
They are used to perform simple arithmetic
operations on primitive data types. * : Multiplication
•
/ : Division
•
% : Modulo
•
+ : Addition
•
– : Subtraction
Relational Operators:
• These
operators are used to check for relations like equality, greater than, less
than. They return boolean result after the comparison and are extensively used
in looping statements as well as conditional if else statements. General format
is,
• ==,
Equal to : returns true if left hand side is equal to right hand side.
• !=,
Not equalto: returns true if left hand side is not equal to right hand
side.
• <,
less than: returns true if left hand side is less than right hand side.
• <=,
less than or equal to: returns true if left hand side is less than or
equal to right hand side.
• >,
Greater than: returns true if left hand side is greater than right hand
side.
• >=,
Greater than or equal to: returns true if left hand side is greater than
or equal to right hand side.
Logical Operators:
•
These operators are used to perform
“logical AND” and “logical OR” operation, i.e. the function similar to AND gate
and OR gate in digital electronics.
•
One thing to keep in mind is the second
condition is not evaluated if the first one is false, i.e. it has a
short-circuiting effect. Used extensively to test for several conditions for
making a decision.
•
&&,
Logical AND: returns true when both conditions are
true.
•
||,
Logical OR: returns true if at least one condition is
true.
•
!,
Logical NOT: returns true when a condition is
false.
Bitwise Operators:
•
These operators are used to perform
manipulation of individual bits of a number. They can be used with any of the integer
types. They are used when performing update and query operations of Binary
indexed tree.
•
&, bitwise AND operator: returns
bit by bit AND of input values.
•
|, Bitwise OR operator: returns
bit by bit OR of input values.
•
^, Bitwise XOR operator: returns
bit-by-bit XOR of input values.
•
~, Bitwise Complement Operator: This
is a unary operator, which returns the one’s compliment representation of the
input value, i.e. with all bits inversed.
Assignment Operator:
•
‘=’ Assignment
operator is used to assign a value to any variable. It has a right to left
associativity, i.e value given on right hand side of operator is assigned to
the variable on the left and therefore right hand side value must be declared
before using it or should be a constant.
Unary Operators :
•
Unary operators need only one operand.
They are used to increment or decrement a value.
•
(–) :Unary minus,
used for negating the values.
•
(+) : Unary plus,
used for giving positive values. Only used when deliberately converting a
negative value to positive.
•
(++) :
Increment operator, used for incrementing the value by 1.
There are two varieties of increment operator.
i. Post-Increment: Value
is first used for computing the result and then incremented.
ii. Pre-Increment: Value
is incremented first and then result is computed.
•
(- -) :
Decrement operator, used for decrementing the value by 1.
There are two varieties of decrement operator.
i.
Post-Decrement: Value is first used
for computing the result and then decremented.
ii. Pre-Decrement: Value
is decremented first and then result is computed.
Shift Operators:
•
These operators are used to shift the bits
of a number left or right thereby multiplying or dividing the number by two
respectively. They can be used when we have to multiply or divide a number by
two. General format-
•
<<, Left shift operator: shifts
the bits of the number to the left and fills 0 on voids left as a result.
Similar effect as of multiplying the number with some power of two.
•
>>, Signed Right shift
operator: shifts the bits of the number to the right
and fills 0 on voids left as a result. The leftmost bit depends on the sign of
initial number. Similar effect as of dividing the number with some power of
two.
•
The dot (.) operator is
used to select members of a class or object instance:
•
The new operator is used
to create objects out of a class.
• In
Java, type casting is a method or process that converts a data
type into another data type in both ways manually and automatically.
• The
automatic conversion is done by the compiler.
• And
manual conversion performed by the programmer.
• There
are two types of type casting:
1. Widening
Type Casting.
2. Narrowing
Type Casting.
Widening Type
Casting:
• Converting a lower data
type into a higher one is called widening type casting.
• It is also known as implicit
conversion or casting down.
• It is done automatically.
• It is safe because there is
no chance to lose data.
It takes place when:
• Both data types must be
compatible with each other.
• The target type must be
larger than the source type.
•
For Example: -
Ø byte -> short -> char -> int -> long -> float -> double.
Narrowing
Type Casting
• Converting a higher data
type into a lower one is called narrowing type casting.
• It is also known as explicit
conversion or casting up.
• It is done manually by the
programmer.
• If we do not perform
casting, then the compiler reports a compile-time error.
• We can represent flow of
narrowing type casting as follow: -
double -> float -> long -> int -> char -> short -> byte.
• Conditional statements are
used to perform different actions based on various conditions.
• Java supports two types of
selection statements: if and switch.
• These statements allow us
to regulate the flow of our program’s execution.
The if-else
statement:
• The if-else statement is
used to carry out a logical test and then take one of two possible actions
depending on the outcome of the test (i.e. whether the outcome is true or
false).
• The else position of the
if-else statement is optional. Thus, in its simplest general form, the
statement can be written.
if(expression)
{
statement;
}
• The expression must be
placed in parenthesis, as shown. In this form, the statement will be executed
only if the expression has non-zero value (i.e. true).
•
If
the expression has a value of zero (i.e. expression is false), then the
statement will be ignored.
•
The
statement can be either simple or compound. In practice, it is often compound
statement, which may include other control statements.
•
The
general form of an if statement which include the else clause is: -
if
(expression)
{
statement1;
}
else
{
statement2;
}
•
If
the expression has a non-zero value, then statement 1 will be executed.
•
Otherwise,
(i.e. expression is false), statement 2 will be executed.
•
It
is possible to next if…else statement within one-another, just as we did with
loops, there are several different forms that nested if…else two-layer nesting
is:
•
In
this situation, one can complete if… else statement will be executed if
expression is nonzero (true) and another complete if else statement will be
executed if expression is false (zero).
•
It
is of course, possible that statement 1, statement2, statement3 and statement4
will contain another if…else statements. We would then have multi-layer
nesting.
Switch Case
Statement:
• The switch case statement provides and
alternative way to tack decision it is work on user choice.
• It means it provide a way by which user
can tack them on decision.
• It is not a logic but provide powerful structure
to maintain more than one logic in it.
• Switch case statement tack an integer
expiration for execute a specific block.
• In switch case statement each case is
prated label.
• Switch statement have a default option if
none of the expiration match with any case than it executes default case.
•
Following is a syntax of switch case
statement.
Switch
(expression)
{
Case
1:
Statement;
Break;
Case
2:
Statement;
Break;
Default:
Statement;
}
• According to the syntax switch is reserve
keyboard and it contain expression in character or in int type data
• All the case is containing within two
curly bracket and each case has separate label.
• Switch case statement contain break in
every case statement to stop execution of switch case after maintain their
desire case.
• Default case statement does not require
break statement because it always written at last in switch case block.
• In Java, there are three types of loops.
1. for loop
2. while loop
Java for
Loop:
• Java for loop is used to run a
block of code for a certain number of times. The syntax of for loop
is:
• For Loop is the example of Entry
controlled loop.
for
(initialization; condition; increment/decrement) {
// body of
the loop
}
•
The
execution of the for statement is as follows:
1. Initialization of the control variables
is done first, using assignments statement such as i=0 and count=0.
The variables i and count are known as loop controls.
2. The value of the control variables is
tested using the test condition. The test condition is relational expression,
such as i>0 or i<10 that determines when the loop is
terminated and the execution continues with statement that immediately follow
by the loop.
3. When the body of the loop is executed,
the control is transferred back to the for statement either evaluating
the last statement is the loop.
•
Now,
the control variable is incremented using and assignment statement such as i=i+1
and if the new value of the control is satisfied under test condition, then the
body of the loop is executed.
•
This
process continues till the value of the control variable fails to satisfy the
test-condition.
While statement:
•
The
while statement is used to carry out looping operations. The general form of
the statements.
•
While Loop is the
example of Entry controlled loop.
initialization;
while(exp)
{
statement1;
statement2;
increment/decrement;
}
•
The
enclose statements within two braces will be executed repeatedly as long as the
expression evaluated as true.
•
When
the expression is evaluating a false or when condition will be false then it
will come out from the loop and stop the execution of that loop.
•
Initialization
statement initialize some memory variable with some value.
•
Increment
or decrement operator increase or decrease the value of operator is use by
expression.
Java
do...while loop
• The do...while loop is similar
to while loop.
• However, the body
of do...while loop is executed once before the test expression is
checked. For example,
do
{
statement1;
statement2;
increment/decrement;
} while (exp);
•
The
enclosed statement within two braces will be executed repeatedly as the value
of expression is not zero.
•
Notice
that statement will always be executed at least once.
•
Since
the test for repetition does not occur until the end of the first, pass through
the loop.
• Jumping
statements are control statements that transfer execution control from one
point to another point in the program. There are two Jump statements that are
provided in the Java programming language:
1. Break
statement.
2. Continue
statement.
3. Return
Break
statement
•
In
java, the break statement is used to terminate the execution of the nearest
looping statement or switch statement. The break statement is widely used with
the switch statement, for loop, while loop, do-while loop.
Following is a Syntax to declare
break statement:
break;
•
When
a break statement is found inside a loop.
• The break
statement is written simply as break; without any embedded expressions or
statement. For example :
for (i=1; i<=10; i++)
{
if(i==5) {
break;
printf(“\ni=%d”,i);
}
}
•
The output will be 1,2,3,4 and then break will
terminate this loop and stop the execution of the for loop.
Continue
statement
•
The
continue statement is used to skip or to bypass some step or iteration of
looping structure. It does not terminate the loop but just skip or bypass the
particular sequence of the loop structure. It is simply written as continue.
for(i=1; i<=10; i++)
{
if(i==5) {
continue;
printf(“\ni=%d”,i);
}
}
•
The
output of the above program will be 1,2,3,4,6,7,8,9,10. The 5th iteration
of the loop will be skipped as we have defined the continue for that iteration.
So it will not print the value ‘5’.
Return
statement
• In Java programming, the return statement
is used for returning a value when the execution of the block is completed.
• In Java, every method is declared with a
return type such as int, float, double, string, etc.
• These return types required a return
statement at the end of the method. A return keyword is used for returning the
resulted value.
• The void return type does not require any
return statement. If we try to return a value from a void method, the compiler
shows an error.
• The syntax of a return statement is the
return keyword is followed by the value to be returned.
return return_value
According to above, return is keyword and return value is value return by the return keyword.
•
An
array is a fixed size of sequential collection of element of same data type.
•
Array
is also known as collection of same data type element.
•
It
is a simple looping of data type it.
•
It
is used to represent a list of numbers or a list of names.
•
It
is one of the data structure of Java.
•
Individual
values of an array are callan element.
•
Array
is refer to use as a derived data type.
•
Array
is define as a set of homogenous data item.
•
It
provide ability to use a signal name to represent collection of items and refer
to an item by the specific item index.
•
Java
support following different type of array.
1.
One
Dimensional Array
2.
Multi-Dimensional
Array
3.
Jagged
Array
•
Array
is a collection of similar type element that have same name.
One
Dimensional Array:
•
In
a one-dimensionalarray, a list of item can be given a variable name using only
one subscript operator and such a variable is called signal dimensional array.
•
Following
is general syntax of one-dimensional array.
•
int
[]a = new int [10];
•
data_type
[ ] variable_name = new data_type[size] ;
•
According
to above syntax data type may be any valid primitive or non-primitive type
•
Variable
name must be in a valid form and using subscript operator we can define a size
of an array and It must be in whole
number.
•
For
example: if you want to represent a set of five integer number than it can be
declare as int []a = new int[5];
•
One
thing to be noted that the subscript of an array is start always with 0(zero).
•
To
access the element of an array in a program use any valid looping structure.
•
Java
also allow to assign initial value of an element which is declare in an array.
•
Like
int [ ]a=new int[5]{1,2,3,4,5};
•
The
list of initial value must be enclosed within two curly brackets and value are
separated by comma it’s also called compile time initialization.
•
According
to above example array can be initialize at compile time which can be describe
as follow.
•
a[0]
= 1
•
a[1]
= 2
•
a[2]
= 3
•
a[3]
= 4
•
a[4]
= 5
•
According
to above example array can also be define with any other valid data type.
Multi-Dimensional
Array:
•
In
a two dimensional array a list of item can be given a variable name using only
two subscript operator and such a variable is called two dimensional array.
•
Following
is general syntax of two dimensional array.
•
int [ ][ ]a = new int[3][3];
•
Data_type
[ ][ ] variable_name = new data_type[row size][column size];
•
According
to above syntax data type may be any valid primitive or non-primitive type.
•
Variable
name must be in a valid form and using subscript operator we can define a row
and column size of an array and It must be in an whole number.
•
For
example: if you must be in an whole number a set of 3*3 integer number than it
can be declare as
•
int
[ ][ ]a = new int [3][3];
•
One
thing to be noted that the subscript of an array is start with zero.
•
To
access the element of an array in a program use any valid nested looping
structure.
•
Java
also allow to assign initial value of an element which is declare in an array.
•
int int [ ][ ]a = new int [3][3] {{1,2,3},{4,5,6},{7,8,9}};
•
The
list of initial value most be enclose within two curly brackets and value are
separated by comma.
•
It’s
also called compile time initiation.
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
•
According
to above example array can be initialize at compile time which can be describe
as
•
According
to above example array can also be define with float character or any other
valid data type.
Jagged Array:
•
A
jagged array is an array of arrays such that member arrays can be of
different sizes, i.e., we can create a 2-D array but with a variable number of
columns in each row.
•
In
other words, the length of each array index can differ.
•
The
elements of Jagged Array are reference types and initialized to null by
default.
•
Jagged
Array can also be mixed with multidimensional arrays.
•
Here,
the number of rows will be fixed at the declaration time, but you can vary the
number of columns.
•
data_type[][]
name_of_array = new data_type[rows][]
•
According
to above syntax data type may be any valid primitive or non-primitive type.
•
Variable
name must be in a valid form and using subscript operator we can define only
row size because column size may be variable of an array and It must be in an
whole number.
•
For
example: if you must be in an whole number a set of 3*3 integer number than it
can be declare as
•
int
[ ][ ]a = new int [3][];
•
One
thing to be noted that the subscript of an array is start with zero.
•
To
access the element of an array in a program use any valid nested looping structure.
•
Java
also allow assigning initial value of an element, which is declare in an array.
•
int
[ ][ ]a = new int [ ][ ] {{1,2},{3,4,5,6},{7,8,9}};
•
The
list of initial value most be enclose within two curly brackets and value are
separated by comma.
•
•
According
to above example array can be initialize at compile time which can be describe
as
•
According
to above example array can also be define any other valid data type.
•
The
java command-line argument is an argument i.e. passed at the time of running
the java program.
•
The
arguments passed from the console can be received in the java program and it
can be used as an input.
•
So,
it provides a convenient way to check the behavior of the program for the
different values.
•
You
can pass N numbers of arguments from the command prompt
•
When
command line arguments are supplied to JVM, JVM wraps these and supply to
args[].
•
It
can be confirmed that they are actually wrapped up in args array by checking
the length of args using args.length
•
Following
is an example of command line Argument…
class
Cmd
{
public static void main(String args[])
{
System.out.println("Your
first argument is: "+args[0]);
}
}
• OOP stands for Object Oriented Programming
Language.
• Object means a real-world entity such as a pen, chair,
table, computer, watch, etc.
•
Object-Oriented Programming is a methodology or paradigm to design a program
using classes and objects.
• OOP concepts are as follows:
1. Class
2. Object
3. Encapsulation
4. Inheritance
5. Polymorphism
Class:
• Class is a collection of data member and
data function
• Class is a template that define the form
of an object
• Class is an implementation of
encaptulization concept of OOP
• Class is nothing but one kind of structure
of data binding
• Class is user defined data type we can use
class in a main function or in any event using an object
• Object is a variable or instance of the
class
• We can create as many instance of class as
we want
• Usually with the help of class we define a
big project in a small part class make modification and improvements of large
and complex program
• Usually we create data member as a private
and member functions are public but it is not compulsory
• Class contains constructor destructor
nested class methods etc…
Object:
• Object is key to understand object
oriented Technology.
• An object is an entity that has state and
behavior.
• It can be physical or logical tangible or
intangible.
• Following is some characteristics of an
object.
• State : State the present data value of an object.
• Behaviour : Present behaviour or a
functionality of an object such as amount deposit in a bank and withdraw from
the bank.
• Identity : Object identity is typically
implemented using an unique ID the value of the ID is not visible to the
external user heat is used internally by the system to identify each object
uniquely.
• For example Pen is an object its name
is Synosoft and its color is a blue it is known as a state and it is used
to write something so writing is its behaviour object is an instance of a
class.
• Class is a blueprint or template from
which object are created so, the object is an instance of a class.
Encapsulation:
• Encapsulation is an object oriented
programming based concept.
• Encapsulation protect important data
inside the class.
• Which we do not want to be exposed outside
the class.
• Encapsulation process means binding the
Data Member and Member function in single unit.
• Data member means variable property etc...
while the Member function means method and the class is one of the best example
of an encapsulation.
• Encapsulation hide private or
unwanted data from outside the class.
• With the Encapsulation we can make
variable property and method to private.
Inheritance:
• Inheritance is a mechanism in which one
class acquire all the properties and behaviour of another parent class.
• The idea behind Inheritance is just you
create a new class that are built upon an existing class.
• When you inherit from an existing class
you can reuse method and field of parent class you can add new method and field
also.
• Inheritance represent “is a” relationship.
• It is also known as parent child
relationship.
• Inheritance also known as generalization.
• Java use extend keyword to create
inheritance.
• The main advantages of inheritance: -
• Reusability.
• Save time.
• Help in modularization of code.
Polymorphism:
• Polymorphism is a concept by which we can
perform a single action by different ways
• Polymorphism is derived from Greek word
where poly means many and morphism means forms,
• So polymorphism means many forms there are
two types of polymorphism in Java
• Compile time polymorphism
• Runtime polymorphism
• We can perform polymorphism in Java by
method overloading and overriding
• If overload static method in java it is
the example of compile time polymorphism
• While the runtime polymorphism are the
dynamic method, dispatch is a process in which a call to an overloading is
resolved at runtime rather than Compile time.
• In this process overriding method is call
to the reference variable of a superclass
•
The
determination of the method to be call is based on object being referred by the
reference variable
•
Polymorphism
can be static or dynamic
•
In
a static polymorphism response to a function is determined at the compile time
and the dynamic polymorphism is decided at runtime
•
The
static polymorphism linking function with an object during compile time is
called early binding it is call Static Binding
•
In
a dynamic polymorphism, you have to create abstract class that used to provide
partial class implementation of an interface.
•
Implementation
is a completed when derived class inherit from it
Abstract class contain abstract method,
which are implemented by derived class.
Thank
You
::
Any Query::
Contact:
Ruparel Education Pvt. Ltd.
Mobile
No: 7600044051