Sunday, October 26, 2014

String, StringBuffer and StringBuilder

With respect to C or C++ in java String handling is different. Java provides String class to represent Strings. For better memory utilization java treats String as constant and their value can't be changed.
This behavior is popularly known as immutable.The idea is if some one write
String hello = "Hello"; 
String greetings = "Hello";
Internally both refer to same object. In JVM memory area there is a dedicated memory for these String literals, known as String pool.
So in our example hello == greetings will return true.
But if someone write
String str = new String("Hello");
It will create a new object, it won't point to same literal available in string pool.
So hello == str will return false. 
Another advantage of java String is we can use + for concatenation.

String greetMe = hello + "Java";

We can concatenate diffrent type of object other than String with String too. The typecast is automatic.String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.
As we learned String objects are immutable means the values cannot be altered. So strings are always thread safe. Any operation such as hello.toLowerCase() will result another object. For extensive String manipulation java provides two other classes StringBuffer and StringBuilder. These classes gives us a way to manipulate String efficiently. The only difference is StringBuffer is thread safe while StringBuilder is not. So by design StringBuilder is faster than StringBuffer.

So when to use what

String is used to handle multiple literal, it gives you optimum way to handle duplicate literal for better memory utilization.

StringBuffer is natural choice if we need to do a lots of String manipulation in a thread safe way. If synchronization is not the issue StringBuilder should be used since its faster than StringBuffer.

Have fun with your Strings  ;)

Wednesday, June 11, 2014

Future of Enterprise Applications Development

This is something I came to know while working with Weblogic for past few years. Over the years weblogic has developed quite a bit from one of its oldest version 6 (formerly known as BEA and before that known as Aqualogic) to Weblogic 12 (currently known as Oracle weblogic).

David Moyes might have left Manchester United in a mess this year (2013-2014), but oracle has done its bit to enhance Weblogic for its betterment. The most stable version currently in industry standard is 11g (10.3.6). Forgive me for showing my untiring effort to link Football to Weblogic, which are no way related, but one more expensive player bought on board by Oracle is Sun Java. And as a branch of Oracle Java is growing very fast and although Java 6 is considered as an industry standard now, there is already a new version of Java 8 already released in March 2014.

Now, naturally the question is where am I leading you to? The point I am trying to make is, the version of Java that comes for the latest Weblogic is still Java 6. Although you can use Java 7 in weblogic 12c (http://docs.oracle.com/cd/E24329_01/doc.1211/e24492/jdk7.htm), as of now we haven’t seen Weblogic using the full potential of the new Java versions. All features available might not be of use, but features like new concurrent Garbage Collector , improved compiler warnings etc would mean a vastly improved Weblogic application server with less and less memory issues for heavy enterprise applications.

And the latest in this is Java 8 which boasts of a very small JVM of 3MB. These features combined with the new improved weblogic GUI, security features gives enterprise applications a huge boost in terms of performance and ease of handling. What I am looking forward to is coming days with a complete package having the latest version of all the products required for enterprise application development. A Language (Java),an Application Server (Weblogic), a database (Oracle Database) and an IDE (NetBeans). This might act as a Commercial Of the Shelf (COTS) product for oracle and would standardize the process starting from development till deployment. Although this might sound as taking away the flexibility of the individual options that you can have but it is more to make an industry acceptable standard for enterprise architecture while free lancers and open source community continuing their adventure to explore new avenues and improve the standard in future.


Saturday, May 24, 2014

Do it my way ...

Every body like to do their work in their own way, and others to follow their way. If not everybody, certainty most of the people. The art of making others doing the work in your way is called managerial skill, leadership etc .. No need to worry I am not getting some sort of management concepts.
We programmers  do copy the real world arrangement and behaviors to solve software designing problems. So the idea of one program,module and class controlling the way other behave is quite natural. Dependency injection is the well known way of doing such thing.
In Java it used to happen through interface and abstract class. Anonymous class is widely used to define the concrete class for such interface or abstract class.

JButton testButton = new JButton("Test Button");
     testButton.addActionListener(new ActionListener(){
     @Override public void actionPerformed(ActionEvent ae){
        System.out.println("Click Detected ...");
       }
     });

In the above code we are creating an anonymous class which extends  ActionListener. This is a commonly used technique. In Java 8 this whole concept is more formalized into lambda expression. We can re-write the previous example

JButton testButton = new JButton("Test Button");
testButton.addActionListener(e->System.out.println("Click Detected ..."));

Lambda expression simplifies the code significantly. Lambda expression is used when we have to implement a functional interface. Functional interface means interface with only one method to be implemented.
So the advantage of lambda expression over anonymous class is it reduce the lines of code.

Happy Coding ;)

