Invocation of toString on an array

private String [] arrActions;

public String [] getArrActions() {
  return arrActions;
}

public void setArrActions(String [] parrActions) {
  this.arrActions = parrActions;
}

String action = null;

if(form.getArrActions() != null){
  action = form.getArrActions().toString;
}

Bug Code

  action = form.getArrActions().toString;

Fix Code

  action = Arrays.toString(form.getArrActions());

——————————————————————————————————————————————————————

Incorrect lazy initialization and Update of Static Field

Below is the Sample code trying to implement Singleton pattern without synchronized

public static Object getInstance() {
    if (instance != null) {
        return instance;
    }

    instance = new Object();
    return instance;
}

In a multi thread environment, there would be potential for your singleton to be created more than once with your current code.

The race condition here is on the if check. On the first call, a thread will get into the if check, and will create the instance and assign it to ‘instance’. But there is potential for another thread to become active between the if check and the instance creation/assignment. This thread could also pass the if check because the assignment hasn’t happened yet. Therefore, two (or more, if more threads got in) instances would be created, and your threads would have references to different objects.

The solution for this can be in Two Ways
Solution 1

private static volatile Object myLock = new Object(); // must be declared volatile

public static Object getInstance() {
    if (instance == null) { // avoid sync penalty if we can
        synchronized (MyCurrentClass.myLock) { // declare a private static Object to use for mutex
            if (instance == null) {  // have to do this inside the sync
                instance = new Object();
            }
        }
    }

    return instance;
}

For more Details on Volatile memory refer the following Link
http://tutorials.jenkov.com/java-concurrency/volatile.html

Solution 2

public synchronized static Object getInstance() {
    if (instance == null) {
        instance = new Object();
    }

    return instance;
}

——————————————————————————————————————————————————————
Covariant Equals method Defined

When equals method not properly overridden in your class then this Error is Thrown.

Things to Check
1.The Passing parameter should be Object Type.

Code for Overridding equals() method

public boolean equals(Object other){
    boolean result;
    if((other == null) || (getClass() != other.getClass())){
        result = false;
    } // end if
    else{
        People otherPeople = (People)other;
        result = name.equals(otherPeople.name) &&  age == otherPeople.age;
    } // end else

    return result;
} // end equals

——————————————————————————————————————————————————————

int value cast to float and then passed to Math.round

 public static String createFileSizeString(big size)
 {
   if (size < 1048576)
   {
     return (Math.round(((size * 10) / 1024)) / 10) + " KB";
   }
 }

Math.round was intended to be used on floating point arithmetic.The (size*10)/1024 may or may not return float value.So we should explicitly convert to float before using math.round

  (size * 10) / 1024 

should be

  ((float)size * 10) / 1024

——————————————————————————————————————————————————————
Bad comparison of nonnegative value with negative constant

 if (rows.size() < 0 || rows.isEmpty()) {
 .
 .
 .
 }

rows.size() can either be 0 or Positive Integer

 if (rows.size() < 0 || rows.isEmpty()) 

Should be

 if (rows.size() <= 0 || rows.isEmpty()) 

——————————————————————————————————————————————————————
the parameter is dead upon entry

 public void TestSample(boolean status)
 {
  .
  . 
  .
  status = true;
 }

The Parameter status is set to true and it was never used.So we can simply remove it as shown below

 public void TestSample(boolean status)
 {
  .
  . 
  .
 }

——————————————————————————————————————————————————————
Null value guaranteed to be De referenced
Solution
Check for null value of the Bean before calling bean method

 .
 shipmentBean.getStatus();
 .

should be

 .
 (shipmentBean==null?null:shipmentBean.getStatus());
 .

——————————————————————————————————————————————————————
Suspicious reference Comparison
Suspicious reference Comparison of Long

  Long val1 = 127L;
  Long val2 = 127L;

  System.out.println(val1 == val2);

  Long val3 = 128L;
  Long val4 = 128L;

  System.out.println(val3 == val4);

Output

 true
 false

This is happening because you are comparing Long objects references, not long primitives values.

To safely do this comparison you want (still using Long objects), you have the following options:

 System.out.println(val3.equals(val4));                    // true
 System.out.println(val3.longValue() == val4.longValue()); // true
 System.out.println((long)val3 == (long)val4);             // true

Java caches the primitive values from -128 to 127. When we compare two Long objects java internally type cast it to primitive value and compare it. But above 127 the Long object will not get type caste. Java caches the output by .valueOf() method.

This caching works for Byte, Short, Long from -128 to 127. For Integer caching works From -128 to java.lang.Integer.IntegerCache.high or 127.

