java programming

What is Queue?

Queues are dynamic collections which have some concept of order. This can be either based on order of entry into the queue - giving us First-In-First-Out (FIFO) or Last-In-First-Out (LIFO) queues. Both of these can be built with linked lists: the simplest "add-to-head" implementation of a linked list gives LIFO behaviour. A minor modification - adding a tail pointer and adjusting the addition method implementation - will produce a FIFO queue.

Queue Implementation

package queue

import java.util.Scanner;

public class Queue {

int[] arr = new int[10];
int a = -1;
int b=0;
public void enqueue(int i){


System.out.println("Queue is full");



public void dequeue(){


for(b=0; b




public void display(){

for(int i=0; i<=9; i++){

System.out.println("values at index "+i+":"+arr[i]);



public static void main(String[] args) {

Scanner sc = new Scanner(;
Queue q = new Queue();
int var = -1;
while( var !=0 ){

System.out.println("1. Insert item in queue");
System.out.println("2. Delete item from queue");
System.out.println("3. Display Queue");
System.out.println("0. Exit");
var = sc.nextInt();

case 1:
System.out.print("Enter item in Queue: ");
int pushItem = sc.nextInt();
case 2:

case 3:


In this tutorial , i will teach you about a database connectivity with mysql database using java programming language.

As java is a object oriented programming language so the best approach to follow is :

1. Make a individual package for a database.

1.1 In this package create a class for database connectivity.

2. Make a individual packages for each of tables in a database.

2.1 In each of package make a bean class of a particular table.

2.2 Make a class for a task which you want to do. (e.g Selection , insertion etc ).

The following code is for a database connectivity with mysql refer to step 1 and 1.1

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class DatabaseConnection {

public static Connection con;

public static void init() throws Exception{
String JDBC_DRIVER   = "com.mysql.jdbc.Driver";
String JDBC_URL      = "jdbc:mysql://localhost:3306/test";
String JDBC_USER     = "root";
String JDBC_PASSWORD = "";

con = (Connection)
System.out.println("Driver Loaded Sucessfully");

}//end init

now i want to fetch record so i follow the step no 2.

for example create class. In a bean class we have only setters and getter of a properties.

package employee;
public class EmployeeBean {
private int emp_id;
private String emp_name;
private String emp_fname;
private String emp_surname;

public int getEmp_id() {
return emp_id;
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
public String getEmp_name() {
return emp_name;
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
public String getEmp_fname() {
return emp_fname;
public void setEmp_fname(String emp_fname) {
this.emp_fname = emp_fname;
public String getEmp_surname() {
return emp_surname;
public void setEmp_surname(String emp_surname) {
this.emp_surname = emp_surname;

public String toString(){
return emp_name;


use following where you want to perform database operations.

class EmpData{

public  Vector<EmployeeBean> getAllEmployees() throws SQLException{
String query = "SELECT emp_id, emp_name, emp_fname, emp_surname FROM employee";

Statement ps = null;
ResultSet rs = null;
Vector<EmployeeBean> vector = new Vector<EmployeeBean>();
ps = (Statement)database.Mysqlstudent.con.createStatement();
rs = ps.executeQuery(query);
EmployeeBean bean = new EmployeeBean();
}//end while loop
if(ps != null) ps.close();
if(rs != null) rs.close();

return vector;

}//end getEmployees


and use it by creating a object of Empdata where we requires it. Like,

Empdata data = new Empdata();

Vector<EmployeeBean> vect = data.getAllEmployees();

so in vect variable you get all record of employees.

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

So we are going to learn that , how we doing serialization in Java.

Lets start...

i m going to create a class person named as Person which implements Serilizable interface.

Serializability of a class is enabled by the class implementing the interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
package serialization;

* @author Maria
public class Person implements Serializable {

private String firstname;
private String lastname;
person(String firstname,String lastname){

this.firstname = firstname;
this.lastname = lastname;


* @return the firstName
public String getFirstname() {
return firstname;

* @param firstname the firstName to set
public void setFirstname(String firstname) {
this.firstname = firstname;

* @return the lastName
public String getLastname() {
return lastname;

* @param lastname the lastName to set
public void setLastname(String lastname) {
this.lastname = lastname;


In above class we have a setters and a getters of a serialiable class.

And now we have a main class in which we create the objects of Person class so we can use our serialized class. 

package serialization;

* @author  Maria

public class Main {

public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException{

FileInputStream fis;
FileOutputStream fos;
ObjectInputStream ois;
ObjectOutputStream oos;

String filename = "p.ser"; // creating a file named as p and .ser is extension for serlizable file
person p = new person("maria","soomro");

fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);


In Above class ,  the main methods throws some exceptions. ObjectOutputStream used for writing  object through writeObject  method.

When i run this main class the p.ser class is created



and for deserialization you have to write following code in calling class like in our example a Main class inside main method.

fis = new FileInputStream(filename);
ois = new ObjectInputStream(fis);
person pe = (person)ois.readObject();

ObjectInputStream used for reading object through readObject method.

Output is :












The new lambda expressions included in Java Platform Standard Edition 8 (Java SE 8).

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection. In addition, new concurrency features improve performance in multicore environments.

The following is a list of hardware and software requirements:

  • Java Development Kit (JDK 8) early access
  • NetBeans 7.4

Lambda Expression Syntax

Lambda expressions address the bulkiness of anonymous inner classes by converting five lines of code into a single statement. This simple horizontal solution solves the "vertical problem" presented by inner classes.

A lambda expression is composed of three parts.

Argument List Arrow Token Body
(int x, int y) -> x + y

The body can be either a single expression or a statement block. In the expression form, the body is simply evaluated and returned. In the block form, the body is evaluated like a method body and a return statement returns control to the caller of the anonymous method. The break and continue keywords are illegal at the top level, but are permitted within loops. If the body produces a result, every control path must return something or throw an exception.


Here is how you write a Runnable using lambdas.

 6 public class RunnableTest {
 7   public static void main(String[] args) {
 9     System.out.println("=== RunnableTest ===");
11     // Anonymous Runnable
12     Runnable r1 = new Runnable(){
14       @Override
15       public void run(){
16         System.out.println("Hello world one!");
17       }
18     };
20     // Lambda Runnable
21     Runnable r2 = () -> System.out.println("Hello world two!");
23     // Run em!
27   }
28 }

In both cases, notice that no parameter is passed and is returned. The Runnable lambda expression, which uses the block format, converts five lines of code into one statement.

Reference Source for this information is



Java8 has been released with a many new features and no doubt these new features are very useful and very interesting to use such as Lambda expressions.

So have a look on java 8's features :

Lambda Expressions 

Lambda Expressions, a new language feature, has been introduced in this release.They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as functional interfaces) more compactly.

Methods References

Method references provide easy-to-read lambda expressions for methods that already have a name.

New Api for Date and Time 

A new set of packages that provide a comprehensive date-time model.

New Stream Api

Improved Type Interface

Method parameter References

Repeating Annotations 

Repeating Annotations provide the ability to apply the same annotation type more than once to the same declaration or type use.

Type Annotations

Type Annotations provide the ability to apply an annotation anywhere a type is used, not just on a declaration. Used with a pluggable type system, this feature enables improved type checking of your code.


Java programming language support these primitive data types:

1. Byte : It is equals to 8 bits

2. Short : (16 bits equals to two bytes)

3. Int : (32 bits equals to 4 bytes)

4. Long : (64 bits equals to 8 bytes)

5. Boolean : Has to possible values either True or False

6. Char :Store a single char

7. Float : (32 bits equals to 4 bytes)

8. Double : (64 bits equals to 8 bytes)

All these data types considered as reserved keywords pre defined by programming language  . Primitive values cannot share its state with other primitive values. primitive data types are not object that why we do not use new keyword. There are default values for above types are:

default values


primitive data types

primitive data types


I am going to start with a basic description for java variables because variables in any language are very useful works as a building block for any program , so lets start...

First of all What is meaning of word variable it means not having a fixed pattern. Its a container hold a value for u ,  When we define a variable with value at beginning of  any program after some code we can change  value of that variable , of course there are lots of other benefits of variables in java we will describe later.

A variable is also called field so do not confuse . In java we can define  variables as :

1. Instance variables

2. Class variables

3. Local variables

1. Instance variables : A variables which also called non static field define with out static keyword and value of instance variable are unique for each instance of a class.

Note : you can not use instance variable in static method till you can not create a object of that class having instance variable.

2. Class variables : A variable with static modifier and value of a static field is same in all over code and we use it by class name do not need to create object.

3. Local variablesA variable only visible to the method where they are declared , no any use of special keyword with local variables.

How we can use these variables:

first we have to define datatype then name of variable end with semicolon like:

int varA ;

now you can assign value to variable here e.g: int varA = 10; or you can assign value where ever you want in code. The advantage of java variable are that we can define and declare a variable any where in the code unlike C.

Working with variables

Working with variables



1 Comment

Object-oriented programming (OOP) is an approach to designing modular, reusable software

systems. Although discussions of object-oriented technology often get mired in the details of one language vs. the other, the real key to the object-oriented approach is that it is a modelling approach first.Although often hyped as a revolutionary way to develop software by zealous proponents, the object-oriented approach is in reality a logical extension of good design practices that go back to the very beginning of computer programming. Object-orientation is simply the logical extension of older techniques such as structured programming and abstract data types. An object is an abstract data type with the addition of polymorphism and inheritance.
Rather than structure programs as code and data, an object-oriented system integrates the two using the concept of an "object". An object has state (data) and behavior (code). Objects correspond to things found in the real world. So for example, a graphics program will have objects such as circle, square, menu. An online shopping system will have objects such as shopping cart, customer,product. The shopping system will support behaviors such as place order, make payment, and offer discount. The objects are designed as class hierarchies. So, for example, with the shopping system there might be high level classes such as electronics product, kitchen product, and book. There may be further refinements for example under electronic products: CD Player, DVD player, etc. These classes and subclasses correspond to sets and subsets in mathematical logic.
The goals of object-oriented programming are:
• Increased understanding.
• Ease of maintenance.
• Ease of evolution.