Friday, 5 February 2016

Java - Interning

public static void main(String[] args) {

    Long long1 = new Long(1234);
    Long long2 = long1;

    // Does this compare references or actual values? 
    if (long1 == long2) {
        System.out.println("Equal");
    } else {
        System.out.println("Not Equal");
    }
 
    // Displays "Equal"
 
    long2 = new Long(1234);

    // Let's create a new object with the same value to see if this works.
    if (long1 == long2) {
        System.out.println("Equal");
    } else {
        System.out.println("Not Equal");
    }

    // Displays "Not Equal" 
 
    // Let's compare the Long wrapper with its equivalent primitive. 
    if ((new Long(1234) == 1234) && (Long.valueOf(1234) == 1234)) {
        System.out.println("Equal");
    } else {
        System.out.println("Not Equal");
    }

    // Displays "Equal" 

    long1 = Long.valueOf(1234);
    long2 = Long.valueOf(1234);

    // We've now used valueOf() instead of creating a new Long, just to see if it works. 
    if (long1 == long2) {
        System.out.println("Equal");
    } else {
        System.out.println("Not Equal");
    }

    // Displays "Not Equal"   
 
    // Numbers in the range [-128, 127] are cached. 
    long1 = (Long.valueOf(127);
    long2 = (Long.valueOf(127);

    if (long1 == long2) {
        System.out.println("Equal");
    } else {
        System.out.println("Not Equal");
    }

    // Displays "Equal" 

    // So let's prove it for numbers greater than 127. 
    if ((Long.valueOf(128) == Long.valueOf(128))) {
        System.out.println("Equal");
    } else {
        System.out.println("Not Equal");
    }
 
    // Displays "Not Equal" 
 }

Thursday, 4 February 2016

Java - Difference between System.gc() and Runtime.getRuntime().gc()

public static void main(String[] args) {
    // Some code here
    // Invoke the Garbage Collector here.
     System.gc();

    // Some more code here
    // Invoke the Garbage Collector again.
     Runtime.getRuntime().gc();
}
 
In the above code, both calls seem to invoke the Garbage Collector. So what is the difference between System.gc() and Runtime.getRuntime().gc()?

Looking at the API documentation for System.gc() it invokes Runtime.getRuntime().gc()which means they are the same and System.gc() is only a convenience method.

Java 8 - Syntax to represent Lambdas

Below is the syntax for representing a Lambda expression :
parameter(s) −> expression(s)
where
  • parameter(s) has the following rules:
  1. Optional type declaration
  2. Optional comma, only for more than one parameter
  3. Optional brackets, only for more than one parameter
  • expression(s) has the following rules:
  1. Optional curly braces
  2. Optional return keyword
As quoted from the Oracle site:

"Note that a lambda expression looks a lot like a method declaration; you can consider lambda expressions as anonymous methods—methods without a name."

Java 8 - List of New Features

Among the loads of features added in Java 8, a summary of the most significant features is provided below:
  • Lambda Expressions - enables you to treat functionality as a method argument
  • Method References - easy to read lambda expressions for methods
  • Default Methods -  new functionality added to interfaces
  • Stream API -  functional style operations on collections of elements 
  • Performance Improvement for HashMaps with Key Collisions
  • Java Tool Updates - New jdeps and jjs commands.
  • New Date Time API - New package for Date and Time
  • java.io and java.util Changes - Updates to these packages
  • Nashorn Javascript Engine
For a more detailed information on all the updates, follow this link to the Oracle site.

More information provided on my blog as well. Follow the Java 8 link in the Labels section at the bottom of this post for more information. Oh, and feel free to drop some comments, whether you find the posts helpful or not.

Happy coding!

Tuesday, 26 May 2015

ECHO in a Command Prompt

Contents:

Introduction

As per the DOS shell, the ECHO command brings up the following when you type HELP ECHO

C:\>help echo

Displays messages, or turns command-echoing on or off.
  ECHO [ON | OFF]
  ECHO [message] 

Type ECHO without parameters to display the current echo setting.

Open a new Command prompt window and try the following:

ECHO without parameters

C:\>echo
ECHO is on.

ECHO [ON | OFF]

To turn Echo Off, try the below
C:\>echo off
_
You would notice that the prompt with the current path has now disappeared. Test the setting by typing in echo and press Enter. You would see the below:
 
echo
ECHO is off.

To turn Echo off, type the below and press Enter. You should now see your prompt with the current path being displayed:

echo on
C:/>

ECHO [message]

  • ECHO Static String

On the command prompt, try the below:
C:\>echo Hello World
Hello World

C:\>echo "Hello World"
"Hello World"

C:\>echo 'Hello World'
'Hello World'


In short, anything that follows the Echo will be displayed. You dont have to quote it. If you add quotes, you notice that the quotes themselves are also displayed.
  • ECHO with Variables

If your using variables, which you might do in a Command Prompt, then the below is useful

C:\>set STRING="HELLO WORLD"
C:\>echo %STRING%
"HELLO WORLD"

If your using Environment Variables for instance:
C:\>echo %JAVA_HOME%
C:\Program Files\Java\jdk1.8.0_45
 


Thursday, 15 January 2015

Hibernate Errors - Unknown column 'column_name' in 'where clause'

Just thought I'd share this.

While writing your hibernate queries, if your Restriction and ProjectList contain the same column you could get "Unknown column 'column_name' in 'where clause'" something like below

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'y17_' in 'where clause'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1888)
    at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
    at org.hibernate.loader.Loader.doQuery(Loader.java:674)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2213)
    ... 86 more