Comparing non-primitives (aka Objects) in Java with == compares their reference instead of their values. Long is a class and thus Long values are Objects.

——————————————————————————————————————————————————————

Convert double to BigDecimal and set BigDecimal Precision

new BigDecimal(0.1)

The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625.

This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

 new BigDecimal(0.1)

should be

 BigDecimal.valueOf(0.1)

Value that is returned by BigDecimal.valueOf is equal to that resulting from invocation of

Double.toString(double).

——————————————————————————————————————————————————————
Dead store to Local Variable

public class Foo
{
    public static void Bar()
    {
    
    }
}

public class Abc
{
    public void Test()
    {
      Foo objFoo = new Foo(); 
      objFoo.Bar();

    }
}

Bug

    Foo objFoo = new Foo(); 
    objFoo.Bar(); 

Fix

   Foo.Bar();

If I look at code someVariable.SomeMethod() I expect it to use the value of someVariable. If SomeMethod() is a static method, that expectation is invalid. Java won’t let you use a potentially uninitialized variable to call a static method, despite the fact that the only information it’s going to use is the declared type of the variable.

//Function declaration
function foo() { return 5; }

//Anonymous function expression
var foo = function() { return 5; }

//Named function expression
var foo = function foo() { return 5; }

function declarations loads before any code is executed.While function expressions loads only
when the interpreter reaches that line of code.

So if you try to call a function expression before it’s loaded, you’ll get an error

But if you call a function declaration, it’ll always work. Because no code can be called until all declarations are loaded.

Function Expression

  alert(foo()); // ERROR! foo wasn't loaded yet
  var foo = function() { return 5; } 

Function Declaration

  alert(foo()); // Alerts 5. Declarations are loaded before any code can run.
  function foo() { return 5; } 

The difference is in First case foo() is defined at run-time, whereas in second case foo() is defined at parse-time
for a script block

Getting GMT Time for the Day

 var strDate = new Date();

 hour = strDate.getUTCHours().toString();
 minute = strDate.getUTCMinutes().toString();
 second = strDate.getUTCSeconds().toString();

 if (hour.length == 1) { hour = "0" + hour; }
 if (minute.length == 1) { minute = "0" + minute; }
 if (second.length == 1) { second = "0" + second; }

 Cal.SetHour(hour); 
 Cal.SetMinute(minute); 
 Cal.SetSecond(second);

Creating Database Connection in Context.xml

<Context>
  <Resource name="jdbc/[YourDatabaseName]"  auth="Container" type="javax.sql.DataSource"
   username="[DatabaseUsername]" password="[DatabasePassword]" driverClassName="com.mysql.jdbc.Driver"
   url="jdbc:mysql:/[yourserver]:3306/[yourapplication]" maxActive="15" maxIdle="3"/>
</Context>

context.xml

<Context>
  <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000" username="root" password="" driverClassName="com.mysql.jdbc.Driver"  url="jdbc:mysql://localhost:3306/test"/>
  <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

Test.java

 PrintWriter out = response.getWriter();
 String Sno = request.getParameter("t1");
 String Name = request.getParameter("t2");
		
 try{
   InitialContext context = new InitialContext();
   DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/test");
   Connection conn  = ds.getConnection();
   PreparedStatement statement = conn.prepareStatement("insert into Details values(?,?)");
   statement.setString(1, Sno);
   statement.setString(2, Name);
   statement.execute();
   conn.close();
   statement.close();
   out.println("Done");
  }catch (Exception e) {
   e.printStackTrace();
  }

How to check for overlapping events in a period

Case1: No Event between Period Start Date and Period End Date
Case2: No Event which subsets another event Period Start Date and Period End Date
Case3: No Event which starts before period start date and ends in between some other events end date
Case4: No Event which starts after some other event period start date and ends after events end date

oVERLAPPING eVENT

Where Clause:

SELECT count(event) 
  FROM tblEvent
 WHERE (new_event_start_date BETWEEN old_event_start_date and old_event_end_date OR
        new_event_end_date BETWEEN old_event_start_date and old_event_end_date) OR 
        new_event_start_date <= old_event_start_date OR new_event_end_date >= old_event_start_date

Case1,2,3:

     new_event_start_date BETWEEN old_event_start_date and old_event_end_date OR
     new_event_end_date BETWEEN old_event_start_date and old_event_end_date

Case4:

new_event_start_date <= old_event_start_date OR new_event_end_date >= old_event_start_date

