Friday 29 January 2016

CALL BY VALUE AND CALL BY REFERENCE IN JAVA

There is only call by value in java, not call by reference. If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method.
Example of call by value in java
In case of call by value original value is not changed. Let's take a simple example:
class Operation{  
 int data=50;  
  
 void change(int data){  
 data=data+100;//changes will be in the local variable only  
 }  
     
 public static void main(String args[]){  
   Operation op=new Operation();  
  
   System.out.println("before change "+op.data);  
   op.change(500);  
   System.out.println("after change "+op.data);  
  
 }  
}  

Thursday 28 January 2016

JAVA ARRAY

Normally, array is a collection of similar type of elements that have contiguous memory location.
Java array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array.
Array in java is index based, first element of the array is stored at 0 index.
java array

Advantage of Java Array
  • Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
  • Random access: We can get any data located at any index position.
Disadvantage of Java Array
  • Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at run time. To solve this problem, collection framework is used in java.

Types of Array in java
There are two types of array.
  • Single Dimensional Array
  • Multidimensional Array
Single Dimensional Array in java
Syntax to Declare an Array in java
dataType[] arr; (or)  
dataType []arr; (or)  
dataType arr[]; 

Instantiation of an Array in java

arrayRefVar=new datatype[size]; 

Example of single dimensional java array
Let's see the simple example of java array, where we are going to declare, instantiate, initialize and traverse an array.

class Testarray{  
public static void main(String args[]){  
  
int a[]=new int[5];//declaration and instantiation  
a[0]=10;//initialization  
a[1]=20;  
a[2]=70;  
a[3]=40;  
a[4]=50;  
  

//printing array  
for(int i=0;i<a.length;i++)//length is the property of array  
System.out.println(a[i]);  
  
}}
 

Wrapper class in Java

Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.
Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into primitive automatically. The automatic conversion of primitive into object is known and autoboxing and vice-versa unboxing.

Wednesday 27 January 2016

JAVA STATIC KEYWORD

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
The static can be:
  1. variable (also known as class variable)
  2. method (also known as class method)
  3. block
  4. nested class
1) Java static variable
If you declare any variable as static, it is known static variable
  • The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
  • The static variable gets memory only once in class area at the time of class loading.
Advantage of static variable
It makes your program memory efficient (i.e it saves memory).

Understanding problem without static variable
class Student{  
     int rollno;  
     String name;  
     String college="ITS"
Suppose there are 500 students in my college, now all instance data members will get memory each time when object is created.All student have its unique rollno and name so instance data member is good.Here, college refers to the common property of all objects.If we make it static,this field will get memory only once.

this keyword in java

Usage of java this keyword
Here is given the 6 usage of java this keyword.
  1. this keyword can be used to refer current class instance variable.
  2. this() can be used to invoke current class constructor.
  3. this keyword can be used to invoke current class method(implicitly)
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this keyword can also be used to return the current class instance.
1) The this keyword can be used to refer current class instance variable.
If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.

Understanding the problem without this keyword

class Student10{      int id;      String name;  
    Student10(int id,String name){      id = id;  
     name = name;  
    }  
    void display(){System.out.println(id+" "+name);}    
    public static void main(String args[]){      Student10 s1 = new Student10(111,"Karan");      Student10 s2 = new Student10(321,"Aryan");      s1.display();  
    s2.display();  
    }  
}  
Final Keyword In Java
The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:
variable
method
class
The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.




Monday 25 January 2016

CONSTRUCTOR IN JAVA

Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
Rules for creating java constructor
  1. Constructor name must be same as its class name
  1. Constructor must have no explicit return type
Types of java constructors
  1. Default constructor (no-arg constructor)
  1. Parameterized constructor

java constructor










Java Default Constructor
A constructor that have no parameter is known as default constructor.
Syntax of default constructor:
<class_name>(){}
Example of default constructor
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.
class Bike1{  
Bike1(){System.out.println("Bike is created");}  
public static void main(String args[]){  
Bike1 b=new Bike1();  
}  
}  

Java parameterized constructor

A constructor that have parameters is known as parameterized constructor.
Why use parameterized constructor
Parameterized constructor is used to provide different values to the distinct objects.

Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
class Student4{  
int id;  
String name;  
      
Student4(int i,String n){  
id = i;   
name = n;  
}  
void display(){System.out.println(id+" "+name);}  
   
public static void main(String args[]){  
Student4 s1 = new Student4(111,"Karan");  
Student4 s2 = new Student4(222,"Aryan");  

s1.display();  
 s2.display(); 
}
}
There are basically two rules defined for the constructor.

