Monday, 20 January 2014

A CLOSER LOOK AT METHODS AND CLASSES

METHOD OVERLOADING


If a class have multiple methods by same name but different parameters, it is known as Method Overloading.Method overloading is one of the ways through which java supports polymorphism. Method overloading can be done by changing number of arguments or by changing the data type of arguments.If two or more method have same name and same parameter list but differs in return type are not said to be overloaded method.

Using finalize() method in java

The finalize() method:


finalize method in java is a special method much like main method in java. finalize() is called before Garbage collector reclaim the Object, its last chance for any object to perform cleanup activity i.e. releasing any system resources held, closing connection if open etc.The intent is for finalize() to release system resources such as open files or open sockets before getting collected.

Using Garbage Collection in java

GARBAGE COLLECTION:

In java, garbage means unreferenced objects.The java.lang.System.gc() method runs the garbage collector. Calling this suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

this KEYWORD IN JAVA

this  is a keyword in Java. Which can be used inside method or constructor of  class. It(this) works as a reference to current object whose method or constructor is being invoked. this keyword can be used to refer any member of current object from within an instance method or a constructor.

CONSTRUCTORS IN JAVA


A constructor initializes an object immediateily upon creation.It has the same name as the class in which it resides and is syntactically similar to a method.Once defined, the constructor is automatically called immediately after the object is created,before the new operator completes.Constructors look a little strange because they have no return type ,not even void.Attributes of an object may be available when creating objects if no attribute is available then default constructor is called, also some of the attributes may be known initially.

Using object in java


As you know, a class provides the blueprint for objects; you create an object from a class.A typical Java program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.

Sunday, 19 January 2014

INTRODUCING METHODS

In java,there are some fundamentals that you need to learn now so that you can begin to add methods to your classes.Although it is perfectly fine to create a class that contains only data,it rarely happens.Most of the time you will use methods to access the instance variables defined by the class.

DIFFERENT TYPE OF VARIABLES IN JAVA

A class can contain any of the following variable types.
                           1.Local Variables
                           2.Instance variables
                           3.Class variables

FUNDAMENTAL CLASS PATTERNS IN JAVA

A class the basic building block of an object-oriented language such as Java is a template that describes the data and behavior associated with instances of that class. The data associated with a class or object is stored in variables; the behavior associated with a class or object is implemented with methods.

Friday, 17 January 2014

JUMP STATEMENTS OR BRANCHING STATEMENTS:return

Using return statement


return:


The return statement is used to explicitly return from a method. The return statement is used to explicitly return from the method . That is, that is it causes program control to transfer back to the caller of the method.

JUMP STATEMENTS OR BRANCHING STATEMENTS:continue


Using continue statement


continue:


 statement in java used to skip the part of the loop.Sometimes it is useful to force an early iteration of a loop.Unlike break statement it does not terminate the loop,instead it skip the remaining part of the loop and the control again goes to check the condition again.Continue statement can be used only loop control statements,

Thursday, 16 January 2014

Iteration statements : do-while

How to use java do-while:Exit control loop

Using do-while

A do-while is similar to while loop,except that a do-while loop is quaranteed to execute atleast one time. For that you have to use the do while loop in  after that check the condition then we use the do-while loop statement. do-while loop is same as the while loop statement but while loop checks the certain condition is first and if condition is true then statements or processes are executed otherwise all the statements written under the while loop is ignored by the interpreter

Thursday, 9 January 2014

JUMP STATEMENTS OR BRANCHING STATEMENTS:break

 Jump statements:break,continue,and return:These statements transfer control to another part of your program.

Using break statement


break:


Break statement is one of the several control statements Java provide to control the flow of the program.Break Statement is generally used to break the loop of switch statement.In java,break statement has three uses.First ,as you  have seen,it terminates a statement sequence in a switch statement.Second,it can be used to exit a loop.Third,it can be used as a : "civilized" form of goto.

Let’s look at syntax of break statement:

break;



The break statement is written as break; without any expression.

Example for simple break  statement

class BreakDemo
{
  public static void main(String[] args)
  {
  for (int i = 0; i < 5; i++)
  {
  System.out.println(i);
  if (i==3)
  {
  break ;
  }
  }
  }
}

This would produce the following results

0
1
2
3




Saturday, 4 January 2014

Iteration statement:while loop


How to use Java while:

Using while loop

While loop:

The while loop is java’s most fundamental looping statement.It repeats a statement or block while its controlling expression is true.In while statement condition can be any boolean expression.

Iteration statements:for loop in java

The most common iteration Statements in Java

Iteration Statement: for loop


The 'for' LOOP:


The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. If only one statement being repeated, there is no need for the curly braces. When using this version of the for statement, keep in mind that,The initialization expression initializes the loop; it's executed once, as the loop begins.

Switch Statement in Java

How to use Java Switch: Case: Default: 

Using switch statement

Swich:

The switch statement is Java’s multiway branch statement.It provides an easy way to dispatch execution to different parts of your code based on the value of an expression.It is an alternatives of else-if ladder. Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression. The expression used in the switch statement must return an int, a String, or an enumerated value.

Friday, 3 January 2014

Nested if Statement in Java

The most common Control Statements in Java

Control Statement Nested if


The nested if Statement:

Nested if are very common in programming. Nesting an if  Statement just means putting one if Statement inside of another. when you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is with  in the same block as the else and that is not already associated with an else.