Monday, 8 May 2017

Java Modifiers ~ mkniit

Modifiers in Java

Modifiers are keywords that are added to change meaning of a definition. In Java, modifiers are catagorized into two types,
  1. Access control modifier
  2. Non Access Modifier

1) Access control modifier

Java language has four access modifier to control access levels for classes, variable methods and constructor.
  • Default : Default has scope only inside the same package
  • Public : Public scope is visible everywhere
  • Protected : Protected has scope within the package and all sub classes
  • Private : Private has scope only within the classes
Read More »

Garbage Collection ~ mkniit

In Java destruction of object from memory is done automatically by the JVM. When there is no reference to an object, then that object is assumed to be no longer needed and the memory occupied by the object are released. This technique is called Garbage Collection. This is accomplished by the JVM.
Unlike C++ there is no explicit need to destroy object.
garbage collection in java
Read More »

this keyword ~ mkniit

  • 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 :
class Box
 {
  Double width, weight, dept; 
  Box (double w, double h, double d)
  {
   this.width = w;
   this.height = h;
   this.depth = d;
  }
}
Here the this is used to initialize member of current object.


The this is used to call overloaded constructor in java

class Car
{
 private String name;
 public Car()
 {
  this("BMW");    //oveloaded constructor is called.
 }
 public Car(String n)
 {
  this.name=n;   //member is initialized using this.
 }
}

The this is also used to call Method of that class.

public void getName()
{
 System.out.println("Studytonight");
}

public void display()
{
 this.getName();
 System.out.println();
}

this is used to return current Object

public Car getCar()
{
 return this;
}
Read More »

Constructor in Java ~ mkniit

A constructor is a special method that is used to initialize an object.Every class has a constructor,if we don't explicitly declare a constructor for any java class the compiler builds a default constructor for that class. A constructor does not have any return type.
A constructor has same name as the class in which it resides. Constructor in Java can not be abstract, static, final or synchronized. These modifiers are not allowed for constructor.
class Car
{
 String name ;
 String model;
 Car( )    //Constructor 
 {
  name ="";
  model="";
 }
}  
Read More »

Method Overloading ~ mkniit

Methods in Java

Method describe behavior of an object. A method is a collection of statements that are group together to perform an operation.
Syntax :
return-type methodName(parameter-list)
{
 //body of method
}

Example of a Method

public String getName(String st)
{
 String name="StudyTonight";
 name=name+st;
 return name;
}
Read More »

Objects and Classes ~ mkniit

Since Java is an object oriented language, complete java language is build on classes and object. Java is also known as a strong Object oriented programming language(oops).
OOPS is a programming approach which provides solution to problems with the help of algorithms based on real world. It uses real world approach to solve a problem. So object oriented technique offers better and easy way to write program then procedural programming model such as C, ALGOL, PASCAL.

Main Features of OOPS

  • Inheritence
  • Polymorphism
  • Encapsulation
  • Abstraction
As an object oriented language Java supports all the features given above. We will discuss all these features in detail later.
Read More »

Operators in Java ~ mkniit

Java Operators

Java provides a rich set of operators enviroment. Java operators can be devided into following categories
  • Arithmetic operators
  • Relation operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Conditional operators
  • Misc operators
Read More »

Java Array ~ mkniit

Concept of Array in Java

An array is a collection of similar data types. Array is a container object that hold values of homogenous type. It is also known as static data structure because size of an array must be specified at the time of its declaration.
An array can be either primitive or reference type. It gets memory in heap area. Index of array starts from zero to size-1.

Array Declaration

Read More »

Variable in Java ~ mkniit

Variable

Java Programming language defines mainly three kind of variables.
  1. Instance variables
  2. Static Variables
  3. Local Variables
Read More »

Typecasting ~ mkniit

Type Casting

Assigning a value of one type to a variable of another type is known as Type Casting.
Example :
int x = 10;
byte y = (byte)x;
In Java, type casting is classified into two types,
  • Widening Casting(Implicit)
  • widening-type-conversion
  • Narrowing Casting(Explicitly done)
  • narrowing-type-conversion
Read More »

Data type and Identifier ~ mkniit

Data Types in Java

Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.
In java, data types are classified into two catagories :
  1. Primitive Data type
  2. Non-Primitive Data type
Read More »