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

Comparable vs. Comparator in JAVA

When your class implements Comparable, the compareTo method of the class is defining the "natural" ordering of that object. That method is contractually obligated (though not demanded) to be in line with other methods on that object, such as a 0 should always be returned for objects when the .equals() comparisons return true.

Comparator is its own definition of how to compare two objects, and can be used to compare objects in a way that might not align with the natural ordering.
For example, Strings are generally compared alphabetically. Thus the "a".compareTo("b") would use alphabetical comparisons. If you wanted to compare Strings on length, you would need to write a custom comparator.


In short, there isn't much difference. They are both ends to similar means. In general implement comparable for natural order, (natural order definition is obviously open to interpretation), and write a comparator for other sorting or comparison needs.



Monday 18 September 2017

Lambda Expression : Replacement of Anonymous Inner Class

Lambda Expression:
·       A new feature of java8s
·       Main intension is replacement of anonymous inner class.
·       Lambda expression is nothing but an anonymous method(method without name)

The general form is :
(para-list)->
{
   //method body

};

Let's see an example for better clarity using normal interface approach, then using anonymous inner class approach, then lambda expression approach:

Approach-1:
package java8s;

interface Transaction
{
   public void withDraw(int amt);
}

class A implements Transaction
{
   public void withDraw(int amt)
   {
       System.out.println("amount withdrawn is"+amt);
   }
}
public class LambdaExpressionExample {

   public static void main(String[] args) {
      
       A ob1=new A();  
      
       ob1.withDraw(15000);
      
   }

}

Approach-2:
package java8s;

interface Transaction
{
   public void withDraw(int amt);
}



public class LambdaExpressionExample {

   public static void main(String[] args) {
      
       Transaction ob1=new Transaction()   //Anonymous Inner Class
       {
          public void withDraw(int amt)
          {
              System.out.println("amount withdrawn is"+amt);
          }
       };
      
       ob1.withDraw(15000);
      
   }

}

Approach-3:
package java8s;

interface Transaction
{
   public void withDraw(int amt);
}
public class LambdaExpressionExample {

   public static void main(String[] args) {
       Transaction ob1=(int amt)->
          {
              System.out.println("amount withdrawn is"+amt);
          };
ob1.withDraw(1500);
       

   }

}

Output:
amount withdrawn is1500