Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, 17 May 2016

Interface in Java

Interface in Java

Hello friends today we learn one of the most important topic in Java; Interface in Java

Interface in Java


The interface keyword is used to declare an interface. Here is a simple example to declare an interface:

/* File name : NameOfInterface.java */
import java.lang.*;
//Any number of import statements

public interface NameOfInterface
{
//Any number of final, static fields
//Any number of abstract method declarations\
}

Interface is a special class which can be implemented by sub-classes.  (Got implemented by sub-classes, or substantiated).   So, Interface is a special class suitable for generalization of some concreted classes or abstract classes.  It allows subclasses to be implemented with different data definition and method implementation.

Some Important points related to Interface

  • Interface is a special class in Java.
  • Interface has no data.
  • Interface has only abstract methods (no implementation)
  • Interface is more vague than abstract.  Abstract is also vague.  Normal class is concrete.
  • So, interface has no data and no implementation.
  • Then, why we need interface?
  • Interface is like header file in C language.

How Interface similar to class

  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The byte code of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

Sunday, 15 May 2016

Difference Between Java and C++

Difference Between Java and C++

Hello friends today we will discuss about most important interview question; Difference Between Java and C++

Difference Between Java and C++

The main difference between java and C++ is;

  • Java is platform independent but C++ is platform dependent language 
  • Java is used for design web based application but C++ is mainly used for design desktop application.
  • Java uses compiler and interpreter both but in c++ their is only compiler
  • C++ supports operator overloading multiple inheritance but java does not.
  • C++ is more nearer to hardware then Java.
  • Java does not support default arguments but C++ have.
  • C++ have got statement but java have no goto statement.
  • Java does not support unsigned integer.
  • Java supports classes, but does not support structures or unions.
  • Save java program with extension .cpp C++ program save with extension .java
I hope these information is helpful for you.

Wednesday, 6 April 2016

This Keyword in Java

This Keyword in Java

Hello friends Today we will learn about This keyword in Java; this keyword is mainly used to refer to current object.
This Keyword in Java

Some Important points for remember about this keyword


  • this keyword is used to refer to current object.
  • this is always a reference to the object on which method was invoked.
  • this can be used to invoke current class constructor.
  • this can be passed as an argument to another method.

Example of this keyword

class Rectangle
 {
  int length;
  int breadth;
  void setDiamentions(int ln,int br)
  {
  this.length  = ln;
  this.breadth = br;
  }
}
class RectangleDemo
{
  public static void main(String args[])
 {
  Rectangle r1 = new Rectangle();
  r1.setDiamentions(20,10);
  System.out.println("Length of Rectangle : " + r1.length);
  System.out.println("Breadth of Rectangle : " + r1.breadth);
  }
}

Output :

Length  of Rectangle : 20
Breadth of Rectangle : 10

Whenever we define constructor then if the local parameter names and instance variable names are same then local parameters hide the instance variables(members of class)by overriding it, so to resolve this ambiguity "this" keyword is used.

class Demo
{
int a,b;
     Demo(int a,int b)
     {
        this.a=a;
        this.b=b;
       }
}

Monday, 4 April 2016

Features of JSP

Features of JSP

Hello friend now we discuss some important Features of JSP.
JSP is tag based language to develop web application it is internally servlet. Every jsp code first convert into servlet.

features of jsp

JSP is Extension to Servlet, it have all the features of servlet and it have also implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP easy to develop any application.

There are six JSP Actions


  • <jsp:include/>
  • <jsp:forward/>
  • <jsp:plugin/>
  • <jsp:usebean/>
  • <jsp:setProperty/>
  • <jsp:getProperty/> 

Simple JSP Code for web page


<%@ page language = "java" %>
<HTML>
<HEAD><TITLE>JSP Web Page</TITLE></HEAD>
<BODY>
<%
PrintWriter print = request.getWriter();
print.println("Welcome");
%>
</BODY>
</HTML>

Friday, 1 April 2016

Print Pascal Triangle Program in Java

Print Pascal Triangle Program in Java

