JAVA Programming
JAVA Programming
JAVA Intro
Java is a high-level programming
language originally developed by Sun
Microsystems and released in 1995. Java
runs on a variety of platforms, such as
Windows, Mac OS, and the various
versions of UNIX.
JAVA Comments
Use // for single line comments
Use /* and */ for multiline comments
JAVA Datatypes
Data types are divided into two groups:
Primitive data types - includes byte,
Loop statements
Decision Making
Statements
These are used to control the flow of
Java program execution.
Simple If
If Else
Else If ladder
Nested If
Switch
Simple If
This statement consists of only one block
of statements. SIMPLE IF evaluates a
condition and if the given condition is
true, then the block gets executed.
Otherwise, the block is ignored.
If Else
This statement consists of two blocks of
code. It evaluates a condition and if the
condition is true, then IF BLOCK gets
executed, otherwise ELSE BLOCK gets
executed.
Else If Ladder
Else if ladder consists of multiple IF ELSE
blocks. This helps to verify multiple
conditions and make decision. When any
one condition is true, the rest of the IF
ELSE blocks are ignored.
Nested If
In nested if-statements, the if statement
can contain a if or if-else statement
inside another if or else-if statement.
Switch Statement
The switch
statement allows
us to execute a
block of code
among many
alternatives.
Looping statements
Looping statements are used for
repetition of execution.
They are 4 types:
For loop
While loop
Do while
For each loop
For loop
For loop executes a block of statements
until the given condition is true. When
ever the condition becomes false, then
loop gets terminated and remaining
statements are executed.
While Loop
While loop executes a block of code till
the given condition is true.
Do while
Do while loop executes a block of
statements only once irrespective of the
given condition. It executes a block of
statements first and then verifies the
condition. If the condition is true, the
loop will be repeated otherwise it exits
the loop
For each loop
In Java, the for-each loop is used to
iterate through elements of arrays and
collections (like ArrayList). It is also
known as the enhanced for loop.
Disadvantage:
Size Limit: We can store only the fixed size of
elements in the array. It doesn't grow its size at
runtime. To solve this problem, collection
framework is used in Java which grows
automatically.
Types of Arrays
Single Dimensional Array
Multi Dimensional Array
User-defined Method
Predefined Method
Predefined methods are the method that
is already defined in the Java class
libraries is known as predefined
methods. It is also known as the
standard library method or built-in
method. We can directly use these
methods just by calling them in the
program at any point. Some pre-defined
methods are length(), equals(),
compareTo(), sqrt(), etc.
User-defined Method
The method written by the user or
programmer is known as a user-
defined method. These methods are
modified according to the requirement.
Method Overloading in Java
O/P: HJavalo
Delete()
The delete() method of StringBuilder class
deletes the string from the specified
beginIndex to endIndex.
Ex:
StringBuilder sb =
new StringBuilder("Hello");
sb.delete(1,3);
System.out.println(sb);
O/P: Hlo
reverse()
The reverse() method of StringBuilder
class reverses the current string.
StringBuilder sb =
new StringBuilder("Hello");
sb.reverse();
System.out.println(sb);
capacity()
The capacity() method of StringBuilder class
returns the current capacity of the Builder. The
default capacity of the Builder is 16. If the
number of character increases from its current
capacity, it increases the capacity by
(oldcapacity*2)+2. For example if your current
capacity is 16, it will be (16*2)+2=34.
Ex: StringBuilder sb=new StringBuilder();
System.out.println(sb.capacity());
sb.append("Hello");
System.out.println(sb.capacity
sb.append("Java is my favourite language");
System.out.println(sb.capacity());
Initialization Blocks
Initialization blocks are executed
whenever the class is initialized and
before constructors are invoked. They
are typically placed above the
constructors within braces.
Encapsulation
Encapsulation in Java is a process of
wrapping code and data together into a
single unit, for example, a capsule which
is mixed of several medicines.
To achieve encapsulation in Java −
Declare the variables of a class as
private.
Provide public setter and getter methods
Native Driver,
Thin Driver
Why Should We Use JDBC