Thursday, September 26, 2013

Encrypted JDBC connection with Oracle Database

To protect sensitive data encryption is the standard way. Its highly recommended if you are dealing with sensitive data, the application database connection also need to be encrypted. In this article we will go through how to establish a secure JDBC connection to Oracle database.

 1) We need to turn on encryption in oracle server. We can do that by starting Oracle Net Manger. There we have to select Oracle Advance Security and in Encryption tab there is option to turn on Encryption for server.

2) Once the encryption for server is turned on. Next is how to establish a secure connection from Java.
JDBC driver has the capability to establish a encrypted connection. we have to pass necessary information as part of properties to do so. Here is documentation for that.

In this way we can establish encrypted connection. To test whether its working or not, we have to install some network sniffer tools. By that we can verify the exchanged data is encrypted or not. Another way is by turning on the tracing in Oracle Net Manger for network communication. And to check those trace for network traffic data is encrypted or not. 

Saturday, August 3, 2013

Internationalization Of af:query

I found ADF af:query is very useful. It provide out of box search and reporting capability. Typically we define some search criteria in View Object, and we use that criteria as af:query component.
All the labels we see in the page can be internationalized by using resource bundle in normal way.
For some labels we can use the below properties of af:query
                              ·         headerText
                              ·         resetButtonText
                              ·         saveButtonText
                              ·         searchButtonText
“Advanced” and “Saved Search” automatically take care. In fact all these labels are automatically translated as per locale. Only to support the language for which translation is not available or you want to have your customize label you can use above properties.
And rest of the labels can be customized or internationalized by using resource bundle configured for application module. To do so we need to modify the attribute of view object, In Control Hints section, Label Text filed can be modified to point right message key from the bundle.


Saturday, July 6, 2013

ADF Tree Table javax.faces.model.NoRowAvailableException

Usually this issue appears with Tree Table, when we modify the model associated with the tree. The UI state (disclosure and section information) is captured corresponding UI component object. When the model is updated those state become invalid. And when the tree try to restore the old state with new set of data then there could be a situation where this NoRowAvailableException can occur.

To avoid this better to clear the selection when model is updated. And then immediately partial refresh the tree .

I had used something like this to clear.

 if (treeTable != null && treeTable .getDisclosedRowKeys()!=null ){
        treeTable .getDisclosedRowKeys().clear();
    }

JBO-26001: NoXMLFileException

This exception comes in ADF BC projects. In my case <project>.jpx file was not part of deployment.
Run time BC needs the .jpx, As it is not able to find it throw JBO-26001: NoXMLFileException exception.
To make sure .jpx is part of deployment. Right click on project under deployment section check whether its included or not. If its not included add it.
Hope it solve the issue.

Saturday, April 13, 2013

Hashcode and Equals Method