Hello friends here we will write simple code for Print Pascal Triangle Program in Java. Pascal’s triangle is a set of numbers arranged in the form of a triangle. Each number in a row is the sum of the left number and right number on the above row. If a number is missing in the above row, it is assumed to be 0. The first row starts with number 1. The following is a Pascal triangle with 5 rows,
Pascal Triangle Program in Java

Code

public class PascalTriangleDemo
{
  public static void main(String[] args)
{
     
int n = 10;
int[] row = new int[0];    
 for (int i=0; i<n; i++)
{            
row = pascalRow(row);    
 for (int j=0; j< n - i; j++)
{
System.out.print(" ");
 }    
for (int j=0; j< row.length; j++)
 {
System.out.print(row[j]+" ");
 }
System.out.println();  
}
}
}

Thursday, 31 March 2016

Difference Between Method and Constructor

Difference Between Method and Constructor

The main Difference Between Method and Constructor is;

Difference between method and constructor

Method can be any user-defined name and it should return some value. The constructor must be class name and It should not have any return type (even void) so it not return any value.
Method call by programmer manually but constructor call automatically when the object of the class is created.

Some Important Differences between Method and Constructor are;


  • Constructor are used to initialize the state of object,where as method is expose the behavior of object.
  • Constructor must not have return type where as method must have return type.
  • Constructor name same as the class name where as method may or may not the same class name.
  • Constructor invoke implicitly where as method invoke explicitly.
  • Constructor compiler provide default constructor where as method compiler does't provide.


Difference Between Array and Collection

Difference Between Array and Collection

The main difference between Array and Collection is; We can't increase size of Array but in case of Collection we can decrease or increase size of collection according to our requirement.


difference between array and collection

Below we discuss one by one array and collection features;

Features of Array:


  • Array is fixed in size.
  • Memory point of view arrays is not recommended to use.
  • It shows very good Performance
  • Array Can hold only Homogeneous data elements.
  • Arrays can hold both primitive data types and objects
  • In terms of performance, Arrays are better.

Features of Collections:

  • Collection are Growable in nature.
  • Memory point of view collections are recommended.
  • It shows poor Performance.
  • It Can hold both homogeneous and Heterogeneous .
  • Collections hold only Objects.
  • In terms of performance Collections are slower
I hope all above differences are helpful for you. For more information you can visit our website.

Monday, 28 March 2016

Real life example of Encapsulation

Real life example of Encapsulation

Hello friends here we discuss a little bit about Encapsulation and Real life example of Encapsulation. This is the main feature of any object-oriented programming language.
Encapsulation.

Encapsulation is nothing but wrapping the data and functions into a single entity.

Real life example of Encapsulation

Real-life example of Encapsulation

A simple example of Encapsulation is a mobile phone; No need to know the internal working of the mobile phone to operate it just enough to know about how to call and other features. You have an interface to use the device behavior without knowing implementation details.

Difference between Abstraction and Encapsulation

The main difference between Abstraction and Encapsulation is; Abstraction is logical part like what data you want to show to the customer...  but encapsulation Hides unnecessary data which is not important for the customer

Sunday, 27 March 2016

Real Life Example of Abstraction

Real Life Example of Abstraction


Hello, friend here we discuss Abstraction and Real life example of Abstraction.
Abstraction means providing essential features without showing its inner details or hiding the internal implementation.
Real Life Example of Abstraction

We can enhance the internal implementation without affecting the outside world. Abstraction provides security. A class contains a lot of data and the user does not need the entire data.

Advantage of Using Abstraction

The main advantage of abstraction is that every user will get his own view of the data according to his requirements and will not get confused with unnecessary data.

Example

public abstract class Bank
{
private int accno;
private String name;
void display_to_clerk()
{
System.out.println ("Accno = " + accno);
System.out.println ("Name = " + name);
}
void display_to_manager ()
{
System.out.println ("Accno = " + accno);
System.out.println ("Name = " + name);
System.out.println ("Loan = " + loan);
System.out.println ("Balance = " +balance);
}
}

Real-life Example of Abstraction 

The simple and easy real-life example of Abstraction is a car we know about how to drive a car but we can't know its internal working.

Real Life Example of Polymorphism

Real Life Example of Polymorphism