There are two types of constructors: 

Friday 22 January 2016

METHOD OVERLOADING IN JAVA

If a class have multiple methods by same name but different parameters, it is known as Method Overloading.
If we have to perform only one operation, having same name of the methods increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behaviour of the method because its name differs. So, we perform method overloading to figure out the program quickly.

Advantage of method overloading?

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java
  1. By changing number of arguments
  2. By changing the data type
m method performs addition of three numbers.
class Calculation{  
  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[]){  
  Calculation obj=new Calculation();  
  obj.sum(10,10,10);  
  obj.sum(20,20);  
  
  }  
  }

2)Example of Method Overloading by changing data type of argument

In this example, we have created two overloaded methods that differs in data type. The first sum method receives two integer arguments and second sum method receives two double arguments.
class Calculation2{  
  void sum(int a,int b){System.out.println(a+b);}  
  void sum(double a,double b){System.out.println(a+b);}  
  
  public static void main(String args[]){  
  Calculation2 obj=new Calculation2();  
  obj.sum(10.5,10.5);  
  obj.sum(20,20);  
  }
  }

Thursday 21 January 2016

OBJECTS AND CLASSES IN JAVA

In object-oriented programming technique, we design a program using objects and classes.
Object is the physical as well as logical entity whereas class is the logical entity only.

Object in Java

An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tengible and intengible). The example of integible object is banking system.
An object has three characteristics:
  • state: represents data (value) of an object.
  • behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc.
  • identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.
For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior.
Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.

Class in Java
A class is a group of objects that has common properties. It is a template or blueprint from which objects are created.
A class in java can contain:
  • data member
  • method
  • constructor
  • block
  • class and interface

Syntax to declare a class:

class <class_name>{  
data member;  
method;  
}  

Simple Example of Object and Class

In this example, we have created a Student class that have two data members id and name. We are creating the object of the Student class by new keyword and printing the objects value.
class Student1{  
int id;//data member (also instance variable)  
String name;//data member(also instance variable)  
public static void main(String args[]){  
 Student1 s1=new Student1();//creating an object of Student  
 System.out.println(s1.id);  
 System.out.println(s1.name);  
}  


Instance variable in Java

A variable that is created inside the class but outside the method, is known as instance variable.Instance variable doesn't get memory at compile time.It gets memory at runtime when object(instance) is created.That is why, it is known as instance variable.

Method in Java

In java, a method is like function i.e. used to expose behaviour of an object.

Advantage of Method


  • Code Reusability
  • Code Optimization

new keyword
The new keyword is used to allocate memory at runtime.


Wednesday 20 January 2016

JAVA OOPS CONCEPT

Object Oriented Programming is a paradigm that provides many concepts such as inheritancedata bindingpolymorphism etc.
Simula is considered as the first object-oriented programming language. The programming paradigm where everything is represented as an object, is known as truly object-oriented programming language.
Smalltalk is considered as the first truly object-oriented programming language.

OOPs (Object Oriented Programming System)

Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:
  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Object

Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

Class

Collection of objects is called class. It is a logical entity.

Inheritance

When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

Polymorphism

When one task is performed by different ways i.e. known as polymorphism. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc.
In java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.

Abstraction

Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing.
In java, we use abstract class and interface to achieve abstraction.

Encapsulation

Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

Tuesday 19 January 2016

JAVA PROGRAMS

These programs can be asked from control statements, array, string, oops etc. Let's see the list of java programs.

1. Fibonacci series

Write a java program to print fibonacci series without using recursion and using recursion.
Input: 10
Output: 0 1 1 2 3 5 8 13 21 34

2. Prime number

Write a java program to check prime number.
Input: 44
Output: not prime number
Input: 7
Output: prime number

3. Palindrome number

Write a java program to check palindrome number.
Input: 329
Output: not palindrome number
Input: 12321
Output: palindrome number

4. Factorial number

Write a java program to print factorial of a number.
Input: 5
Output: 120
Input: 6
Output: 720

5. Armstrong number

Write a java program to check Armstrong number.
Input: 153
Output: Armstrong number
Input: 22
Output: not Armstrong number