New Java Puzzler Found while reading Java Puzzler book

Today, while solving puzzles from Java Puzzler book I myself created a new Java Puzzle. So, in this blog I am writing about that puzzle.

Puzzle

Will the code given below results in an infinite loop.


public class MyJavaPuzzle {

 public static void main(String[] args) {
 Double i = Double.NaN;
 while(i != i){
 System.out.println("Infinite Loop");
 }
 }
}

Solve this puzzle and have fun. Happy Puzzling!!!

Post your answer and explanation in comments.

What we should not write on company’s blog?

These days organization expects their employees to write blogs/publish articles so that the organization gains more visibility. I believe blogs are a very good way by which you can gain more popularity and visibility in the developer’s community.  But I see lot of blogs published on company’s official blog as  developer’s personal experience or personal learning. These blogs lack the content and value as expected from a company blog. People just write the blog for the heck of writing it.

In my opinion writing such blogs does not increase the organization visibility.There should be a difference between what you should write in your personal blog to what you should write in an organization blog.

The blogs that you should write on a personal blog rather than on company blog are:-

1) Hello World blogs = These type of blogs does not provide any value as you can find such blogs all over the web. They just contain a very basic explanation of the concept or framework with or without a Hello World program. These blogs have one characteristic that the person who wrote this blog will also never read it again.
2) Opinionated blogs = These type of blogs contain your opinion about a topic, concept or an event. Writing opinionated blogs is not bad but
if you are writing your opinion on the company’s blog then it in a way represent your organization opinion also. So, when ever you write an opinionated blog, please match it with your organization values.
3) Blog with links to other blogs/articles= These blogs does not have any content of their own they just point to other links (video or text).

These are some of the types of blogs that should not be written on an official blog. Company’s blog should have good content and quality should matter over number of blogs published. Please post in your comments on what you think.

Reasons for incompetent software developers in India

Reasons for incompetent software developers in India

Most of the times, I have heard that Indian developers don’t have the quality as compared to their counterparts who are working in western countries.  Development teams in western countries often blame their offshore counterparts for slowing them down.  It has been said that Indians are not technically competent; write poor code, they don’t give any suggestions for the problems, etc.

In my opinion, most of these are true. Yes, we are not at par with developers in western countries and we sometimes really suck. Please note that this is just my personal opinion and not all software developers in India are bad. It’s the problem of quantity versus quality. In this blog, I will be putting up some reasons why I feel Indians developers lag behind developers from other countries.

Reasons

  1. Developer by chance not by choice = In India anybody can become a software developer whatever his/her qualification is. I, myself, was a mechanical engineer, but in the college campus was recruited by a Software company so I ended up becoming software developer. Likewise I have so many friends who become software developer by chance. Most of the college students who join any Software company does not know anything about software development or have any knowledge about programming.
  2. College education does not help = I have graduated from one of the good college in India but I can tell you one thing that the quality of education in India is very poor whichever college you get graduated from. In India, importance is given to marks than to practical learning, students just cram the things and get score but practically they know nothing. I recently interview a guy who had close to 6 years of experience, graduated from a good college in Computer Science with a very high percentage, was not able to write a Fibonacci series program.
  3. Developers don’t keep themselves updated = If you ask a developer which last technical book you read or how you keep yourself updated, most of the times you will not get any answer. Nobody wants to learn or improve themselves. Whenever I get a chance to speak to developers I ask which last technical book they have read 99% of the time answer is either none or Head First SCJP book.
  4. Everybody wants to become a manager = In India you can become team leader at 5 years of work experience. Once you become team leader, your next goal is to become manager and for becoming manager you need to be good at giving your work to others, doing dirty politics, and most important doing nothing. So, you can see, we do not know anything about programming when we enter the software development world and at an experience of 5 years most of the developers start thinking about becoming manager. Last week I was giving Java 8 training to a set of developers with average experience of less than 4 years and I asked a question how many of you think you will be coding 5 years down the line. No one raised their hand. I was shocked to see this.
  5. No contribution to open-source community = I don’t know any of my friends or friends of friends including me who has contributed to open-source community.  We can only use the open-source project and if we find any bug in the project we will never fix it but blame the developers who wrote the code.

There can be more reasons but at this point of time I can think of these 5 only.  I am trying to make myself a better developer by reading, writing, listening. Tell me what you guys think?

Maven classpath ordering lesson learnt

Today, I was working on a user story which spans across two modules(A and B) of our project (C).  As we follow test driven development, so I first wrote a test for the functionality that I need to add in module A and then wrote the piece of code(TDD is not the topic of this blog so please don’t go in detail).  The test passed with the green bar and i moved to the B module. I wrote a test and then  wrote a piece of code but this time test failed with error NoClassDefFoundError: org/objectweb/asm/CodeVisitor . I was a bit surprised why in one module A test is passing and in module B test is failing because both the projects had similar dependencies.

After googling, i found out that this error comes because hibernate has cglib-2.1_3.jar as the dependency which uses older version of asm jar which was having CodeVisitor class. CodeVisitor class has retired and does not exists in newer version of asm jars.  But now the issue was why junit testcase was passing in A module. To find why test case in A module pass I did the maven dependency check on both module using


mvn dependency:tree

In module A, I found out out that it loaded cglib-nodep-2.1_3.jar not cglib-2.1_3.jar . cglib-nodep-2.1_3.jar was loaded because easymockclassextension has dependency on cglib-nodep jar.

In module B, I found out that it loaded cglib-nodep-2.1_3.jar not cglib-nodep-2.1_3.jar. This jar was loaded because hibernate has dependency on cglib jar.

