Multi-threading is a topic, that I found most difficult to understand. First thing that needs to be understood is the distinction between a Thread and a Program. Thread is the smallest unit of execution. A program may contain more than one thread that executes concurrently. Let me tell you that saying concurrent execution is not correct, a computer processor can execute only one process at a time (single core processors), and if a process involves more than one Thread, only one Thread, would execute at a time, when a Thread gets paused, then and then only, any other Thread can start executing. If you ask what exactly is the advantage of multi-threading, then the advantage of multi-threading is that you can assign different tasks to Threads, for example, As in Android, Image Retrieval from database is time consuming and processor intensive task, so it is preferred to implement image retrieval mechanism on a separate thread.
Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts
Methods with Variable Number of Arguments
In my last article, I discussed how before the introduction of VarArgs, programmers had to deal with the requirement of methods. Here, I am going to discuss the VarArgs Methods, that allow you to deal with the requirement of variable number of arguments in a method. A variable length argument is specified by three periods. For example
static void VarMethod(int...v)
Old School Variable Argument Method
Before introduction of Varargs(which is going to be our next topic of discussion in Java Journal), two appraoches were used to create methods with variable number of arguments. They are
First approach invloved the use of overloaded method. The method simply involved creating all poosible set of input arguments and define a separate method with same name. This approach gaurantees to push you onto edge.
Second approach was to use a single method with on;y one argument array of the required type. We will discuss this method in here. However, the problems associated with this approach was that each time, preparing for a call to a method, an array was supposed to be created. This method can be extremely complex and errorprone if number of calls are large.
First approach invloved the use of overloaded method. The method simply involved creating all poosible set of input arguments and define a separate method with same name. This approach gaurantees to push you onto edge.
Second approach was to use a single method with on;y one argument array of the required type. We will discuss this method in here. However, the problems associated with this approach was that each time, preparing for a call to a method, an array was supposed to be created. This method can be extremely complex and errorprone if number of calls are large.
Static Block Demystified
Static keyword in Java has unusual properties. A variable flagged as static acts like a global variable, a class maintains a single copy of the static variables in the memory which is shared by all the class instances or objects of that class. A method tagged as static can be called without the creation of class object at first. Being a static method also puts some restrictions on the method, for example, it can only access static variables, can call only static methods and can not refer to "this" or "super" keyword in any way. In this article, I will try to clear some concepts regarding Static Block.
Java Program to swap values of two numbers without temporary variable
A typical value swapping progran involves use of a temporay storage to store the value of either of the two variables whose values is to be swapped. In here, I would try to make you understand the true logic of the oprogrm. This program is simply based on the fact that java assignment statement, for example,
a = 1 +3;
would add first 1 and 3, after the addition, the value is stored in the left hand value of the storage variable. Now, the program swapping vales is.
a = 1 +3;
would add first 1 and 3, after the addition, the value is stored in the left hand value of the storage variable. Now, the program swapping vales is.
How to return Objects from Methods
Java enables us to return objects from the methods as well. Returning and passing objects need to consider the parent child relationship among the classes. This relationship holds importance because, as a matter of fact, Object of parent class can hold the address of object of child class.
Fibonacci series program in Java
Fibonacci series is one the most series es in the world. In a Fibonacci series, each term is the sum of last two terms. For the first two terms, 1st is taken as 0 and second is taken as 1. Fibonacci series has attracted a lot of attention and research. So. let us devote a JAVA program to print the Fibonacci series.
Java program to reverse a number
Java program to reverse a number is one of the simplest programming problems. The JAVA program that reverses a number is given.
How to use Objects as Parameter
The first thing that anyone needs to know is the difference between argument and parameters. Arguments are the variables that are passed to the functions and parameters are the variables that receive the value of the arguments. Java rich Object Oriented Features allow us to use Objects as the parameter.
Method Overloading Demystified
Method Overloading is one of the best features of the Object Oriented Language. With Method Overloading, Java implements Polymorphism.
Garbage Collector Program in JAVA
Java language dynamically allocates the memory to the objects of the classes being used in the program. Unlike C++ where a destructor is needed to free the memory, JAVA employs Garbage Collector Mechanism to reclaim the allocated memory. When no referance to the object exists in the memory, it becomes eligible to be collected by Garbage Collector.
The finalize method provided by java sees to it that this method is executed just before the memory allocated to the object is being reclaimed. There is no gurantee that a finalize method would be executed. My aim was to observe the functioning of the garbage collector. So, I created a program. And here it is.
Subscribe to:
Posts (Atom)