My code is as below:

criteria.add(Restrictions.in("code", accommodationCodes)); 

and later
 
projectionList.add(Projections.property("name"), "name");
projectionList.add(Projections.property("code"), "code");


Basically when your restriction and projection contain the same column, you will see this error. To get around this, all you have to do is change

criteria.add(Restrictions.in("code", accommodationCodes));

to

criteria.add(Restrictions.in("this.code", accommodationCodes));

Thursday, 12 June 2014

Version Control/Revision Control - Introduction

We are thinking of migrating to GIT from Subversion, hence I thought let me include a guide to version control systems. I might in a later post also including the steps we have thought of for the migration hoping this might be a guide for someone


Coming back to what I would mention as part of this post, here's a quick guide to what Version Control is all about. Version control also know as Revision Control is the process of managing changes to any kind of information that is capable of changing or having revisions. The kind of information we deal with in the Software Industry could range from anything like Requirements Documents, Design Documents, Software Code, Test Case Documents, System Configuration Information and a lot more.

So how do we deal with different versions of documents and if many different people in the team are using the same document and updating the same section of the document. For example, if you had 2 developers working on the same piece of code and writing 2 different bits of functionality which affected the same method? How would you deal with the changes every time you need to release the code. You would have to do individual merges of code from everyone's machine and retest different bits of functionality to ensure nothing's broken.

All of this can be automatically taken care of if you were using a centralised repository, where every time someone makes a change, all you have to do is sync your local code with the repository version and you've pulled in the latest code. If there is a conflict with the same sections being modified by 2 or more different people, the changes have to be merged in manually. But usually the job becomes easier if your using diff tools to compare the differences.

There are other advantages for the Release process while using Version Control systems but I won't go in there in this post. It's more about the branches and tags as part of the Release process but that could be a separate topic altogether. 

So there are various Repository tools which support Revision Control or Version Control. Some examples of the most common ones are

Microsoft Visual Source Safe - Licensed cost
IBM Rational ClearCase - Licensed cost
CVS - Free
Apache SVN - Free
GIT - Free

In addition most suites provide for version control. Spreadsheet and document tools have an inbuilt versioning control system, which allows for changes between multiple users. They also do provide a mechanism for merging in 2 or more versions of the document into a single one. Since the repository may not have any inbuilt mechanism for comparing various kinds of documents, the merge tools are usually third parties for the various kinds of documents that you may have. The merge is usually not a functionality that the Repository provides.The document once merged and updated  can then be stored in the Repository that your using.

Hope you liked this short introduction to Version Control or Revision Control. Please feel free to leave any comments or feedback.