Saturday, 25 January 2014

Suspending,Resuming,and Stoping Thread using java 2

modern way of suspending,resuming,stoping:...

The Modern Way of Suspending, Resuming, and Stopping Threads


The Modern Way of Suspending, Resuming, and Stopping Threads While the suspend(),resume( ), andstop( ) methods defined by Thread seem to be a perfectly reasonable and convenient approach to managing the execution of threads, they must not be used for new Java programs.

Suspending


The suspend( ) method of theThread class was deprecated by Java 2 several years ago. This was done because suspend( ) can sometimes cause serious system failures. Assume that a

THREAD PRIORITIES

SET THREAD PRIORITY IN JAVA

Explain thread priority in java with suitable example

In Java, thread scheduler can use the thread priorities in the form of integer value to each of its thread to determine the execution schedule of threads. Thread gets the ready-to-run state according to their priorities. The thread scheduler provides the CPU time to thread of highest priority during ready-to-run state.  Priorities are integer values from 1 to 1.

Friday, 24 January 2014

Using isAlive() and join() method in java

 how to use isAlive() and Join( ):

Using isAlive( ) and join( )


As mentioned, often you will want the main thread to finish last.Two ways exist to determine whether a thread has finished.


  •   isAlive()
  • join()

CREATING A THREAD

CREATING AND STARTING THREADS IN JAVA

What are the two different methods of creating a thread in Java? What method must be implemented in either case?


In Java, an object of the Thread class can represent a thread. Thread can be implemented through any one of two ways:

                          1.You can implement the Runnable interface.
                          2.You can extend the Thread class itself.

Creating Multiple Threads in java

how to create multiple thread:multi tasking

Creating Multiple Threads


Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. you have been using only two threads: the main thread and one child thread.

THE MAIN THREAD

Java Exception in thread main Understanding with Examples

The 'main()' method in Java is referred to the thread that is running, whenever a Java program runs. It calls the main thread because it is the first thread that starts running when a program begins. Other threads can be spawned from this main thread. The main thread must be the last thread in the program to end. When the main thread stops, the program stops running.Main thread is created automatically, but it can be controlled by the program by using a Thread object. The Thread object will hold the reference of the main thread with the help of currentThread() method of the Thread class.

Multi Threaded Programming in java

how to use multithreaded :runnable interface:...

Multithreaded Programming:


Unlike many other computer languages, Java provides built-in support formultithreaded programming. A multithreaded program contains two or more parts that can run concurrently.There are two distinct types of multitasking:process based and thred based.A process.in essence,a program that is executing.

CHAINED EXCEPTIONS IN JAVA

What are the Chained Exceptions  Used in java

An application often responds to an exception by throwing another exception. In effect, the first exception causes the second exception. It can be very helpful to know when one exception causes another.Chained Exceptions help the programmer do this.Exception chaining, or exception wrapping, is an object-oriented programming technique of handling exceptions by re-throwing a caught exception after wrapping it inside a new exception. The original exception is saved as a property (such as cause) of the new exception.

JAVA'S BUILT-IN EXCEPTIONS

Java Exception Handling:Unchecked and Checked Exception

What are the various Checked exceptions defined in the java.lang.package and Unchecked RuntimeException subclasses 


The built-in exceptions in java are categorized on the basis of whether the exception is handled by the java Compiler or not. Java consists of the following categories of built-in exceptions:

  • Checked Exceptions
  • Unchecked Exceptions

Thursday, 23 January 2014

BASIC finally EXCEPTION HANDLING IN JAVA

Exceptions:The finally Block 

Example to show Finally exception in java

The Finally block in java is used along with the try-catch statements.This block executed even after the unexpected exception occured.The Run time always execute the expression in finally block irrespective of the try block.  The main usage of finally block is to do clean up job. Keeping cleanup code in a finally block is always a good practice, even when no exceptions are occured. The runtime system always executes the code within the finally block regardless of what happens in the try block. So it is the ideal place to keep cleanup code.