Now the problem was why in A module cglib-nodep jar is loaded but in B module cglib jar is loaded. I looked at the pom.xml and found out that in A module easymockclassextension dependency was declared before the hibernate dependency

<dependencies>
 <dependency>
 <groupId>org.easymock</groupId>
 <artifactId>easymock</artifactId>
 <version>2.5.2</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.easymock</groupId>
 <artifactId>easymockclassextension</artifactId>
 <version>2.5.2</version>
 <scope>test</scope>
 </dependency>

 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.15</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-core</artifactId>
 <version>3.3.2.GA</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-annotations</artifactId>
 <version>3.4.0.GA</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-commons-annotations</artifactId>
 <version>3.3.0.ga</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-entitymanager</artifactId>
 <version>3.4.0.GA</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>

In module B pom.xml easymockclassextension dependency was declared after the hibernate dependency.

<dependencies>
 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.15</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-core</artifactId>
 <version>3.3.2.GA</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-annotations</artifactId>
 <version>3.4.0.GA</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-commons-annotations</artifactId>
 <version>3.3.0.ga</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-entitymanager</artifactId>
 <version>3.4.0.GA</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.easymock</groupId>
 <artifactId>easymock</artifactId>
 <version>2.5.2</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.easymock</groupId>
 <artifactId>easymockclassextension</artifactId>
 <version>2.5.2</version>
 <scope>test</scope>
 </dependency>
 </dependencies>

Because easyclassextension was declared before the hibernate dependency, cglib-nodep was getting loaded hence test was passing.

In this way, I learn that in maven dependencies are loaded in the order they are mentioned in pom.xml. As of version 2.0.9 maven introduced deterministic ordering of dependencies on the classpath.The ordering is now preserved from your pom, with dependencies added by inheritence added last.

Finding all the indexes of a whole word in a given string using java

Intent

To find all the indexes of a whole word in a given searchable string.

Motivation

Today, I had to write a piece of code in which I had to find all the indexes of a particular keyword in a searchable string. Most of the times, when we have to find index of a keyword in a searchable string we use indexOf method of String class.

For example,


String searchableString = “Don’t be evil. Being evil is bad”;

String keyword = “be”;

So, we can find the index of keyword as


int index = searchableString.indexOf(keyword);

This will give us the index of first occurrence of keyword (“be”). Now suppose, we have to find all the indexes of keyword (“be”), we will have to loop over searchableString and find all the indexes of keyword “be”.

This can be done as follows

	public static void findIndexes(){
		String searchableString = "don’t be evil.being evil is bad";
		String keyword = "be";

		int index = searchableString.indexOf(keyword);
		while (index >=0){
			System.out.println("Index : "+index);
			index = searchableString.indexOf(keyword, index+keyword.length())	;
		}

	}

	public static void main(String[] args) {
		findIndexes();
	}

This program will print :

Index :  6

Index : 14

There is a problem in this code as we should not get “Index : 14” because we are looking for a keyword “be” and the index it has found is from word “being” . This just looks for any occurrence of keyword in the string it does not have to be a whole word. To solve this problem, we will be writing a solution using Regular expressions.

Solution

We have to find out the indexes which correspond to the whole word. For example, we should only get “Index : 6” as that corresponds to the keyword “be” not “Index : 14” as it corresponds to the word “being” .

This can be done as follows :-

WholeWordIndexFinder.java

package org.dailywtf.string;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WholeWordIndexFinder {

	private String searchString;

	public WholeWordIndexFinder(String searchString) {
		this.searchString = searchString;
	}

	public List<IndexWrapper> findIndexesForKeyword(String keyword) {
		String regex = "\\b"+keyword+"\\b";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(searchString);

		List<IndexWrapper> wrappers = new ArrayList<IndexWrapper>();

		while(matcher.find() == true){
			int end = matcher.end();
			int start = matcher.start();
			IndexWrapper wrapper = new IndexWrapper(start, end);
			wrappers.add(wrapper);
		}
		return wrappers;
	}

	public static void main(String[] args) {
		WholeWordIndexFinder finder = new WholeWordIndexFinder("don’t be evil.being evil is bad");
		List<IndexWrapper> indexes = finder.findIndexesForKeyword("be");
		System.out.println("Indexes found "+indexes.size() +" keyword found at index : "+indexes.get(0).getStart());
	}

}

IndexWrapper.java

package org.dailywtf.string;

public class IndexWrapper {

	private int start;
	private int end;

	public IndexWrapper(int start, int end) {
		this.start = start;
		this.end = end;
	}

	public int getEnd() {
		return end;
	}

	public int getStart() {
		return start;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + end;
		result = prime * result + start;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IndexWrapper other = (IndexWrapper) obj;
		if (end != other.end)
			return false;
		if (start != other.start)
			return false;
		return true;
	}

}

Explanation
First of all a regular expression is built using the keyword. Regex \b defines a word boundary. \b allows you to perform a “whole words only” search using a regular expression in the form of \bword\b.
More information on regex could be found at this link.  Next, we just iterate over the matcher till matcher can find any match. Whenever a match is found an wrapper object is created which contains the start and end position of the keyword.

Using the above code, we can find all the indexes of a keyword in a searchable string.

Anonymous Blogger

Anonymous Blogger

Today, I was thinking why I and many other bloggers write as anonymous blogger without disclosing their correct identity.   I am writing down some reasons which lead me to write in an anonymous manner:-

  1. Fear of not looking stupid: – I feel this is the number one reason why people hide their identity.  This can be related to a blog which does not make sense or poor code examples.
  2. Writing on the topics which you can’t write otherwise: – There are topics on which you don’t want to write with your correct identity like your workplace environment, your manager  J , interview questions and many others. By hiding your identity, you can very easily do this.