Sunday, June 9, 2013

Define different main method format in Java

Do you know what do you mean of “public static void main()”?

Public

It  is “access specifier” ; so it is basically define the scope of methods, classes and interface and etc.  you know JRE (Java Run-time Environment) is the outside part of class. if we use other access specifier (private, protected) so you can’t access the that class. If you are not used any access specifier. So compiler use the default access specifier.
Static

In class there are two type members
      1-Instance member
          represent attributes and behavior of individual object. By default all the      member of class is instance member.
     2-class member
          represent attributes and behavior of whole class. Denoted  by  “static”   keyword.
So static keyword are used define the class member.
Void

Functions are always requires a return type so void is one of them to returns value implicitly. If any error occur in the program so O.S returns the value with the help of  JRE.
Main:

only method name
Are you know the given below syntax are valid or not valid  in JAVA?

Syntex-(Modifier ReturnType MethodName() )

Static public void main (String [] arg ) true
Static public void main(String[][] arg) runtime error NoSuchMethodError
Static void public main (String [][] arg) compile time error
Void static public main (String arg[]) compile time error
 public static void main (String arg) runtime error NoSuchMethodError
Public void static main ([] String arg) compile time error
Public static void main (String[] arg[]) runtime error NoSuchMethodError
Public static void main() runtime error NoSuchMethodError

                                                 Author-Ravi Kumar


Wednesday, June 5, 2013

Joins in SQL

Introduction

SQL joins are used to relate information in different table or say joins are used to combine two or more tables.

Types of Join

 Types of joins are
  1. Self Join
  2. Inner Join
  3. Outer Join
    Outer Join are three types:

    (a) Right Outer Join
    (b) Left Outer Join
    (c) Full Outer Join
     
  4. Cross Join
    Cross  Join are two types:

    (a) Implicit cross join
    (b) Explicit cross join
Now, I explain each and every separately.

1- Self Join

A "self-join" joins a table itself. Basically "self-join" are rare, they are some time useful for retrieving data that can't retrieved any other way.

Syntax

SELECT a.columnName, b.columnName.... FROM table1 a, table1 b where a.commonField=b.common_field;
Example

This example describes you how to play with SELF-JOIN.
Look "empdtl" table design

Now I am inserting some data in that table, After inserting some values table data look like

Now, I want to get details of those "employee's", who are manager. Then I simply write a query for it.

SELECT distinct e1.empid, e1.empname FROM empdtl e1, empdtl e2 where e1.empid=e2.managerid;Output


2- Inner Join
The Join condition indicates how the two table should be compared. The "inner join" is a join in which the values in the columns being joined are compare using a common operator. You can simple say "inner join" selects all rows from both table as long as there is a match between the columns in both tables.
Syntax
SELECT columnsName or selectlist FROM table1 INNER JOIN table2 ON table1.columnName=table2.columnName

OR
SELECT columnsName or selectlist FROM table1  JOIN table2 ON table1.columnName=table2.columnName
Here I shown "empdtl" and "dept" table data as a image for using the INNER JOIN

Query For Inner Join 



Here I want to get the "deptname" from  the "dept" table, "id" and "name" from "empdtl" table, on the behalf of "id"  of the "empdtl" table, Let's see how to do it:

SELECT empdtl.id,empdtl.name,dept.deptname from empdtl join dept on empdtl.id=dept.deptid;
 Output

 

3- Outer Join

The "outer join" are three types I explain each and every separately:
(a) Right Outer Join
 
Right Outer join returns all rows from the right table, with the matching row in the left table, the result is NULL in the left side when there is no match.Data of both "empdtl" and "dept" table 

Example

SELECT empdtl.id,empdtl.name, dept.deptname from empdtl RIGHT OUTER JOIN dept on empdtl.id=dept.deptid;Output


(b) Left Outer Join

The "left outer join" is used to returning the all rows from the left table, with the matching row in the right table and the NULL is appear in the right side when there is no match.
Data of both "empdtl" and "dept" table 

Example
SELECT empdtl.id,empdtl.name, dept.deptname from empdtl LEFT OUTER JOIN dept on empdtl.id=dept.deptid;Output

(c) Full Outer Join

The "full outer join" is used to returns all woes from the left and right table or you can say full outer join combines the result of both left and right joins.

Example

SELECT empdtl.id,empdtl.name, dept.deptname from empdtl FULL OUTER JOIN dept on empdtl.id=dept.deptid;Output

4- Cross Join

A "cross join" produces a result set that indicates each row from the first table joined with each row of the second table and this result set is known as the Cartesian Product of the table and the "cross join" have two types.

(a) Explicit cross join

To use an "explicit cross join", write the "cross join" keywords after the first table name and before the second table name
Syntax
SELECT columnsName or selectList FROM table1 CROSS JOIN table2

Example
SELECT id,name,deptname from empdtl CROSS JOIN deptOutput

(b) Implicit  cross join

To use an "implicit  cross join", you do not need to write the "cross join" keywords after the first table name and before the second table name, you just simply write comma (,) symbol after the first table name and before the second table name.
Syntax
SELECT columnsName or selectList FROM table1, table2

Example
SELECT id,name,deptname from empdtl,deptOutput

Sunday, June 2, 2013

Basic Interview Question in Java



Introduction

Java, now it’s time to play with JAVA. I started with a new existing point.

Can we use multiple main in a program

Yes; we can use multiple “main()” method in a java program. But the both main methods argument should not be same, as you know, the  java programs execution is start from “main()” method means (
public static void main(String arg[]), then simply declare the second ‘main()” method in this main method for execution, Now the given below example shown you how to do it.

E
xample

 class A
{
public static void main(String arg[])
{System.out.println("welcome");
main();

}
public static void main()
{
System.out.println("first");
}
}

Output

Can we run the program without main(String [] args) method

Yes we can run a java program without  main() method. Because java provide a functionality with the use of “static” keyword and that “static” keyword can be a block or static data member or static method,  firstly execute whenever you compile a  java program. Let’s see, it works or not.

Example
 class A
{
static
{
System.out.println("welcome");
System.exit(0);
}
}Output



Can we change the main thread name?

Yes, we can change the main thread name.

Can we print the message before the main method execution?

Yes, we can print the message before the main method. Let’s see how

Example
class A
{
static
{
System.out.println("welcome");
}
public static void main(String arg[])
{System.out.println("main method");}}Output