V1 Java Standard Edition
V1 Java Standard Edition
Content
Features of java
Java Environment and JDK, JRE, JVM.
Java Syntax and class.
Data types , Variables, Methods, access modifiers.
Control statements (if, switch, loops), break, continue
Arrays.
OOP principle
Collections, Generics.
Exceptions handling.
GUI.
Features of Java
Class name
Method
argument
s
if(condition)
statement1; if(condition) {
statement2; Statements to
Here if the condition is execute if condition
true, if block will consider is true
only statement1 to be inside }
its block.
if-else: The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it won’t.
But what if we want to do something else if the condition is false.
Here comes the else statement. We can use the else statement with if
statement to execute a block of code when the condition is false.
Syntax: if (condition) {
Executes this
block if condition
is true
} else {
Executes this block
if condition is
false
}
nested-if: A nested if is an if statement that is the target of another
if or else. Nested if statements means an if statement inside an if
statement. Yes, java allows us to nest if statements within if
statements. i.e, we can place anifif statement inside
(condition1) { another if
statement. Executes when condition1 is
Syntax: true
if (condition2) {
Executes when condition2 is
true
}
}
if-else-if ladder: Here, a user can decide among multiple options.
The if statements are executed from the top down. As soon as one of
the conditions controlling the if is true, the statement associated with
that if is executed, and the rest of the ladder is bypassed. If none of
the conditions is true, then the final else statement will be executed.
if (condition)
statement;
else if (condition)
statement; . .
else
statement;
switch-case The switch statement is a multiway branch statement. It provides an easy way to
dispatch execution to different parts of code based on the value of the expression.
Syntax:
switch (expression) {
Expression can be of type byte, short, int char or an enumeration. case value1:
Beginning with JDK7, expression can also be of type String. statement1;
Dulplicate case values are not allowed. break;
case value2:
The default statement is optional. statement2;
The break statement is used inside the switch to terminate break;
a statement sequence. .
.
The break statement is optional. If omitted, execution will continue case valueN:
on into the next case. statementN;
break;
default:
statementDefault;
}
For loop used to repeat a set of statements until a particular condition is satisfied.
Syntax of for loop.
for(initialization; condition ;
End of for increment/decrement) { statement(s);
loop }
First step: In for loop, initialization happens first and only one time, which means that the
initialization part of for loop only executes once.
Second step: Condition in for loop is evaluated on each iteration, if the condition is true
then the statements inside for loop body gets executed. Once the condition returns false, the
statements in for loop does not execute and the control gets transferred to the next statement in
the program after for loop.
Third step: After every execution of for loop’s body, the increment/decrement part of for
loop executes that updates the loop counter.
While loop In while loop, condition is evaluated first and if it returns true then
the statements inside while loop execute. When condition returns false, the control
comes out of loop and jumps to the next statement after while loop.
while(condition) {
statement(s);
increment/decrement
}
The important point to note when using while loop is that we need to use increment
or decrement statement inside while loop so that the loop variable gets
changed on each iteration, and at some point condition returns false. This way we
can end the execution of while loop otherwise the loop would execute indefinitely
do-while do-while loop is similar to while loop, except one thing difference
between them: In while loop, condition is evaluated before the execution
of loop’s body but in
do-while loop condition is evaluated after the execution of loop’s body.
First, the statements inside loop execute and then the condition gets evaluated, if
the condition returns true then the control gets transferred
do to
{ the “do” else it
jumps to the next statement after do-while. statement(s);
increment/
decrement
}
while(condition);
Continue statement is mostly used inside loops.
encountered inside a loop, control directly jumps to the beginning of the loop for next
iteration, skipping the execution of statements inside loop’s body for the current
iteration.
useful when you want to continue the loop but do not want the rest of the
statements(after continue statement) in loop body to execute for that particular
iteration.
continue;
Output :
0 1 2 3 5 6
Break statement Use break statement to come out of the loop instantly.
Whenever a break statement is encountered inside a loop, the control directly comes
out of loop and the loop gets terminated for rest of the iterations
It is used along with if statement, whenever used inside loop so that the loop gets
terminated for a particular condition.
The important point to note here is that when a break statement is used inside a
nested loop, then only the inner loop gets terminated.
Output :
Value of variable is: 0
Value of variable is: 1
Value of variable is: 2
Out of while-loop
Arrays
An array is a group of like-typed variables that are referred to by a common name.
In Java all arrays are dynamically allocated.
A Java array variable can also be declared like other variables with [] after the data
type.
The variables in the array are ordered and type varName[];
OR
each have an index beginning from 0.
type[] varName;
Java array can be also be used as a static field,
a local variable or a method parameter.
Array can contains primitives data types as well as objects of a class depending on
the definition of array.
int intArray[]; //declaring array
intArray = new int[9]; // allocating memory to array.
int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; // Declaring array literal
Multidimensional Arrays Multidimensional arrays are arrays of arrays with each
element of the array holding the reference of other array
Output :
2 7 9
3 6 1
7 4 2
String Class
string is an object that represents a sequence of characters.
You can create a string object. String s1="Welcom
e";
String s2="Welcom
e";
String s=new String("Welcom
e");
creates two objects and o
ne
reference variable
string objects are immutable. Immutable simply means unmodifiable or
unchangeable.
StringBuffer VS StringBuilder
Those classes similar to String class except that is used to create an mutable
(modifiable) string).
The Java StringBuilder class is same as StringBuffer class except that it is non-
synchronized.
Synchronized means thread safe.
All methods in String class are representing in those classes with different name.
Object Oriented Programming
(OOP).
is a programming paradigm based on the concept of “objects” that
contain data and methods. The primary purpose of object-oriented
programming is to increase the flexibility and maintainability of
programs.
What is an Object the object is an instance of the class, that contain
a data and its behavior(often known as methods), They have states
and behaviors.
Characteristics of Objects:
Abstraction: Abstraction is a process where you show only “relevant”
data and “hide” unnecessary details of an object from the user.
Everything is an object. Think of an object as a fancy variable; it stores data, but you can “make
requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual
component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an
object in your program.
A program is a bunch of objects telling each other what to do by sending messages. To make
a request of an object, you “send a message” to that object. More concretely, you can think of a message as
a request to call a method that belongs to a particular object.
Each object has its own memory made up of other objects. Put another way, you create a new
kind of object by making a package containing existing objects. Thus, you can build complexity into a
program while hiding it behind the simplicity of objects.
Every object has a type. each object is an instance of a class, in which “class” is synonymous with
“type.” The most important distinguishing characteristic of a class is “What messages can you send to it?”
All objects of a particular type can receive the same messages. This is actually a loaded
statement, as you will see later. Because an object of type “circle” is also an object of type “shape,” a circle
is guaranteed to accept shape messages. This means you can write code that talks to shapes and
automatically handle anything that fits the description of a shape.This substitutability is one of the
powerful concepts in OOP.
Light
Light lt = new Light();
on();
lt.on();
off();
the name of the type/class is Light. dim();
the name of this particular Light object is lt. brighten();
the requests that you can make of a Light object are to turn it on, turn it off,
make it brighter, or make it dimmer.
You create a Light object by defining a “reference” (lt) for that object and
calling new to request a new object of that type.
Every class has a Constructor.
Constructor looks like a method but it is in fact not a method.
It’s name is same as class name and it does not return any value public class Light{
Int powerCapacity;
Class can have more than one constructor (Parameterized constructor )
Public Light(){
}
Public Light(int
powerCa){
}
}
Encapsulation
}
Now we can’t
SampleClass sample= new
SampleClass();
Interface
an interface is used for full abstraction
Abstraction is a process where you show only “relevant” data and “hide”
unnecessary details of an object from the user
Interface looks like a class but it is not a class
An interface can have methods and variables just like the class but the methods
declared in interface are by default abstract (only method signature, no body).
the variables declared in an interface are public, static & final by default.
java programming language does not allow you to extend more than one class,
However you can implement more than one interfaces in your class.
class Test implements MyInterface
{
public void method1()
{ System.out.println("implementatio
n of method1"); interface MyInterface {
} public void method1();
public void method2() public void method2();
{ System.out.println("implementatio }
n of method2");
}
Interface in Java 8
Java 8 allows the interfaces to have default and static methods.
The reason we have default methods in interfaces is to allow the developers to add
new methods to the interfaces without affecting the classes that implements these
interfaces.
An interface with only single abstract method is called functional interface.
While creating your own functional interface, mark it with @FunctionalInterface
annotation, this annotation is introduced in Java 8.
@FunctionalInterface
We define it by using new Lambda Expression in java interface
8 MyFunctionalInterface {
public int addMethod(int a, int b);
}
try{ try{
// //
code that may throw exception code that may throw exception
}catch(Exception_class_Name r }finally{
ef){} }
try{
//code that may throw exception
}catch(Exception_class_Name ref){
} catch(Exception_class_Name ref){
} catch(Exception_class_Name ref){
}
throw and throws
Java throw keyword is used to explicitly throw an exception.
Java throws keyword is used to declare an exception.
Throw is followed by an instance (ref object)
Throws is followed by class.
Throw is used within the method.
Throws is used with the method signature. void m()throws ArithmeticException
You cannot throw multiple exceptions. {
//method code
You can declare multiple exceptions }