Hello friends here we learn about Polimorphism in java and also Real Life example of Polymorphism.

Polymorphisms is nothing but ability to take more than one forms(One Interface,Multiple Methods or One Name,Many Forms).

polymorphism:
Poly = many
morphs = forms
existing in many forms.

It is achieved by OVERLOADING and OVERRIDING.

Real Life Example of Polymorphism

Ben (Ben Ten) can transform in different different Aliens, Here one person can transform in more than one form.

Real Life Example of Polymorphism

Another Real Life Example of Polymorphism is;

A software engineer can perform different task at different instance of time depending on the task assign to him
he can done coding , testing , analysis and designing depending on the task assign and the requirement

Example-2: 

Person behaves SON in house at the same time that person behaves EMPLOYEE in office.

Example-3: 

Your mobile phone, one name but many forms
As phone
As camera
As mp3 player
As radio


Wednesday, 16 March 2016

Factorial Program in Java

Factorial Program in Java

The factorial of a number is defined is the product of natural numbers from one to that particular number. Mathematically, Here we write Factorial Program in Java.

Factorial Program in Java


n! = 1 * 2 * 3 * .... * (n-1) * n

For example, the factorial of 4 and 5 is

4! = 4*3*2*1 = 24

5! = 5*4*3*2*1 = 120

Here we write factorial program in java

Code:

public class Factorial
{
public static void main(String[] args) 
{
int n = 5;
int fact = 1;
for (int i = 1; i <= n; i++) 
{
fact = fact * i;
}
System.out.println("Factorial of 5 is: " + fact);
}
}

Output:

Factorial of 5 is : 120

Tuesday, 23 February 2016

Difference Between Array and Collection

Difference Between Array and Collection

The main difference between Difference Between Array and Collection

Difference between Array and Collection
  • Arrays are fixed in size and hence once we created an array we are not allowed to increase or decrease the size based on our requirement. Collections are growable in nature and hence based on our requirement we can increase or decrease the size.
  • Performance point of view arrays are recommended to use.(Faster)
  • Performance point of view collections are not recommended to use.
  • Arrays can hold only homogeneous elements but Collections can hold both homogeneous and heterogeneous elements.
  • Arrays can hold both primitives as well as objects but Collections can hold only objects.
  • Memory point of view arrays are not recommended to use. Memory point of view collections are recommended to use.

Thursday, 18 February 2016

Print star pattern in java | Print star triangle in java

Print star pattern in java

Here i will show you how to print star pattern in java in java programming same concept are apply to write this code only you need to change C or C++ syntax.

Java Program to Print star pattern

Pattern 

            *
          ***
        *****
      *******
     *********
   ***********
  *************
***************