It’s a common topic In  java interviews. And these two methods are always common to any class you write in Java. So let’s know them in and out.
boolean equals(Object  obj) and int hashCode() are two methods implemented in Object class with several other methods.  In Java every class implicitly inherit Object class so every class has these methods.  Equals method check the reference is holding same object or not.
MyClass obj1 = new MyClass();
MyClass obj2 = 0bj1;
Then obj1.equals(obj2) returns true.
MyClass obj3 = new MyClass();
In this case obj1.equals(obj3) will return false.
It means original equals method check whether two reference contain same object or not. If two object are logically similar, for ex like wrapper class Integer, Boolean etc we need to override equals method.
Boolean b1 = true;
Boolean b2 = true;
b1.equals(b2)  will return true. 
To overload equals method programmer may chose to compare field by filed.
Public class Employee
{
Integer employeeId;
String name;
public  boolean equals(Object obj)
{
 if (obj != null && obj instanceof Employee)
{
 Employee obj2 = (Employee) obj;
 return (obj2.getEmployeeId() == null ? this.employeeID == null : 
 obj2.getEmployeeId().equals(this.employeeID))
&& (obj2.getEmployeeName() == null ? this.employeeName == null : 
 obj2.getEmployeeName().equals(this.employeeName));  
}
return false;
}

Hascode method always return integer. Any two object equals return true always return same hashcode.  But its not necessary if two object produce same hashcode need to be equal.
Now here is the million dollar question if we override equals method do we need to override hashcode method. The answer is yes. We need to override hashcode method. It should satisfy two condition every distinct object produce different hashcode consistently and equals object will produce same hashcode.
Hashcode is important when we use HashTable, HashSet or HashMap in collections api. If we don’t construct the object in the above way, these collections may not work properly, performance can deteriorate.
So bottom line is always override hashcode when you override equals. J

Monday, November 26, 2012

What is the Big Deal about Big Data


Like every sector in our computer world we always have some new trends.  In fact the dynamics of changing trend in Computer industry is very evident. Few years back all want to adopt tablet and different form of post pc devices. And recent time Cloud was the big word. All most all companies have launched lots of cloud specific services or platform. Every blogger including me has written about cloud. And now the recent buzz is Big Data, Hadoop, Data Analytics etc.. 
There is a say Pen is mightier than Sword.  It means Knowledge, information is the key. If a student knows the right answer of the question in exam he will succeed. If the doctor knows more information about the disease he can diagnose and treat better. Similarly if a business knows has right information about its customer needs it can succeed well. So it’s important to get right information in time. And if we think most of the time that “information” is the only difference between success and failure.
So people invest huge to store data and retrieve/derive the useful information out of it in real time.  The whole process is divided in two steps first part is to store related data. And most of the time these are unstructured data. And the second thing is to analysis or processes those data to get right information in real-time. These unstructured data can come from or around your business.  For example any ecommerce portal will like to store and analysis its visitor browsing pattern. This will help them to arrange or present their products efficiently. Similarly an bank should like to know what its customers like, dislike or spending habit. So that it can customize its product as per it. More or less all big corporate houses are started spending in BigData and Analytics.
Well Data and Analytics not only help big organization. It can be useful  to stream line personal finance too. Money manager from Intuit is an example in this direction. It tracks all spending, and categorize them. So that one can easily track income and spending.  Its early days, In coming days we will see more product or services around Analytics and BIgData.  

Friday, July 20, 2012

Do I need to google ?

To find anything in internet the best way is googling. Any one who use internet the first thing they learn is googling. When I say googling it include all web search like Bing,Yahoo etc. I do googling to read news, to watch video, to check weather, movie sports etc.Since information is scattered in different website, and they are too many to remember. So people use search engine to locate their content. Few years back everything was working in this pattern, we used to search stuffs on web and browse our content in random site. And search engines used to insert some sponsored links, advisement  in the web pages or in search result to earn their revenue.

In last few year few major things happened one is evolution of post pc devices such as iPad,iPhones and other android devices. Its clear that tablet sale is outpacing PC sale. If we combine tablet + smart phones the number will exceed the PC sales. It means more and more people are accessing internet through their post PC devices. In post PC devices poeple connect to internet through different apps. For example to see weather, to read news, to book movie ticket I use my apps on phone. I don't do any web search for that. It doesn't mean web search is reduced to 0 in post pc devices, certainly it decreased dramatically. As pattern of  digital content consumption in post pc devices are quite different. And more or less every body agree that the future is all about post pc devices. Country like India a whole lots of people get introduced to digital world through mobile phones.


The inception of Facebook only resonate Human is a social animal. I will prefer to ask my friends or read post something from closed group instead of searching randomly in internet. People spend more and more time with each other at Facebook. It automatically reduced the need of google search.

Last one but not least. Evolution of "Siri" and similar services in line with it created lots of noise in the area of local search. Currently Apple announced  map service integrated with Siri and opened to lots of Social App like Facebook, yelp gives many useful information to the users interactively.

Its not that anything of above will replace search in future. Search will be there but it won't be first choice anymore it will be the last option.

Wednesday, June 13, 2012

Read me if you can

In my starting days of coding I had a notion if you don't understand the code means its a tough program, some hi-fi advanced code. And if the code compile with out error and execute properly means its perfect in all aspect.
My first programming language was C. There was a lab exam where we need to code 5 given program. I had completed well ahead of time, all the five were compiling and running properly. The teacher was checking each student's program and evaluating. You have to get minimum three programs right to clear the exam. The teacher came to me he read one by one program. He had checked 4 programs and found all are correct. Fifth one he saw the code, in first look he didn't get the logic he told its incorrect with out compiling or executing. Before I could explain back he had already deleted the source folders. I was bit upset because I knew I got five out of five correct. But it was recorded 4.  I was thinking I didn't get 5 out 5 just because some body was not able to read it.
And after I started working in real time project, I understand 90% time you will be working around the codes not written by you. And some point of time some one else will be working on your code for sure. And there is some figure floated on internet, as 80% effort goes in to understanding others code. All these made the point clear codes need to be written such a way that another person can understand it well. And sometime people prefer simpler algorithm instead a complicated one.
There are different best practices in line with various programming language. I found few points which help
1) Naming Convention
name of the variable and method  revel lots of information. for ex :
if ( authenticate(userName,password)
 {
  }
It clearly understood that we are authenticating by passing user name and password.
2) Comments
We all know we need to write comments. I feel Class label comments, method label comments are always handy. It should always tell more than code. I mean certainly by seeing code we get some idea, and comments should add some thing to it. For ex:
/* Check for  Ldap authentication */

