Monday, October 12, 2009

Threads

Threading concept is very important in Java Programing language. A thread is a sequential path of code execution within a program. And each thread has its own local variables, program counter and lifetime.

Life Cycle of A Thread

Life Cycle of Thread contains different states - New state, Runnable, Running state, Dead state, Non-Runnable state.


Thread Creation
Thread can be implementing by one of two ways - Extending the java.lang.Thread Class, Implementing the java.lang.Runnable Interface.

Thread Constructors


Several constructors are available for creating new Thread instances like Thread(), Thread(String), Thread(Runnable) etc....

Introduction to Multithreading

Multithreading is a technique that allows a program or a process to execute many tasks concurrently (at the same time and parallel). It allows a process to run its tasks in parallel mode on a single processor

Creation of Multiple Threads

Like creation of a single thread, You can also create more than one thread (multithreads) in a program using class Thread or implementing interface Runnable.

Thread Priorities and Scheduler

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.

Deadlock

A situation where a thread is waiting for an object lock that holds by second thread, and this second thread is waiting for an object lock that holds by first thread, this situation is known as Deadlock.

Lock and Synchronized Threads

Java uses monitor also known as “semaphore” to prevent data from being corrupted by multiple threads by a keyword synchronized to synchronize them and intercommunicate to each other. Lock term refers to the access granted to a particular thread that can access the shared resources.

Inter-Thread Communication

A process where, a thread is paused while running in its critical region and another thread is allowed to enter (or lock) in the same critical section to be executed. This technique is known as Inter-Thread communication

Daemon Thread

Daemon threads are service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing

Example for String Buffer

import java.io.*;

public class stringBuffer{
public static void main(String[] args) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str;
try{
System.out.print("Enter your name: ");
str = in.readLine();
str += ", This is the example of SringBuffer class and it's functions.";

//Create a object of StringBuffer class
StringBuffer strbuf = new StringBuffer();
strbuf.append(str);
System.out.println(strbuf);
strbuf.delete(0,str.length());

//append()
strbuf.append("Hello");
strbuf.append("World"); //print HelloWorld
System.out.println(strbuf);

//insert()
strbuf.insert(5,"_Java "); //print Hello_Java World
System.out.println(strbuf);

//reverse()
strbuf.reverse();
System.out.print("Reversed string : ");
System.out.println(strbuf); //print dlroW avaJ_olleH
strbuf.reverse();
System.out.println(strbuf); //print Hello_Java World

//setCharAt()
strbuf.setCharAt(5,' ');
System.out.println(strbuf);
//prit Hello Java World

//charAt()
System.out.print("Character at 6th position : ");
System.out.println(strbuf.charAt(6)); //print J

//substring()
System.out.print("Substring from position 3 to 6 : ");
System.out.println(strbuf.substring(3,7)); //print lo J

//deleteCharAt()
strbuf.deleteCharAt(3);
System.out.println(strbuf);
//print Helo java World

//capacity()
System.out.print("Capacity of StringBuffer object : ");
System.out.println(strbuf.capacity()); //print 21

//delete() and length()
strbuf.delete(6,strbuf.length());
System.out.println(strbuf); //no anything
}
catch(StringIndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
}
}

String Buffer in Java

This example explains how you can use functions provided by the StringBuffer class like append, insert, reverse, setCharAt, charAt, length, deleteCharAt, substring, delete, capacity etc. to manipulate the string operation in your program.

The StringBuffer class is used to represent characters that can be modified. This is simply used for concatenation or manipulation of the strings.

StringBuffer is mainly used for the dynamic string concatenation which enhances the performance. A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. There are some functions used in the given example. All the functions have been explained below with example :

append()

This is the append() function used for the concatenate the string in string buffer. This is better to use for dynamic string concatenation. This function works like a simple string concatenation such as : String str = str + "added string";.

insert()

This is the insert() function used to insert any string or character at the specified position in the given string.

reverse()

This is the reverse() function used to reverse the string present in string buffer.

setCharAt()

This is the setCharAt() function which is used to set the specified character in buffered string at the specified position of the string in which you have to set the given character.

charAt()

This is the charAt() function which is used to get the character at the specified position of the given string.

substring()

This is the substring() function which is used to get the sub string from the buffered string from the initial position to end position (these are fixed by you in the program).

deleteCharAt()

This is the deleteCharAt() function which is used to delete the specific character from the buffered string by mentioning that's position in the string.

length()

This is the length() function is used to finding the length of the buffered string.