throws JAVA KEYWORD

Java:Using throws in Exception Handling

How to throws exceptions in java with example

The throws keyword is used to declare an exception. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.Exception Handling is mainly used to handle the checked exceptions. If there occurs any unchecked exception such as NullPointerException, it is programmers fault that he is not performing check up before the code being used.

throw EXCEPTIONS IN JAVA

Java:Using throw


When to use throw in a Java method declaration?

All methods use the throw statement to throw an exception. The throw statement requires a single argument: a Throwable object. Throwable object are instances of any subclass of the Throwable classThe flow of execution stops immediately after the throw statement;any subsequent statements are not executed.

Wednesday, 22 January 2014

NESTED try STATEMENT

NESTED try IN EXCEPTION HANDLING


Java:nested try-catch blocks with simple example

The try block within a try block is known as nested try block.Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested, one within another. In java, this can be done using nested try blocks. A try statement can be inside the block of another try. In a nested try block, every time a try block is entered the context of that exception is pushed on the stack.

MULTIPLE catch CLAUSES IN JAVA

HANDLING MULTIPLE catch CLAUSES
Java Exception Handling:catching multiple exceptions

The code bound by the try block need not always throw a single exception. If in a try block multiple and varied exceptions are thrown, then you can place multiple catch blocks for the same try block in order to handle all those exceptions. When an exception is thrown it traverses through the catch blocks one by one until a matching catch block is found.

USING try AND catch

How to use exception handling keywords try and catch in java

THE try BLOCK


A try statement is used to catch exceptions that might be thrown as your program executes. You should use a try statement whenever you use a statement that might throw an exception,That way,your program won’t crash if the exception occurs.The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block. 

EXCEPTION HANDLING IN JAVA

how to use exception handling

Exception-Handling Fundamentals

An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error.When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error.That method may choose to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed.

INTERFACES IN JAVA

INTERFACE 


Defining an Interface, Implementing Interfaces,Accessing Implementations Through Interface References,Partial Implementations

An  interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.

PACKAGES AND INTERFACES

PACKAGES

Packages in java with example program

Packages are containers for classes. Packages help in avoiding naming conflicts.A package defines a name space for a set of classes that are related to each other. The name space defined by a package ensures that there are no two classes with the same name. But two packages can have a class with the same name without any name collision among them.We can define classes inside a package that are not accessible by code outside that package.One can also define class members that are only exposed to other members of the same package. This allows your classes to have intimate knowledge of each other, but not exposing that knowledge to the rest of the world.

The Object class in java

what are the methods define object in java

The object class:


There is one special class, Object, defined by Java. All other classes are subclasses of Object.That is, Objectis a superclass of all other classes. This means that a reference variable of type Object can refer to an object of any other class. Also, since arrays are implemented as classes,a variable of type Object can also refer to any array.

Tuesday, 21 January 2014

Using final with Inheritance

how to use final apply to inheritance.

Using final to Prevent Overriding


While method overriding is one of Java’s most powerful features.To disallow a method from being overridden,specify final as a modifier at the start of its declaration.

METHOD OVERRIDING IN JAVA

Method Overriding

Brief note about method overriding 


Method overriding means have same signature but with different implementation. It occurs when a class declares a method that has the same type signature as a method declared by one of its super classes.Signature is a combination of a method name and the sequence of its parameter types.When a method in the subclass overrides a method in a super class, the method in the super class is hidden relative to the subclass object.Method Overriding is a very important feature of java because it forms the basis for run-time polymorphism.  

ABSTRACT CLASSES AND METHODS IN JAVA

How to declare abstract class and abstract methods in java 

USING ABSTRACT CLASSES


A class which can not be instantiated is known as abstract class.If a class contain any abstract method then the class is declared as abstract method.It must be overridden. An abstract class must be extended and in a same way abstract method must be overridden.Abstract method has no body,it  always end the declaration with a semicolon(;).

