Monday, January 23, 2012

Heap Memory In Java


When Java program executes, the JVM uses Heap memory to manage the runtime  data. If your program required more than what memory is allotted then we get OutOfMemoryError.

The heap size may be configured with the following VM options:
·         -Xmx<size> - to set the maximum Java heap size
·         -Xms<size> - to set the initial Java heap size

For example, you can set minimum heap to 126 MB and maximum heap 512 MB for a Java program.





Eclipse:
In Eclipse, The eclipse.ini is a text file containing command-line options that are added to the command line used when Eclipse is started up.




Open the eclipse.ini (any Editor, here I am using Notepad++)



 The minimum size and maximum size can be changed  by using






   -Xms256m
   -Xmx1024m

Save the file.  This is the way you can increase heap memory size.

Friday, January 20, 2012

Varargs Java 1.5


Varargs:

Varargs is  introduced from Java 1.5. by using varargs  Programmers can create methods that receive an unspecified number of arguments.

In method varargs can be declare as
                      
                                Data_type… variable_name


Example:
                         public void sum(int... a){
                       
                          }                

public void sum(int... a) indicates that the method receives a variable number of arguments of integer type.


Example:


package com.jsl.core;
public class VarArgsDemo {
           
                public int  sum(int... a){
                  
                        int res=0;
                      
                        for(int i=0;i<a.length;i++){
                                    res=res+a[i];
                        }
                      
                         return res;
            }
           
            public static void main(String[] args) {
                       
                        VarArgsDemo obj=new VarArgsDemo();
     
                        System.out.println("The sum of two variable is :"+obj.sum(10,20));
           
                        System.out.println("The sum of three variable is :"+obj.sum(10,20,30));

                         System.out.println("The sum of four variable is :"+obj.sum(10,20,30,40));
                       
            }

}


Output:

The sum of two variable is   :30
The sum of three variable is :60
The sum of four variable is  :100


Example:

package com.jsl.core;
public class VarArgsDemo {
           
public static void main(String... args) {
                                    for(String name:args){
                                                System.out.println(name);
                                    }
            }
}

java VarArgsDemo Jsl Tech Java Training

output
Jsl
Tech
Java
Training

The  varargs length  must be only one variable in method parameter list.

                        public void sum(int... a, float... b) it leads to compilation error,  Because in a method we can declare only one varargs length variable.


If the varargs length variable is combined with other variables, this varargs length variable should be last in the parameter list.

public void sum(int x,float y,int... a)


The method overloading with same type parameter in different length or passing array to a method can accomplished by using varargs.

Example:


package com.jsl.core;
public class VarArgsDemo {
      public int sum(int[]...a){
                  int res=0;
                  for(int i=0;i<a.length;i++){
                        for(int j=0;j<a.length;j++){
                              res+=a[i][j];
                        }
                  }
                  return res;
      }
     
      public static void main(String... args) {
         
     VarArgsDemo obj=new VarArgsDemo();
     
     System.out.println("The sum is : "+obj.sum(new int[][]{{1,2,3},
                        {4,5,6},{7,8,9}}));
      }
}

output :

The sum is : 45

Monday, January 9, 2012

JDBC Programming

Simple JDBC program


package com.miani.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class GetDeptData {
public static void main(String[] args) {

try{


         Class.forName("oracle.jdbc.driver.OracleDriver");
         Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jsltech","jsl");
         Statement st=con.createStatement();
         ResultSet rs=st.executeQuery("select * from dept");
         while(rs.next()){
                     System.out.print(rs.getInt("deptno"));
                     System.out.print(rs.getString("dname"));
                     System.out.print(rs.getString("loc"));
                     System.out.println("\n_______________\n");
     }
        st.close();
        con.close();
    }catch (Exception e) {
          System.out.println(e);
       }
    }
}


The program to store the image into the database

package com.miani.jdbc.psmt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;


