Monday, 26 March 2018

Can main() method overloaded ??

Yes, main() can be overloaded. Let’s see the following examples for better clarity :

ExampleMain.java

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

public static void main(int x)
{
System.out.println("Hello");
}

}

Output
Hiii


Tuesday, 2 January 2018

JAVA 9 private method in interface

Hii frnds, here we will know an exciting feature of JAVA 9, that is definition of private method in interface. That means it is possible that we can happily define a private method inside the interface.
IntefaceExample.java
interface Test
{
void show();
default void print()
{
System.out.println("JAVA means SILAN TECHNOLOGY");
display();
}
private void display()
{
System.out.println("www.java8s.com");
}

}

class Demo implements Test
{
public void show()
{
System.out.println("SILAN TECHNOLOGY");
}
}

class InterfaceExample
{
public static void main(String[] args)
{
Demo obj=new Demo();
obj.show();
obj.print();
}

} 

Output
 SILAN TECHNOLOGY
JAVA means SILAN TECHNOLOGY
www.java8s.com

Monday, 18 December 2017

Can we compile and execute a java program without a class??

Ans: Yes, we can compile and execute without a class. Let’s see the following code:

Test43.java
enum Test43{
;  
public static void main(String[] args)
{
System.out.println("Silan Technology");
}

}

Output
Silan Technology

Friday, 8 December 2017

JAVA 9 Features

JAVA 9 Features :

1. Modularity -- jigsaw

2. REPL --  JShell

3. try with resource enhancements

4. Factory methods in collection

5. private methods in interface

6. Stream API enhancements

7. Http 2 Client(java.util.http)

8. G1 garbage collector

Friday, 24 November 2017

JDBC Program - How to Create a table

JdbcExample1.java

package java8s;

import java.sql.*;
public class JdbcExample1 {

     public static void main(String[] args) throws Exception{
         
          Class.forName("oracle.jdbc.driver.OracleDriver");
         
          Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
         
          Statement st=con.createStatement();
         
          String s= "create table customer("
                   + "C_ID NUMBER(5) NOT NULL, "
                   + "CNAME VARCHAR(100) NOT NULL, "
                   + "Caddress VARCHAR(200) NOT NULL, "
                   + "Creport_DATE DATE NOT NULL, " + "PRIMARY KEY (C_ID) "
                   + ")";
         
          System.out.println(s);
          st.execute(s);
          System.out.println("table is created");
         

     }


}

Output

create table customer1(C_ID NUMBER(5) NOT NULL, C_NAME VARCHAR(20) NOT NULL, C_add VARCHAR(20) NOT NULL, C_DATE DATE NOT NULL, PRIMARY KEY (C_ID) )
table is created

Thursday, 23 November 2017

JAVA Program to generate OTP

Otp.java

package java8s;

import java.util.Random;

public class Otp {
     static char[] sendOtp(int length){
          System.out.println("your otp for this transaction is:");
          String numbers="0123456789";
          Random r=new Random();
          char[] otp=new char[length];
         
          for(int i=0;i<length;i++)
          {
              otp[i]=numbers.charAt(r.nextInt(numbers.length()));
          }
          return otp;
     }
    
    

     public static void main(String[] args) {
         
          System.out.println("generating otp....");
          System.out.println(sendOtp(4));

     }


}

Output 
generating otp....
your otp for this transaction is:
7176

Wednesday, 1 November 2017

How we can sort primitive types or Object array and list with a simple JAVA program ?

package com.java8s.sort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class JavaObjectSortingExample {

    /**
     * This class shows how to sort primitive arrays,
     * Wrapper classes Object Arrays
     * @param args
     */
    public static void main(String[] args) {
        //sort primitives array like int array
        int[] ar = {50,100,40,30};
        Arrays.sort(ar);
        System.out.println(Arrays.toString(ar));
       
        //sorting String array
        String[] arr = {"A", "C", "B", "Z", "E"};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
       
        //sorting list of objects of Wrapper classes
        List<String> l = new ArrayList<String>();
        l.add("A");
        l.add("C");
        l.add("B");
        l.add("Z");
        l.add("E");
        Collections.sort(l);
        for(String str: l) System.out.print(" "+str);
    }
}

Output
 [30,40,50,100]
 [A,B,C,E,Z]
  A B C E Z