Using Dynamic Method Dispatch in java

 how to use dynamic method dispatch:method overriding


Dynamic Method Dispatch

Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic Method Dispatch is related to a principle that states that an super class reference can store the reference of subclass object. However, it can't call any of the newly added methods by the subclass but a call to an overridden methods results in calling a method of that object whose reference is stored in the super class reference.

MULTILEVEL HIERARCHY IN JAVA PROGRAMMING

MULTILEVEL HIERARCHY


Creating multilevel hierarchy in java

In simple inheritance a subclass or derived class derives the properties from its parent class, but in multilevel inheritance a subclass is derived from a derived class. One class inherits only single class. Therefore, in multilevel inheritance, every time ladder increases by one. The lower most class will have the properties of all the super classes’.

Using super in java

how to use super keywords:constructors

super:

super has two general forms. The first calls the superclass’ constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass.

INHERITANCE BASICS

Basics of Inheritance in java

How to use extends keyword to inherit the super class

Inheritance is one of the key feature of Object Oriented Programming.There are three types of inheritance in java,they are Single Inheritance,Multilevel Inheritance and Hierarchical Inheritance.Inheritance provided mechanism that allowed a class to inherit property of another class.When a class extends another class it inherits all non-private members including fields and methods.For class inheritance, Java uses the keyword extends.

Exploring the string class

how to use string classes:creating string

String class:

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.One very important thing to notice about String is that String object are immutable that means once a String object is created, its contents cannot be altered. However if you need to change a String, you can do it by creating a new String object.. string you create is actually an object of type String. Even string constants are actually String objects. 

INTRODUCING NESTED AND INNER CLASSES

NESTED CLASSES IN JAVA

A class declared inside a class is known as nested class. We use nested classes to logically group classes in one place so that it can be more readable and maintainable code. Moreover, it can access all the members of outer class including private members.

INTRODUCING final

In Java class can be declared as final using final keyword. If the final keyword is used in the class declaration the class becomes unable to be sub-classed. No any class can extend the final class i.e. features of a final class can't be inherited. Various classes of Java library are defined as final fro example java.lang.String, java.lang.System etc. If a final class contains the methods then these methods becomes final implicitly. In Java classes are declared as final for the security and efficiency reason. final makes the class immutable.

Introducing access controls in java

how to specified access specifiers:public:private:protect

Access control:


 Encapsulation links data with the code that manipulates it. However,encapsulation provides another important attribute: access control.Java’s access specifiers are public, private, and protected. Java also defines a default access level.protected applies only when inheritance is involved. When a member of a class is modified by the public specifier, then that member can be accessed by any other code.

Monday, 20 January 2014

UNDERSTANDING static


The static keyword is used in java mainly for memory management. We may apply static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.The static keyword can be used in 3 scenarios,they are  static variables,static methods and static blocks of code.

RECURSION IN JAVA

Recursion:


Java supports recursion.Recursion is the process of defining something in terms of itself. As it relates to Java programming, recursion is the attribute that allows a method to call itself. A method that calls itself is said to be recursiveEvery recursion should have some characteristics.That is a simple base case which we have a solution for and a return value and a way of getting our problem closer to the base case. I.e. a way to chop out part of the problem to get a somewhat simpler problem and  a recursive call which passes the simpler problem back into the method.When a method calls itself,

A CLOSER LOOK AT ARGUMENT PASSING

There are two ways that a computer language can pass an argument to a subroutine. The first way is call-by-value.This method copies the value of an argument into the formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect on the argument. The second way an argument can be passed is call-by-reference. In Java, when you pass a simple type to a method, it is passed by value. Thus, what occurs to the parameter that receives the argument has no effect outside the method.

DIFFERENT WAYS TO OVERLOAD THE METHOD

There are two ways to overload the method in java

  • By changing number of arguments
  • By changing the data type

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.