Sunday, March 27, 2016

How to increase length of existing VARCHAR column in SQL Server

You can increase the length of a VARCHAR column without losing existing data in SQL Server. All you need to do is that execute following ALTER TABLE statements. Though, you need to specify NULL or NOT NULL constraint explicitly, depending upon your data.

Here is the SQL command you can use to increase the length of a VARCHAR column in SQL Server:


This command increases the length of title column of Books table to 432 characters. You can use the same command to increase the length of CHAR, NCHAR or NVARCHAR columns as well.

Saturday, March 26, 2016

How to find the first element of Stream in Java 8 - findFirst() Example

In Java 8, you can use the Stream.findFirst() method to get the first element of Stream in Java. This is a terminal operation and often used after applying several intermediate operations e.g. filter, mapping, flattening etc. For example, if you have a List of String and you want to find the first String whose length is greater than 10, you can use the findFirst() method along with stream() and filter() to get that String. The stream() method gets the Stream from a List, which then allow you to apply several useful methods defined in the java.util.Stream class e.g. filter(), map(), flatMap() etc.

Friday, March 25, 2016

Java 1.5 Generics Tutorial: How Generics in Java works with Example of Collections, Best practices, Gotchas

Java Generics Tutorial
Generics in Java is one of important feature added in Java 5 along with Enum, autoboxing and varargs, to provide compile time type-safety. Generics is also considered to be one of the tough concepts to understand in Java and somewhat it’s true as well. I have read many articles on generics in Java, some of them are quite good and detailed but I still felt that those are either too much technical or exhaustively detailed, so I thought to write a simple yet informative article on Java generics to give a head start to beginners without bothering their head too much. In this Java generics tutorial, I will cover How Generics works in Java,  some mysterious wildcards in Generics and some important points about Generic in Java. I will try explaining generics concept in simple words and simple examples.

Thursday, March 24, 2016

Difference between Wait and Sleep, Yield in Java

The difference between wait and sleep or the difference between sleep and yield in Java are one of the popular core Java interview questions and asked on multi-threading interviews. Out of three methods which can be used to pause a thread in Java, sleep() and yield() methods are defined in thread class while wait() is defined in the Object class, which is another interview question. The key difference between wait() and sleep() is that former is used for inter-thread communication while later is used to introduced to pause the current thread for a short duration. This difference is more obvious from the fact that, when a thread calls the wait() method, it releases the monitor or lock it was holding on that object, but when a thread calls the sleep() method, it never releases the monitor even if it is holding. 

Top 10 Servlet Interview Question Answers - J2EE

This time its servlet interview questions, I was thinking what to pick for my interview series and then I thought about J2EE and Servlet is my favorite on that space. Servlet is an important part of any J2EE development and serves as Controller on many web MVC frameworks and that’s why it’s quite popular on J2EE interviews. These Servlet questions are based on my experience as well as collected by friends and colleague and they are not only good for interview practice but also shows a new direction of learning for anyone who is not very familiar with servlet technology.

Servlet interview question and answerAs said earlier this interview question article is part of my earlier series java interview questions , UNIX command interview questions and Java threading interview questions.

You can find answers to all these questions on google but I have also listed my answers for quick reference.

Difference between Stack and Heap memory in Java

The difference between stack and heap memory is common programming question asked by beginners learning Java or any other programming language. Stack and heap memory are two terms programmers starts hearing once they started programming but without any clear and definite explanation. Lack of knowledge of what is a heap in Java and what is stack memory in Java results in misconceptions related to stack and heap. To add to this confusion, a stack is also a data structure which is used to store elements in LIFO(Last In First out) order and available in Java API as java.util.Stack. In general, both stack and heap are part of memory, a program is allocated and used for different purposes. Java program runs on JVM which is launched as a process by "java" command. Java also uses both stack and heap memory for different needs. In our last article 10 points on Java heap space, I have touched base on Java heap space and in this article we will see the difference between stack and heap memory in Java.

Wednesday, March 23, 2016

How Garbage Collection works in Java