if ( authenticate(userName,password)
 {
  }
In this case comments add some more info to the picture.
It is always nice to add few line of comments for a particular block of code.
3)  Use of parenthesis while doing logical operation.
parentheses always make easy to understand complex logical expression.

And the list goes on. You also can share some of the ideas here which helps understand the code. If the code is well understood it adds lots of value. And programmer can happily understand it and work with it. Happy Programmers always do wonderful things. ;)

Sunday, March 18, 2012

Life and Java both throws Exception

With each passing days we realize " man proposes and god disposes" , well we plan some thing if there is an deviation we called it exception. Exceptions are annoying and we just hate it. More or less each exception holds lots of vital information of reality,  to go forward we need to decode these exception. I have not much idea how to decode the exceptions that life throw, but in Java we can certainly plan it very well.
Java got a one of the advanced way to handle exception. To know more about exception handling framework in java follow this link.

I will focus how to design exception in applications. Designing of exceptions is start right from application design. Every time an error happens or something goes wrong, the ideal way is capture as much as information possible and notify some controller who can take alternate steps in run time. Mostly applications are integration of java classes and they perform the task my calling each other method. Consider if methodA of Class A use methodB of Class B and methodB encounter some error. Obviously methodB will not have any idea what could be the alternate flow. The best thing methodB can do is log the exception. Capture as much information possible about the error and wrap it to a customize exception to report to the methodA, methodA will have idea if methodB fail what to do. It should have the alternate plan ( exception handling block).
Mostly application are segregated into different layers like UI layer,  business layer and data access layer. So if an error happen in data layer business layer can take a decision can another operation is possible to carry out or need to report error to UI.And UI layer decide what kind of message need to displayed to the user.

Its always best practice to use application specific exception which wrap java native exceptions. So that it can contain more information about the error. And logging of these exception is very important. Logs help to track the error and the reason of error which gives the required information to fix the problem.
I hope with this we can plan better for unexpected flow in Java, And guys I need your inputs how to handle exception comes in life.


Friday, March 16, 2012

new iPad




After more than one year apple released new version of iPad. And they named it "new iPad". With in less than 2 years people around the globe accepted iPad as a new way of computing and in a lots of way iPad is changing life. Apple is successfully building the ecosystem and content by its app store, iTunes, iCloud and the latest iBook which makes iPad more and more useful.
The new iPad got lots of hardware up-gradation in compare to previous version iPad2. In original iPad Steve Job used to claim it gives a magical experience. And the  experience comes from display and touch. To make the magical experience further better, the new iPad got retina display. And this is big break through in terms of display. To complement it new iPad got a better processor, better camera and new generation antenna system.
In the same event apple had released new version of iWork, iPhoto and iMovie. And these are very powerful and simple apps which specially built for iPad. With apps each day iPad is adding more capability to itself. I am thrilled to see iBook. Reading textbook was never so fun before. It makes book reading more interactive.
I was anticipating new ipad will get Siri. Last year apple introduced Siri with iPhone 4s. which created a whole new way to interact with the system. According to me that is the one this new iPad is missing. Its a great tablet with same old price. I just hope it will be in India soon in right price.

Friday, February 24, 2012

All about imports in Java

