Java
Java
What is Java ?
Java
- Java is not just a programming language but it is a complete
platform for object oriented programming.
What is JRE?
JRE
- The JRE is a collection of software that allows a computer
system to run a Java application. The software collection
consists of the Java Virtual Machines (JVMs) that interpret
Java bytecode into machine code, standard class libraries,
user interface toolkits and a verity of utilities
- Java standard class libraries which provide Application
Programming Interface and JVM together form JRE (Java
Runtime Environment).
What is JDK?
JDK
- The JDK is a programming environment for compiling,
debugging and running Java application, Java Beans, and
Java applet. The JDK includes the JRE with addition of the
Java Programming language and additional development
tool
- JDK (Java development kit) provides all the needed
support for software development in Java.
Java Virtual Machine (JVM)
Runs the Byte Code.
Makes Java platform independent.
Handles Memory Management.
How Java works?
Java compilers convert your code from human readable to
something called "bytecode" in the Java world.
Ifelse
Switch Statement
For Loop
While Loop
Do...While Loop
If else Syntax
if ( <condition> )
{
// Execute these statements if <condition> is TRUE
}
else
{
// Execute these statements if < condition> is FALSE
}
switch Syntax
switch (expression)
{
case cond1:
block_1;
break;
case cond2:
block_2;
break;
...
default:
block_default;
}
For Syntax
Sample:
for( int i=0; i<5; i++ )
{
System.out.println(i);
}
For loop - Working
while (condition)
{
statement 1;
statement 2; . .
}
Sample:
int i=0;
while (i<5)
{
System.out.println(i);
i++;
}
While Loop - Working
int i=0;
while(i < 5){
Statement 1;
Statement 2;
i++;
}
Do While Syntax
do
{
statement 1;
statement 2; . .
} while (condition) ;
Sample:
int i=0;
do
{
System.out.println(i);
i++;
} while (i<5);
Do While Loop - Working
int i=0;
do{
Statement 1;
Statement 2;
i++;
} while(i < 5)
Arrays
4
{ 3
array[3]
array[2]
array[0]
1
}
Java Methods and Classes
Java Philosophy on object oriented programming?
Basic element of object-oriented programming in Java includes classes, Objects,
inheritance and interfaces
Class: Abstractions are one of the fundamental ways in which people handle
complexity. A class models an abstraction by defining the properties and behaviors
for the object represented by the abstraction.
Constructor: The main purpose of constructor is to set the initial state of object
when the object is created using the new operator
Inheritance can be defined as the process where one class acquires the
properties(methods and field) of another class. The class which inherits the
properties of other is known as subclass (child, derived class) and class whose
properties are inherited is known as superclass (base, parent class)
extends keyword is used to inherit the properties of a call
Interface: An interface defines a contract by specifying prototype of method and
not their implementation. Interface having incomplete method should not declared
as abstract as interface is by virtue abstract
An Example class
package com. alabs.entity ; // package
...
}
}
Methods
Ex:
public float convert_to _Celsius( float temp) {
return(((temp * 9.0f) / 5.0f) + 32.0f );
}
Methods
A constructor :
is used in the creation of an object.
is a special method with no return type.
must have the same name as the class it is in.
is used to initialize the object.
if not defined will initialize all instance variables to default value.
Constructor
Constructor
public class Class2 { public class Class1 {
com
classes
myproject
com
MyClass.java
myproject
MyClass.class
Inheritance
Inheritance
System.out.println(ob.Type);
System.out.println(ob.Lives);
ob.fun();
}}
Super keyword
package com.alabs.animal;
package com.alabs.animal;
public class Animal {
int fun(int a, int b){
int c = a + b;
return c; package com.alabs.animal;
} public class Aquatic extends Animal{
} int fun(int a, int b){
System.out.println {"um by super class: " +
super.fun(a, b));
int c = a * b;
return c;
}
}
Method Overriding
package com.alabs.animal;
}
}
Abstract Class
Abstract Class
public class Rectangle extends Shape public class Circle extends Shape {
{ @Override
@Override
void Area() { void Area() {
Double area = length * width; Double area = 3.14 * radius*radius;
}} }}
Interface
Interface
Interface Class
abstract Method1(); abstract Method1();
sum = ob.add(10,10);
ob.print(sum);
} ob.print(sum);
}
Input and Output
Input and Output
int read()
String readLine()
StringTokenizer
enum Colors{
Red, Green, Blue, White, Yellow
}
name of enum 0 1 2 3 4(default constants assigned)
Simple program for Enum
enum Colors_enum{red , green , blue , white , yellow}
public class Main {
public static void main(String args[]) {
Colors_enum colors[]=Colors_enum.values();
for(Colors_enum c:colors)
{
System.out.println(c);
}
}
}
How to assign constants to Enum by user
public class Main {
public static void main(String args[]) {
enum Chocolates{ Chocolates favouritechoco=Chocolates.dairymilk;
dairymilk(20) , switch(favouritechoco)
kitkat(10) , {
munch(5); case dairymilk: System.out.println(Dairy Milk);
int cost; break;
Choloclates(int cost) case kitkat: System.out.println(Kitkat);
break;
{ case munch: System.out.println(Munch);
this.cost=cost; break;
} }
} }
}
Array List
ArrayList class
Java.util.ArrayList size: 5
elementData
0 1 2 3 4
// Adding elements
arraylist.add("Rose");
arraylist.add("Lilly");
arraylist.add("Jasmine");
arraylist.add("Rose");
//removes element at index 2
arraylist.remove(2);
How to trace the elements of ArrayList?
Iterator
ListIterator
For-each loop
Enumeration
Iterator
while (iterator.hasNext()) {
Object object = iterator.next();
System.out.print(object + " ");
}
ListIterator
while (listiterator.hasNext()) {
Object object = listiterator.next();
System.out.print(+ object + );
}
For-each loop
Enumeration enumeration =
Collections.enumeration(arraylist);
while (enumeration.hasMoreElements()) {
Object object = enumeration.nextElement();
System.out.print(object + " ");
}
Hash Maps
HashMap Class
The HashMap is a class which is used to perform operations such
as inserting, deleting, and locating elements in a Map .
The Map is an interface maps keys to the elements.
Maps are unsorted and unordered.
Map allows one null key and multiple null values
HashMap < K, V >
Enumeration keys()
Enumeration elements()
Object get(Object keys)
0 Ravi
Rajiv
Megha
Sunny
HashMap ..
..
..
Atif
HashMap - Insertion
// Putting elements
hashmap.put("Ankita", 9634.58);
hashmap.put("Vishal", 1283.48);
hashmap.put("Gurinder", 1478.10);
hashmap.put("Krishna", 199.11);
HashMap - Display
// Get an iterator
Iterator iterator = hashmap.entrySet().iterator();
// Display elements
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
System.out.print(entry.getKey() + ": ");
System.out.println(entry.getValue());
}
Hashtable
Hashtable Class
Enumeration keys()
Enumeration elements()
Object get(Object keys)
System.out.println(key + ":"+value);
}
Exceptions
What is Exception Handling?
Object
1
Throwable Exception Compile enforced
Exception
2
Error Runtime
Exceptions
Types of Exception
Run-time Exceptions.
Compile Enforced Exception
Run-Time Exceptions
try
{
//code where you think exception would occur
}
catch(Exception_Class reference)
{
//Catch the exception and displays that exception
}
Try Catch example
}
}
What is throw keyword?
throw is a keyword which is used to call the sub class of an
exception class.
try{
throw new Exception_class(message);
}
catch(Exception_class reference){
//statements
}
Example using throw keyword
package com.alabs.exception.throwkeyword;
public class Student {
package com.alabs.throwskeyword;
public class GiveInput {
package com.alabs.throwskeyword;
void takeInput() throws IOException public class Main {
{
BufferedReader reader=new public static void main(String[] args) throws
BufferedReader(new IOException {
InputStreamReader(System.in));
GiveInput input=new GiveInput();
System.out.println("Enter your name"); input.takeInput();
String name=reader.readLine();
}
System.out.println("Your name is: "+name); }
}
}
Uses of finally keyword