I have read many articles on Garbage Collection in Java, some of them are too complex to understand and some of them don’t contain enough information required to understand garbage collection in Java. Then I decided to write my own experience as an article. You can call it a tutorial about garbage collection in simple word, which would be easy to understand and have sufficient information to understand how garbage collection works in Java. Garbage collection works by employing several GC algorithm e.g. Mark and Sweep. There are different kinds of garbage collector available in Java to collect different area of heap memory e.g. you have serial, parallel and concurrent garbage collector in Java.

Tuesday, March 22, 2016

Top 10 basic networking commands in linux/unix

Networking is an essential part Unix and it offer lots of tools and command to diagnose any networking problem. When I was working on FIX Protocol we get a lot of support queries to see whether FIX Sessions are connected or not. Since FIX Protocol uses sockets you can use the netstat , telnet and other networking commands available in Linux for finding a problem and solve that.In this article, I will show you basic networking commands in Unix and for what purpose they are used. with the combination of grep and find command on them, you can troubleshoot most of the networking problem.

Saturday, March 19, 2016

How to delete from table using JOIN in SQL Server

It's a little bit tricky to delete from a table while using any type of JOIN in SQL e.g. Inner Join, Left Outer Join, or Right Outer Join. The obvious syntax doesn't work as shown below:
delete from #Expired e INNER JOIN 
Deals d ON e.DealId = d.DealId
Where d.Brand = 'Sony'

here I have a table with a list of expired deals which I want to delete from the Deals tables, but only for Sony.

When I run this SQL Query in Microsoft SQL Server 2008, it gave me following error:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'e'.

Now, I am puzzled, how to delete from a table while using INNER JOIN in SQL Server?

Friday, March 18, 2016

How to Reverse an ArrayList in Java using Recursion - Example Tutorial

If you ever need to reverse a List in Java e.g. ArrayList or LinkedList, you should always use the Collections.reverse() method. It's safe and tested and probably perform better than the first version of the method you write to reverse an ArrayList in Java. It's also one of the recommended best practice to prefer library method instead of writing your own, as advised by great Joshua Bloch in Effective Java. There are so many benefits of using library method e.g it was written by experts, thoroughly and open source tested and more actively maintained. Anyway, if you have been asked to write a function to reverse a List using recursion in Java, may be as part of your homework, assignment or during an interview, then you are in the right place. In this tutorial, I'll show you how to reverse an ArrayList of String using recursion. You can use this technique to reverse any type of List e.g. List of Integer, List of String or List of your own custom objects.

Thursday, March 17, 2016

Java Synchronization Tutorial : What, How and Why?

Multithreading and synchronization are a very important topic for any Java programmer. Good knowledge of multithreading, synchronization, and thread-safety can put you in front of other developers, at the same time, it's not easy to master this concept. In fact writing correct concurrent code is one of the hardest things, even in Java, which has several inbuilt synchronization utilities. In this Java synchronization tutorial we will learn what is meaning of Synchronization in Java, Why do we need Synchronization in Java, What is java synchronized keyword, examples of using Java synchronized method and blocks, What can happen in multithreading code in absence of synchronized constructs, tips to avoid mistakes, while locking critical section in Java and some of important points about synchronization in Java.

Wednesday, March 16, 2016

How to Convert an Array to Comma Separated String in Java

The simplest way to convert an array to comma separated String is to create a StringBuilder, iterate through the array, and add each element of the array into StringBuilder after appending comma. You just need Java 1.5 for that, even if you are not running on Java 5, you can use StringBuffer in place of StringBuilder. The joining of String has got even easier in JDK 8, where you have got the join() method right in the String class itself. The join() method takes a delimiter and a source, which can be array or collection and returns a String where each element is joined by a delimiter. If you want to create a CSV string, just pass comma as a delimiter. Btw, these two methods are not the only way to create a comma separated String from an array, you can also use Apache Commons or Spring framework's utility class to do the same.

Saturday, March 12, 2016

10 Hibernate Interview Questions and Answers for Java J2EE Programmers

Hibernate Interview Questions are asked on Java J2EE Interviews, mostly for web based enterprise application development role. Success and acceptability of Hibernate framework on Java world have made it one of the most popular Object Relational Mapping (ORM) solution in Java technology stack. Hibernate frees you from database-specific coding and allows you to focus more on utilizing powerful object oriented design principle to implement core business logic. By using Hibernate you can switch between database rather easily and also take advantage of out of box caching facilities provided by Hibernate, in terms of second-level cache and query cache. It also frees Java developer from writing JDBC code as Hibernate takes care of that. In short, provides a complete solution to implement DAO layer of your Java or JEE application. 