வெற்றித் தோற்காமல் இருப்பதில் இல்லை; தவறுகளைத் திருப்பிச்செய்யாமல் இருப்பதில் இருக்கிறது

தொட்டனைத் தூறும் மணற்கேணி மாந்தர்க்குக்
கற்றனைத் தூறும் அறிவு

ஆமை புகுந்த வீடு உருப்படாது??

கல்லாமை,உண்மை பேசாமை,பெரியோர் பேணாமை,சுற்றம் சேர்க்காமை,அன்பு செலுத்தாமை போன்ற ஆமைகள் புகுந்தால் அந்த வீடு முன்னேறுவதற்கு வாய்ப்பே இல்லாமல் போய்விடும்.எனவே, இந்த இல்லாமைகள் அகற்றி வாழ்க்கையில் இன்புற வேண்டும்

ஆயிரம் முறை பொய் சொல்லி கூட ஒரு கல்யாணம் பண்ணலாம்.

ஆயிரம் பொய் சொல்லியாவது ஒரு கல்யாணம் பண்ணலாம் என்பதல்ல. ஆயிரம் முறை போய் சொல்லி ஒரு கல்யாணம் பண்ணலாம்என்பதாகும். அதாவது, நம் உறவினர்கள், நண்பர்கள் ஆகியோருடன் நாம் பகைமை கொண்டு நம் வீட்டில் நடக்கும் கல்யாணம் போன்ற சுபதினங்களில் நாம் அழைக்காமல் இருப்போம். ஆனால், அவர்களை விட்டுவிடாமல் ஆயிரம் முறை போய் சொல்லியாவது அவர்களை அழைத்து நம் வீட்டில் திருமணம் நடத்தவேண்டும் என்பதாகும். அப்படி நல்ல நோக்கத்திற்காக சொல்லப்பட்ட பழமொழிதான் பின்னாளில் மருவி அல்லது மாறி இப்போது உள்ளதுபோல ஆகிவிட்டது.

அடியாத மாடு படியாது

உண்மை பொருள் என்னவென்றால் மாட்டின் கால்களுக்கு லாடம் அடித்தால் தான் அதனால் கடுமையான வேலைகளை (உழுதல் போன்ற ) செய்ய முடியும் என்பது தான்.

கல் தோன்றா மன் தோன்றாக் காலத்தே

(கல்) கல்வி அறிவு தோன்றாத (மன்) மன்னராட்சி ஏற்படுவதற்கு முன்பாகவே, (வாளோடு) வீரத்தோடு தோன்றிய முதல் இனம் தமிழினம் என்பது இன்று கல் தோன்றா மண் தோன்றாக் காலத்தே வாளொடு முன் தோன்றிய மூத்தகுடி என்று, கல்லும் மண்ணும் தோன்றாத காலத்திலேயே (பூமி உருவாவதற்கு முன்பே) தமிழினம் உருவாகி விட்டதாக அர்த்தப் படுத்தப் படுகின்றது.

ஆறிலும் சாவு நூறிலும் சாவு

மகாபாரதத்தில் கர்ணனின் தாய் குந்திதேவி பாண்டவர்களுடன் கர்ணனை சேர்ந்து கொள்ளுமாறு கேட்கின்றாள். அப்போது தான் பாண்டவர்களுடன் சேர்ந்து ஆறாவதாக வந்தாலும் தனக்கு சாவு நிச்சயம். கொளரவர்கள் நூறு பேருடன் சேர்ந்திருந்தாலும் சாவு நிச்சயம். எனவே செய்நன்றிக் கடனுக்காக தான் கௌரவர்களுடனேயே இருந்து விடப் போவதாக கர்ணன் கூறுகின்றான். அதாவது ஐந்து பேருடன் ஆறாவதாகச் சேர்ந்தாலும் சாவுதான். நூறு பேர் கௌரவர்களுடன் இருந்தாலும் சாவுதான் என்பதுதான் அது. ஆனால் ஆறிலும் சாவு நூறிலும் சாவு என்பது இன்று வயதைக் குறிப்பதாக அர்த்தப்பட்டு விட்டது.

நாயைக் கண்டால் கல்லைக் காணோம்; கல்லைக் கண்டால் நாயைக் காணோம்.