delete()

This is the delete() function is used to delete multiple character at once from n position to m position (n and m are will be fixed by you.) in the buffered string.

capacity()

This is the capacity() function is used to know about the current characters kept which is displayed like : number of characters + 6.


For Example Click Here

Saturday, October 10, 2009

Java

Developing a Java program involves

  1. writing code,
  2. compiling it into bytecode
  3. running the bytecode.

Your First Java Program.

public class MainClass {
public static void main(String[] args) {
System.out.println("Java");
}
}




The Primitive Types

Java has eight primitive types of data: byte, short, int, long, char, float, double, and boolean.

These can be put in four groups:

  1. Integers includes byte, short, int, and long
  2. Floating-point numbers includes float and double
  3. Characters includes char, like letters and numbers.
  4. Boolean includes boolean representing true/false values.
byte

The smallest integer type
a range from -128 to 127.
useful when working with a stream of data from a network or file.
Byte variables are declared by use of the byte keyword.

byte b, c;

int

The most commonly used integer type
a signed 32-bit type
Ranging from -2,147,483,648 to 2,147,483,647
used to control loops and to index arrays.
the most efficient type

long

a signed 64-bit type

Friday, October 9, 2009

PL SQL INTERVIEW QUESTIONS

PL-SQL Interview Questions with Answers




1. Describe the difference between a procedure, function and anonymous pl/sql block.
Level: Low
Expected answer : Candidate should mention use of DECLARE statement, a function must return a value while a procedure doesn't have to.



2. What is a mutating table error and how can you get around it?
Level: Intermediate
Expected answer: This happens with triggers. It occurs because the trigger is trying to update a row it is currently using. The usual fix involves either use of views or temporary tables so the database is selecting from one while updating the other.


3. Describe the use of %ROWTYPE and %TYPE in PL/SQL
Level: Low
Expected answer: %ROWTYPE allows you to associate a variable with an entire table row.
The %TYPE associates a variable with a single column type.





4. What packages (if any) has Oracle provided for use by developers?
Expected answer: Oracle provides the DBMS_ series of packages. There are many
which developers should be aware of such as DBMS_SQL, DBMS_PIPE, DBMS_TRANSACTION,
DBMS_LOCK, DBMS_ALERT, DBMS_OUTPUT, DBMS_JOB, DBMS_UTILITY, DBMS_DDL, UTL_FILE. If they can mention a few of these and describe how they used them, even better. If they include the SQL routines provided by Oracle, great, but not really what was asked.



5. Describe the use of PL/SQL tables
Expected answer: PL/SQL tables are scalar arrays that can be referenced by a
binary integer. They can be used to hold values for use in later queries
or calculations. In Oracle 8 they will be able to be of the %ROWTYPE designation, or RECORD.




6. When is a declare statement needed ?
The DECLARE statement is used in PL/SQL anonymous blocks such as with stand alone, non-stored PL/SQL procedures. It must come first in a PL/SQL stand alone file if it is used.



7. In what order should a open/fetch/loop set of commands in a PL/SQL block be implemented if you use the NOTFOUND cursor variable in the exit when statement? Why?
Expected answer: OPEN then FETCH then LOOP followed by the exit when. If not specified in this order will result in the final return being done twice because of the way the %NOTFOUND is handled by PL/SQL.



8. What are SQLCODE and SQLERRM and why are they important for PL/SQL developers?
Expected answer: SQLCODE returns the value of the error number for the last error encountered. The SQLERRM returns the actual error message for the last error encountered. They can be used in exception handling to report, or, store in an error log table, the error that occurred in the code. These are especially useful for the WHEN OTHERS exception.



9. How can you find within a PL/SQL block, if a cursor is open?
Expected answer: Use the %ISOPEN cursor status variable.




10. How can you generate debugging output from PL/SQL?
Expected answer: Use the DBMS_OUTPUT package. Another possible method is to just use the SHOW ERROR command, but this only shows errors. The DBMS_OUTPUT package can be used to show intermediate results from loops and the status of variables as the procedure is executed. The new package UTL_FILE can
also be used.




11. What are the types of triggers?
Expected Answer: There are 12 types of triggers in PL/SQL that consist of
combinations of the BEFORE, AFTER, ROW, TABLE, INSERT, UPDATE, DELETE and
ALL key words:
BEFORE ALL ROW INSERT
AFTER ALL ROW INSERT
BEFORE INSERT
AFTER INSERT etc.