How to Reverse a String in place in Java - Example

It's possible to reverse a String in place by using a StringBuilder. Since String is Immutable in Java, it's not possible to reverse the same String, but you can minimize the number of intermediate String objects by using StringBuilder or StringBuffer, which are mutable. The algorithm to reverse the String in place is similar to the algorithm we have used earlier to reverse an array in place. You need to traverse the String from one end, swapping characters at another end until you reach the middle of the String. At the point characters in your String is reversed. This algorithm only requires one extra character of memory to facilitate the swapping of characters. The time complexity of this algorithm is O(n/2) i.e. O(n) where n is the length of String.

Friday, March 11, 2016

How to Remove First and Last Character of String in Java - Example Tutorial

You can use the substring() method of java.lang.String class to remove the first or last character of String in Java. The substring() method is overloaded and provides a couple of versions which allows you to remove a character from any position in Java. Alternatively, you can convert String to StringBuffer or StringBuilder and then use its remove() method to remove the first or last character. Both StringBuffer and StringBuilder provides a convenient deleteCharAt(int index) method which removes a character from the corresponding index. You can use this method to remove both first and last character from String in Java. In the last article, I have showed you how to get the first and last character from String and today I will teach you how to remove the first and last character of String in Java. Let's see a couple of examples and how you can use these two techniques to solve this problem.

Wednesday, March 9, 2016

Top 50 Java Thread Interview Questions Answers for Experienced

You go to any Java interview, senior or junior, experience or freshers,  you are bound to see a couple of questions from the thread, concurrency, and multi-threading. In fact, this built-in concurrency support is one of the strongest points of Java programming language and helped it to gain popularity among enterprise world and programmers equally. Most of lucrative Java developer position demands excellent core Java multi-threading skills and experience in developing, debugging and tuning high-performance low latency concurrent Java applications. This is the reason, it is one of the most sought after skill on Java interviews. The multithreading and concurrency are also hard to master concept and only good developers with solid experience can effectively deal with concurrency issues.

Sunday, March 6, 2016

How to convert an Array to HashSet in Java - Example Tutorial

The array was there in Java before the Collection framework makes its entry in JDK 1.4, hence, you will find several legacy code, which accepts array than the Collection or Set. Due to this, we often need to convert an array to different types of collection e.g. List or Set. Earlier, I have told you the trick to convert an array to list and today I'll teach you how to convert an array to HashSet by using the same technique. The concept you need to know is that Collection provides a copy constructor i.e. you can pass a collection to create another collection. We'll leverage the same fact and to create an HashSet, we'll pass an ArrayList. Since you can use Arrays.asList() to convert an array to ArrayList, converting an array to HashSet is just two step job, first convert an array to a list and then convert a list to set.

Saturday, March 5, 2016

3 Books to Learn Eclipse IDE for Java JEE Programmers - Best of Lot

In order to become a good Java developer solid knowledge of Eclipse IDE, or whatever IDE you use e.g. Netbeans or IntelliJ Idea, is a must. Java has been blessed with excellent tooling which turbo-charge application development. IDEs or Integrated Development Environment allows you to code, run, test and debug from just one tool. They are the immense productivity booster. Since I have started Java development coding in Notepad, TextPad, and JCreator, I know how it feels having the power of IDEs with you. There are three big IDEs in Java world, Eclipse, NetBeans and IntelliJ Idea. First two are free and third one requires the license. I use Eclipse and it's also the most popular IDE in Java world.

Thursday, March 3, 2016

Difference between map() and flatMap() in Java 8 Stream

The map() and flatmap() are two important operations in new functional Java 8. Both represents functional operation and they are also methods in class. The key difference between map() and flatmap() function is that when you use map(), it applies a function on each element of stream and stores the value returned by the function into a new Stream. This way one stream is transformed into another e.g. a Stream of String is transformed into a Stream of Integer where each element is length of corresponding Stream. Key thing to remember is that the function used for transformation in map() returns a single value. If map() uses a function, which, instead of returning a single value returns a Stream of values than you have a Stream of Stream of values, and flatmap() is used to flat that into a Stream of values.