பண்டைக்காலத்தில் அற்புத சிற்பங்கள் வடிக்கப் பட்டன. மாமல்லபுரம், தஞ்சை, காஞ்சி சிற்பங்கள் இதற்கு எடுத்துக் காட்டாக விளங்கின. இங்கே ஒரு சிற்பி நாயின் உருவத்தை கல்லில் சிற்பமாக வடித்திருந்தான். அந்த சிற்பத்தை ஒருவன் மிகவும் ரசித்தான். அந்த சுவைஞனைச் சிற்பி கேட்டான் “என் சிற்பம் எப்படி? என்று. அதற்குச் சுவைஞன் சொன்ன பதில் ‘நாயைக் கண்டால் கல்லைக் காணோம்; கல்லைக் கண்டால் நாயைக் காணோம்’ என்பதாக இருந்தது. அதாவது அதில் நாயைப் பார்த்தால் கல் தெரியவில்லை. கல்லைப் பார்த்தால் நாய் தெரியவில்லை.

பசி வந்திட பத்தும் பறந்து போகும்

அறிவுடைமை, இன்சொல், ஈகை, தவம், காதல், தானம், தொழில், கல்வி, குலப்பெருமை, மானம் ஆகிய பத்து குணங்களும் பசி என்று வந்து விட்டால் பறந்து போகும் என்பது உண்மை.

ஊரான் பிள்ளையை ஊட்டி வளர்த்தால் தன் பிள்ளை தானே வளரும்.

ஒருவனின் மனைவி கர்ப்பமாக இருக்கும் போது (என்னதான் அவன் மனைவியாக இருந்தாலும் அவள் இன்னொருவன் அதாவது ஊரான் பிள்ளைதானே) அவளை நன்றாக கவனித்து கொண்டால் , அவளின் வயிற்றில் வளரும் தன்பிள்ளை தானாக வளரும் என்பதாகும்.

வாழ்க்கையின் உண்மை

ஒருவனுக்கு நான்கு மனைவிகள் இருந்தார்கள்.
ஆனால் அவன் தனது நான்காவது மனைவியை மட்டும் மிக அதிகமாக நேசித்தான். அந்த மனைவியின் அனைத்து ஆசைகளையும் நிறைவேற்றினான்.
அவளுக்கு தேவையானதை எல்லாம் செய்துகொடுத்தான்.
அவன் தனது மூன்றாவது மனைவியைக்கூட நேசித்தான்.
ஆனால் அவளை தனது நண்பர்களுக்கு முன்னால் காட்டிக்கொள்ள பயந்தான்.
பிறரோடு ஓடி விடுவாளோ என்று பயந்தான்.
அவன் தனது இரண்டாவது மனைவியையும் நேசித்தான்.
ஆனால் தனக்கு பிரச்சினைகள் வரும்போது மட்டும் அவளிடம் போவான். அவளும் அவனுடைய பிரச்சினைகளில் உதவினாள்.
ஆனால் அவன் ஒருபோதும் தனது முதல்மனைவியை நேசிக்கவே இல்லை. ஆனால் அவளோ அவன்மீது மிகவும் நேசம் வைத்திருந்தாள். அவனது எல்லா தேவைகளையும் அவள் கவனித்து கொண்டாள்.
ஒருநாள்…
அவன் மரணப்படுக்கையில் விழுந்தான். தான் இறக்கப்போவதை உணர்ந்துவிட்டான். தான் இறந்த பின் தன்னுடன் இருக்க ஒரு மனைவியை விரும்பினான்.
எனவே தன்னுடன் சாக யார் தயாராய் இருக்கிறார்கள் என அறிந்துகொள்ள விரும்பினான். தான் அதிகம் நேசித்த நான்காவது மனைவியை அழைத்தான்.
அவளோ அதிரடியாக மறுத்துவிட்டு அவனை விட்டு நீங்கினாள். அவன் தனது மூன்றாவது மனைவியை அழைத்தான்.
அவளோ நீயோ சாகப்போகிறாய்.
நான் வேறு ஒருவருடன் போகப்போகிறேன் என்று கூறிவிட்டு சென்றுவிட்டாள். பிறகு தனது இரண்டாவது மனைவியை அழைத்துக் கேட்டான்.
அவளும் சாரி என்னால் உன் கல்லறைவரைக்கும் கூட வரமுடியும். கடைசி வரை உன்னுடன் வரமுடியாது என்று மறுத்துவிட்டாள்.
நொந்துபோன அவன் இதயம் தளர்ந்து போனது. அப்போது தான் அவனது முதல் மனைவியின் குரல் ஒலித்தது. ‘’
நீ எங்கே போனாலும் நான் உன்னுடனே இருப்பேன்.
உன்னுடன் நான் கண்டிப்பாக வருவேன் ‘’ என்று சொன்னாள். ஆனால் அவளோ எலும்பும் தோலுமாக சாகும் தருவாயில் இருந்தாள். காரணம் அவன் அவளை நன்கு கவனித்துக் கொள்ளாததுதான். அவன் வருந்தினான். நான் நன்றாக இருக்கும் போதே உன்னையும் சரியாகக் கவனித்திருக்கவேண்டும். தவறிவிட்டேன் என்று அழுதான். அந்த
வருத்தத்திலேயே மரித்தும் போயினான்.

