Friday, January 25, 2019

Useful ways to working with Strings in Java

  •  When  we want to  print a  array  , We can use this  way
             String text  = "abcaada";
             System.out.println( Arrays.toString(
  text .split("a") )  );

             The string you give  split  defines a regular expression, so any characters special in regular 
             expressions have to be   escaped:
       String[] parts = string.split("\\$@");
  • When we want to convert   List <String>   stringListValues    , to  string value  . We can use following way
            String commaSeperatedStringListValues    =  String.join(  "','"    ,    stringListValues    ) ;
  • When we have string  values set such as   String   names  =  "Saman , Kumara , Ramanayaka "   ;     and  we  want 
            to  separate that  string values from " , " and put into  a  array , we  can use

           String[]  nameArray  =   names.split( " , " ); 
  • String newStringValue = String.join(  "-> ", "Wake up", "Eat", "Play", "Sleep", "Wake up"  );
           System.out.println(  newStringValue );
   
         Output will be :   Wake up-> Eat-> Play-> Sleep-> Wake up
  • To reverse a string

    String  stringObj=  "Hiiiiii  Kamlal";
    StringBuffer  stringBufferObj =  new  StringBuffer( stringObj );

    stringBufferObj.reverse();

 Version : - 1.1.0










Tuesday, January 22, 2019

Singleton Class in Java

In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time.
After first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created. So whatever modifications we do to any variable inside the class through any instance, it affects the variable of the single instance created and is visible if we access that variable through any variable of that class type defined.
To design a singleton class:
  1. Make constructor as private.
  2. Write a static method that has return type object of this singleton class. Here, the concept of Lazy initialization in used to write this static method
public class Singleton {
    private static final Singleton instance = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
    }
}

In terms of practical use Singleton patterns are used in logging, caches, thread pools, configuration settings, device driver objects.
 
Configuration File: This is another  usage of Singleton pattern because this has a performance benefit as it prevents multiple users to repeatedly access and read the configuration file or properties file. It creates a single instance of the configuration file which can be accessed by multiple calls concurrently as it will provide static config data loaded into in-memory objects. The application only reads from the configuration file at the first time and there after from second call onwards the client applications read the data from in-memory objects

We can use the cache as a singleton object as it can have a global point of reference and for all future calls to the cache object the client application will use the in-memory object

Why can’t we use a static class instead of singleton?

  • One of the key advantages of singleton over static class is that it can implement interfaces and extend classes while the static class cannot (it can extend classes, but it does not inherit their instance members). If we consider a static class it can only be a nested static class as top level class cannot be a static class. Static means that it belongs to a class it is in and not to any instance. So it cannot be a top level class.
  • Another difference is that static class will have all its member as static only unlike Singleton.
  • Another advantage of Singleton is that it can be lazily loaded whereas static will be initialized whenever it is first loaded.
  • Singleton object stores in Heap but, static object stores in stack.
  • We can clone the object of Singleton but, we can not clone the static class object.
  • Singleton can use the Object Oriented feature of polymorphism but static class cannot.

References Used :- singleton-class by geeksforgeeks 
                               dzone.com- singleton-design-pattern

 Version :- 1.1.0

Friday, January 11, 2019

May be Useful .......

  • BeanPropertyRowMapper 
                In Spring 2.5, comes with a handy RowMapper implementation called ‘BeanPropertyRowMapper’, which can maps a row’s column value to 
                a property   by matching their names. Just make sure both the property and column has the same name, e.g property ‘custId’ will match
                to    column    name     ‘CUSTID’ or  with underscores ‘CUST_ID’.

        String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";
 
        Customer customer = (Customer)jdbcTemplate().queryForObject(  sql, new Object[] { custId }, 
        new BeanPropertyRowMapper(Customer.class));

               References Used :- spring-jdbctemplate-querying-examples 
  • log4j    vs  System.out.println()                                                                                                                                              Anything that you print to System.out will go to "standard out", and while you can redirect standard out to a file and compare it, what have you, that is very inflexible. Additionally, you cannot filter what goes to standard out if you use System.out... everything will be printed. With log4j, you can set different logging levels, so that logging messages that are below a certain severity/importance threshold are not printed (e.g. if you change the logging level to WARN, then DEBUG and INFO messages will not be displayed anymore).                                                                                                Additionally, log4j allows logging to be controlled on a class-by-class basis, whereas System.out can only be controlled at the granularity of the entire application (if you redirect System.out, you redirect it for the entire program). By contrast, each logger in log4j can be given a different appender. In addition, you can give a log4j logger multiple appenders (so that it goes the system logger, and over the network, for example). You can even have a log4j logger append to a StringBuilder, so that you can easily read what was written. And while System.out can be redirected, this redirection tends to be fairly limited; System.out can be redirected to a file or to a pipe (to another program), but you wouldn't be able to redirect it to a URL, for example; by contrast, it would be very easy to create an appender that transmits logging messages using HTTP POST.                                         References Used :- log4j-vs-system-out-println

  • Difference between a Web Server, an Application Server, and a Database Server       
     https://qph.fs.quoracdn.net/main-qimg-34951fd1d1dcdd807bdb3d50c0e19d2d                                                
  1. Web Server: Web server handles web requests sent by visitors visiting your website(primarily respond to HTTP / HTTPS requests). Web server runs on Apache, Nginx, Microsoft IIS, etc.
  2. Application Server: Application server is the server that works between Web server and database server and basically manages, processes the data.Application server can contain web server in them. For example :- JBoss, WAS
    Application server provides following features over web server :-  Connection pooling , Transaction management , Messaging , Clustering , Load Balancing
  3. Database Server: Database server handles database queries. It runs on MySQL, PostgreSQL, MariaDB, etc
              References Used :-What-is-the-difference-between-a-web-server-an-application-server-and-a-database-server
                                         :-

  • Thymeleaf  is a modern server-side Java template engine for both web and standalone environments.
    Thymeleaf's main goal is to bring elegant natural templates to your development workflow — HTML that can be correctly displayed in browsers and also work as static prototypes, allowing for stronger collaboration in development teams.
    With modules for Spring Framework, a host of integrations with your favourite tools, and the ability to plug in your own functionality, Thymeleaf is ideal for modern-day HTML5 JVM web development     
                                                                         

  • WildFly Server , formerly known as JBoss AS, or simply JBoss, is an application server authored by JBoss, now developed by Red Hat. WildFly is written in Java and implements the Java Platform, Enterprise Edition specification. It runs on multiple platforms.                                                                                                                            
  • JSF  Current   (10th Jan 2019)  version is . 2.3  is the designated user interface standard for Java EE 8. It went final on 17 April 2017. JSF 2.2 is the user interface standard for Java EE 7 The most recent major release of JSF is 2.2.
 Version :-  1.0.1

The AI Driven Software Developer, Optimize Innovate Transform

  The AI-Driven Software Developer: Optimize, Innovate, Transform": AI Transformation in Software Development : Understand how AI is re...