public class BookInfo {


/*create table book_details(book_id number(6),book_image blob)*/


public static void main(String[] args) {
         
                   Connection con=null;
                   PreparedStatement ps=null;
                   ResultSet rs=null;
       try{
              
                   Class.forName("oracle.jdbc.driver.OracleDriver");
                   con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jsltech","jsl");
                   ps=con.prepareStatement("insert into book_details(book_id,book_image) values(?,?)");
                   File file=new File("java.jpg");
                   FileInputStream fis=new FileInputStream(file);
                   ps.setInt(1, 10001);
                   ps.setBinaryStream(2, fis,(int)file.length());
                   ps.execute(); 
           }catch (Exception e) {
                   e.printStackTrace();
     }
  }
}


The program to retrive the image from the database

package com.miani.jdbc.psmt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class BookInfo {


/*create table book_details(book_id number(6),book_image blob)*/


public static void main(String[] args) {


           Connection con=null;
           PreparedStatement ps=null;
           ResultSet rs=null;
try
{
                     Class.forName("oracle.jdbc.driver.OracleDriver");
                     con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jsltech","jsl");
                     ps=con.prepareStatement("select book_id,book_image from book_details where book_id=?");
                     ps.setInt(1, 10001);
                     rs=ps.executeQuery();
          
             while(rs.next()){
                        System.out.println(rs.getInt(1));
                        File f=new File(rs.getInt(1)+".jpg");
                        FileOutputStream fos=new FileOutputStream(f);
                        InputStream r=rs.getBinaryStream(2);
                        int i=r.read();
              
              while(i!=-1){
                          fos.write(i);
                          i=r.read();
                     }
            } 
}catch (Exception e) {
                   e.printStackTrace();
              }
         }
}

Tuesday, January 3, 2012

Abstract Classes and Methods


Abstract methods:

An abstract method is a method that is declared without an implementation, the declaration of an abstract method ends with a semicolon and the method with the keyword abstract.

public abstract class Animal {
                        public abstract void sound();
}


abstract method can’t be
                       
                                       native
                                       final
                                       static
                                       synchronized
                                       private
                                       strictfp                                                        

Abstract class:
   
An abstract class is class which contains the zero or more abstract method. if class is declared as abstract it can’t instantiated.

package com.jsl.core;
public abstract class Animal {
                        public Animal(String name) {
                                    this.name=name;
                        }
                        public abstract String sound();
                       
public String toString(){
                                    return name;
                        }
                        String name;
}

Animal class contains the concrete method as well abstract method. We can’t instantiate the Animal class.
  
The class can be declared as abstract, without abstract method. So it’s restricting not to create object.  

 public abstract class MathOperation {
                        public double multiply(double amount,double price){
                                    return amount*price;
                        }
}


If you want to access the multiply method another class that class must extends the MathOperation.


public class UserOperations extends MathOperation {
                        private double a=10,b=20;
                        public void showResult(){
                                    System.out.println(multiply(a, b));
                        }
}

An abstract class can implement the interface.

package com.jsl.core;
interface Game{
            public void play();
            public void makeSound();
}

public abstract class Car implements Game {
                        @Override
                        public void play() {
                                    ------
                                    ------
                        }
}

The abstract method can be called from the concrete method of the abstract calls.  

package com.jsl.core;
abstract class One{
    public abstract void print(int res);
    public void sum(int a,int b){
        int res=a+b;
        print(res);
    }
}
class Two extends One{

    @Override
    public void print(int res) {
        System.out.println("The result is : "+res);
       
    }
   
}
public class AbstractDemo {
        public static void main(String[] args) {
            One o=new Two();
            o.sum(20, 30);
        }
}

Example:

package com.jsl.core;
public abstract class Car{
                        public void start(){
                                    play();
                        }
                        public void play(){
                                    System.out.println("The play method ");
                        }
}

If the class extends the abstract class, then that class must implements all the methods otherwise that class will become the abstract class.

package com.jsl.core;
abstract class One{
            abstract public void start();
            public abstract void play();
            public abstract void stop();
}
public abstract class Two extends One{
            @Override
            public void play() {
            }

            @Override
            public void stop() {
            }
}

The abstract class can’t create object but it contains the constructor.

package com.jsl.core;
abstract class One{
            public One(){
                        System.out.println("The one constructor");
            }
}
class Two extends One{
            public Two(){
                        System.out.println("The Two constructor");
            }
}
class Three {
            public static void main(String[] args) {
                        Two obj=new Two();
            }
}

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0...