உண்மையில் நாம் அனைவருக்குமே இந்த நான்கு மனைவியர் உண்டு.

1. நான்காவது மனைவி நமது உடம்பு.
நாம் என்னதான் வாழ்நாள் முழுக்க நன்றாகக் கவனித்துக் கொண்டாலும் கடைசியில் நம்முடன் வரப்போவதில்லை.
நாம் இறந்ததும் அதுவும் அழிந்து போகிறது.
2. மூன்றாவது மனைவி நமது சொத்து சுகம்தான்.
நாம் மறைந்ததும் அவை வேறு யாருடனோ சென்றுவிடுகிறது.
3. நமது இரண்டாம் மனைவி என்பது நமது குடும்பம் மற்றும் நண்பர்கள்.
அவர்கள் நமது கல்லறை வரையில் தான் நம்முடன் கைகோர்ப்பார்கள்.
அதற்குமேல் நம்முடன் கூட வரப்போவதில்லை.
4. நாம் கவனிக்காமல் விட்ட முதல் மனைவி நமது ஆன்மா.
நாம் நன்றாக இருக்கும் போது நம்மால் கவனிக்கப்படாமல் நலிந்து சிதைந்து போய் இருந்தாலும் நம்முடன் இறுதி வரை கூட வரப்போவது நமது ஆன்மாதான்.

Working with ENUM
ENUM is just a list of set of value which are static and final.Executing the below code will have a output like one below

public class Test {
	public enum Company {
		EBAY, PAYPAL, GOOGLE, YAHOO, ATT
	}

	public static void main(String[] args) {
                System.out.println(Company.EBAY);
	}
}

Output

  EBAY

Now what you can do is to make this constant (static and final) to have further attributes like one below.

public enum Company {
EBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25);
private int value;
 
private Company(int value) {
this.value = value;
}
}

and more Values with overloaded constructor

public class Test1 {

	public enum JobStatus {
		OPEN("Open", "Its Open", "1"), ERROR("Error", "Its a Error", "2"), WORKING(
				"Working", "Its Working", "3"), CLOSED("Closed", "Its Closed");
		private String	value;
		private String	label;
		private String	order;

		private JobStatus(String label, String value, String order) {
			this.label = label;
			this.value = value;
			this.order = order;
		}

		private JobStatus(String label, String value) {
			this.label = label;
			this.value = value;
		}

		public String getValue() {
			return this.value;
		}

		public String getLabel() {
			return this.label;
		}
	}

	public static void main(String[] args) {
		System.out.println(JobStatus.OPEN.value);
		System.out.println(JobStatus.OPEN.label);
		System.out.println(JobStatus.OPEN.order);
		System.out.println(JobStatus.CLOSED.order);
	}
}

Output

Its Open
Open
1
null

When you need a predefined list of values which do not represent some kind of numeric or textual data, you should use an enum. For instance, in a chess game you could represent the different types of pieces as an enum

enum ChessPiece {
PAWN,
ROOK,
KNIGHT,
BISHOP,
QUEEN,
KING;
}

Assigning Values to ENUM

public enum Company {
EBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25);
private int value;
 
private Company(int value) {
this.value = value;
}
}
  • All enums implicitly extend java.lang.Enum.
  • MyEnum.values() returns an array of MyEnum’s values.
  • Enum constants are implicitly static and final and can not be changed once created.
  • Enum can be safely compare using “==” equality operator
  • An enum can be declared outside or inside a class, but NOT in a method.
  • An enum declared outside a class must NOT be marked static, final , abstract, protected , or private
  • Enums can contain constructors, methods, variables, and constant class bodies.
  • enum constructors can have arguments, and can be overloaded.

Simple Search based on Three Parameters

The Search should happen even when one element of the form is not Empty.


isParamNotEmpty = false;

if(param1.isNotEmpty())
{
  .
  .
  Other Coding Lines
  .
  isParamNotEmpty = true;
}

if(param2.isNotEmpty())
{
  .
  isParamNotEmpty = true;
}

if(param3.isNotEmpty())
{
  .
  . 
  isParamNotEmpty = true;
}

if(isParamNotEmpty)
{ 
  doSearch(); 
}