public class StarPattern
{
public static void main(String[] args)
{
for(int i = 10; i > 0; i--)
{
for(int j=1;j<i+1;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Java Program to Print Star Pattern

*
**
***
****
*****
class StarPattern
{
public static void main(String[] args)
{
int j, i;
for (row = 1; j<= 10; j++)
{
fori= 1; i<= j; i++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Tuesday, 16 February 2016

Exception Handling in Java

Exception Handling in Java

The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors.

Exception handling in java

Exception Handling is the mechanism to handle runtime malfunctions. We need to handle such exceptions to prevent abrupt termination of program. The term exception means exceptional condition, it is a problem that may arise during the execution of program.

Exception

An exception (or exceptional event) is a problem that arises during the execution of a program.
When an Exception occurs the normal flow of the program is disrupted and the program or Application terminates abnormally

Some situation when exception have occurred;

  • Invalid input enter by user
  • File that try to open is not found
  • JVM out of memory


Wednesday, 10 February 2016

Features of Java

Features of Java

Hello friends; Here we learn about some basic Features of Java, the main features of java are; it is a more secure and platform-independent programming language.

features of java

Simple

Java is derived from C++ so it is very simple and easy to learn.

Platform Independent

It is platform independent programming language, JVM make java platform independent.

Architectural Neutral

Here architectures means processor, java code run on any processor.

Portable

You can copy java code and run on another system. It easily runs.

Multi-Threading

It supports to run multiple codes at a time.

Distributed

With the help of this language, you can create a distributed applications like ATM.

Secured

It is a more secure programming language; When we compile java code it converts into byte code which is not readable.

High Performance

Compare to another programming language it has very high performance.

Interpreted

It has both compiler and interpreter

Object-Oriented

The most important feature of java language is; It is an object-oriented programming language.

Difference Between Path and Classpath

Difference Between Path and Classpath

Hello friends; Hitesh is here, Today we will learn some basic points related to difference between path and classpath.

Difference Between Path and Classpath

PATH and CLASSPATH are two environment variable which plays important role on Java development environment.

PATH environment variable is used to locate binary or Java commands required to compile and execute Java program like javac ( java compiler) and java command must be in PATH in order to compile any Java file and run any Java program. 

CLASSPATH is used to find .class file which is loaded by Application ClassLoader. There are three Class loaders that are used to locate and load class files e.g. BootStrap class loader, extension class loader and System or application class loader. out of three, the last class loader is responsible for loading classes from CLASSPATH in Java.

Set path and classpath in java

Command to set PATH in Windows

set PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin

Command to set PATH in UNIX/Linux

export PATH = ${PATH}:/opt/Java/JDK1.6.18/bin

Command to set CLASSPATH in windows

set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib

Command to set CLASSPATH in Unix/Linux

export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib

Final keyword java

Final keyword java

Hello friends; Hitesh is here today we will learn some basic concept about Final keyword java.

Final keyword java

The final keyword in java is used to restrict the user to use some features. In java final keyword can be used in many places;
  • A variable level
  • At method-level
  • At class level

Example of Final keyword in java

class FinalDemo
{
final float PI=3.14159;
void show()
 {
  System.out.println("Value of PI: "+PI);
 }
}
 public static void main(String args[])
 {
  FinalDemo obj=new  FinalDemo();
  obj.show();
 }
}

Output:

Value of PI: 3.14159

Tuesday, 9 February 2016

Method Overloading in Java

Method Overloading in Java

Hello friends; Hitesh is here, today I will give a simple example and definition of Method Overloading in Java.

method overloading in java

If a class has multiple methods by the same name but different parameters, it is known as Method Overloading. If two or more method in a class has the same name but different parameters, it is known as method overloading.

Example of Method Overloading in Java

class OverloadingDemo
{
  void sum(int a,int b)
{
System.out.println(a+b);
}
  void sum(int a,int b,int c)
{
System.out.println(a+b+c);
}

  public static void main(String args[]){
  OverloadingDemo obj=new OverloadingDemo();
  obj.sum(20,20,20);
  obj.sum(10,10);

  }
}  

Output:

60
20

Monday, 8 February 2016

Interface in Java

Interface in Java

Java is the most popular programming language and it is the most secure language, Here we learn about one of the most important features of java, Interface in Java.
interface in java
An interface is a reference type in Java and it is similar to a class, Interface is a collection of abstract methods. The main use of the interface in java is to achieve multiple inheritances in java.

When we need Interface

When you want to give clients the flexibility to provide their own implementation of a method, which could be located across various components, interfaces are the better option.

Example of Interface in Java

interface Walking
{
void run();
}

class Man implements Walking
{
public void run()
{
System.out.println("Run Fast");
}

public static void main(String args[])
{
Man obj = new Man();
obj.run();
}
}

Output:

Run Fast 

Sunday, 31 January 2016

Super keyword in java

Super keyword in java

Hello friends, Hitesh is here; Today we will learn some basic point related to Super keyword in java

First we know about definition of super keyword in java

Definition:

Super keyword in java is a reference variable that is used to refer immediate parent class object.

Note: One point always remember about super keyword is; super() is added in each class constructor automatically by compiler and it is must be first statement.
super keyword in java

Uses of Super Keyword

  1. super.<variable_name> refers to the variable of variable of parent class.
  2. super() invokes the constructor of immediate parent class.
  3. super.<method_name> refers to the method of parent class.

Difference between Super and This keyword

this keyword used to store current object reference and super keyword used to store super class reference in subclass object.

For complete tutorial you can visit our own website. Thanks