Skip to main content

Most Common mistakes in Java Coding.

1. Copy & Paste

Its one of the most powerful tool and technique in the typical programmer. But at the same time its too dangerous that you ought to forgot the code according to you requirements. So always be cautious about what piece of code you are copying. Read at least two to three times to avoid the tricky for fishy things while your unit testing.

2. Exception Catch Block.

Always give priority to write some thing useful and meaning full statements in the exception catch block.It really worth when you troubling shooting the issues. Never leave this block with empty or sop's. And always throw the exception to the calling method.

3. String operations

Since String is immutable object, when you do any operation on string that results a new object. So after you do any modifications on String and do assign the string some object and use it.

4. Null pointers

Don't perform the operations on objects without checking the Object null condition. And while checking the null condition always provide 'null' key word as the first operand. e.g. if(null != object) . Especially while printing any calling any methods on an object.

5. Result Set next() operation.

Be in mindful that when ever you call resultset.next() operation cursor moves from one row to its next row.




Comments

Popular posts from this blog

NO SQL Types and vendors in market

NO SQL

KEY - VALUE Store  - Similar to a  MAP.
   e.g. Dynamo DB or REDIS

DOCUMENT Sore  - Similar to KEY-VALUE, value can be JSON/XML and key will be unqiuely idnetified this document.
   e.g. Coutch DB or mongoDB.

Column Store - Multi timentional table, identifying data with row and column numbers.
  e.g.  Cassandra or Apache HBase

Graph Store - Store relation between nodes (record entities). It has better transaction management
e.g. Neo4J and Orient DB.




Compress a String

package com.sbs.java8.praticse;

public class StringCompression {

public StringCompression() {
// TODO Auto-generated constructor stub
}

public static void main(String[] args) {
System.out.println(compressString("aaaabbbbbccccAAAAccccccccdefg"));
}

public static String compressString(String str) {
//String str = "aaaabbbbbcccc";
char[] charArray = str.toCharArray();
String compressedString = "";
int i = 0;
while (i < charArray.length) { int counter = 1; int j = i + 1; while (j < charArray.length && charArray[i] == charArray[j]) { counter++; j++; i++; } compressedString = compressedString + charArray[i] + counter; i++; } //System.out.println(compressedString); String output =(compressedString.length() > charArray.length)? str: compressedString;
return output;
}
}