In any Java source file first line after package declarations are import statements.  These statements enable us to use different classes from outside package. Compiler always looks at the import statements to find class definition.
Instead of import statements we can write fully qualified name. For ex:
java.io. File file = new java.io.File(filepath);
This is a perfectly valid statement. Only problem is it looks littlie complicated to read.  Good codes are not only run well but also easy to read.
So we will stick to
File file = new File(filepath);
To do so we have to write import statement at the beginning right after the package declaration.
package mej.java.example;
import java.io.File;
If we use more than one classes or interface of java.io packages we need to write multiple import statements.
package mej.java.example;
import java.io.File;
import java. FileInputStream;
or we can use wild characters like
java,io.*;
At first impression it looks simple, there are few side effects with this. It takes down the performance of compiler, as compiler has to look whole package for match.  But it doesn’t affect runtime performance of code in anyway. Many time developers misunderstood it.  There can be cases if we use multiple wild character imports. And if both the package contains class with same name, compiler will not able to resolve. And it also reduces the readability of code. It’s difficult to read which class from which package is being used. So the best way is to use import individually for each class or interface.
Another type of imports are static imports. To use it I can say something like
import static java.lang.Match;
We can directly write  ceil(4.83) instead of Math.ceil(4.38).  By static imports we can directly use the methods or members instead of referring through their class name. This is widely used in junit test cases to refer Assert class methods.
In this case also too much use of static imports decrease code maintainability.  
Please let me know your thoughts about java imports.

Thursday, February 16, 2012

localhost to www




Here we will discuss how to host your web application in internet. Once the application is built and running on your local machine and the testings are over next step is to take it to internet.
First we need a domain name, like for my blog I use www.myexperimentswithjava.com. To check and book your preferred domain there are lots of service providers are available for example: google apps and  godady
Domains are normally sell for 1 year , 2 years or  for some fixed terms.
If the domain name what you are looking for is taken by some one. And still you want it you can try for resale, godady facilitate resales of domain. But normally these are expensive. With domains usually service providers give some free email account, which you can use for business purpose like admin@yoursitename.com.
Once you are ready with the domain. Next thing is the Web servers,application server and database server.
Depending upon the load and traffic you expect these three can be in one machine, or individual machine or for each multiple machine. If you expect more than 1000 users are going to access then professional consultancy is required.
If you have decided to host in a share resource (popular name is cloud), you can check for service provider. I had used Amazon elastic cloud, which is pretty good. For dedicated servers you need to buy physical hardware and the set it up to run it. It should have good connectivity and fixed IP. Now next thing is that to set up each layer. I prefer first to install data base, configure security setting backups. And then create application specific objects and load initial data. Then install application server and install the apps. Check the connectivity with database. And some testing with apps. Once the application looks fine, configure the app server to run on port 80.  Then by just typing the IP of your application server you should able to access your application on internet. And the application server IP must be a static one.

Next we have to configure our domain name to application server.  From domain service provide we will get a control panel to manage the domain. There we can set redirect, configure sub domain etc. In controll panel there will be an option to add MX record. We need to add two entry like for ex:
1) http://myexperimentswithjava.com
2) www.myexperimentswithjava.com

and these entries need to map to application server.
After the configuration it takes some time to work. Normally in a day or two your application can be reachable on internet by your domain.

All the best for your hosting experiments, do post your experience.

Friday, January 13, 2012

Something is in Air

If some on follow computers actually (nowadays its other way computers follow us) must heard about cloud. And the computer guys always copy the terms from real world to use. Like Inheritance, Cloud , Mouse etc ... sometime the original meaning of these words must be thinking to sue for copyright violation.
Well copying and stealing is never a problem in this industry. It is always appreciated.
"Cloud" is such a term today no one can afford to miss it. Companies, Developers and Customers  everybody like to have it.
Well lets understand what does Cloud means and how it make sense. To start we can say your computer is connected to another computer. Then networking is there from more than last 20 years. Each year its becoming more powerful. Computer can connect with each other they can send data and receive from another computer. The next thing was application which run on network by using resource of multiple computer. Web applications are fall in this category. They run in server and user can work with it remotely.
The service providers are hosting the service in server and can cater to multiple user, that brings down end user cost of using service. The application is customized as per users preference. This is something called application on cloud. Actually it is nothing but same web application. And it is customized as per user or groups of user.
Another thing is Computer infrastructure or platform on cloud. Its like you will able to use remote server, database or application server. Amazon, Oracle and Google provide these kind of service. Here the application developers can use these platform to develop, test and host. And its easy to scale as per usage. So its a very cost effective way of using resource. Still there are some concerns with data security, organisations are comfortable to store there important data in remote data center.
And another third category of cloud services are kind of hybrid between platform and application, Salesforce, Oracle public cloud and Apple's iCloud are such example. These gives developers platform to write application on cloud.
Certainly for medium/small scale industries can use cloud services for their IT needs. It enable to have good IT system in lower price.