Skip navigation
1 2 Previous Next

Java Fortune

21 posts

When you pass a primitive data type, such as a char, int, float, or double, to a function then you are passing by value. That means that a copy of the data type is duplicated, and passed to the function. That means that a copy of the data type is duplicated, and passed to the function. If the function chooses to modify that value, it will be modifying the copy only.

 

When you pass a Java object, such as an array, a vector, or a string, to a function then you are passing by reference.So that means that if you pass an object to a function, you are passing a reference to it, not a duplicate. Any changes you make to the object's member variables will be permanent

When we use the == operator, we are actually comparing two object references, to see if they point to the same object. We cannot compare, for example, two strings for equality, using the == operator. We must instead use the .equals method, which is a method inherited by all classes from java.lang.Object.

Ex. String Comparison

     String abc = "abc"; String def = "def";

// Bad way if ( (abc + def) == "abcdef" ) { ….. }

 

// Good way if ( (abc + def).equals("abcdef") ) { ..... }

Fortunately, even if you don't spot this one by looking at code on the screen, your compiler will. Most commonly, it will report an error message like this : "Can't convert xxx to boolean", where xxx is a Java type that you're assigning instead of comparing.

Accessing non-static member variables from static methods (such as main)

 

 

public class StaticDemo

      {

        public String my_member_variable = "somedata";

        public static void main (String args[])

        {

        // Access a non-static member from static method

                System.out.println ("This generates a compiler error" +

            my_member_variable );

        }

     }

 

 

 

Mistyping the name of a method when overriding

 

 

If you mistype the name, you're no longer overriding a method - you're creating an entirely new method, but with the same parameter and return type.


 

Solution: Using LIKE clause in SQL statement like below is simple:

pstmt = conn.prepareStatement("SELECT * FROM table_name WHERE field_name LIKE ?");

String var = "some_string";
pstmt.setString(1, var);


This works fine.

Now, to modify this query in order to search between strings is little tricky. Below is the SQL statement to counter it:


pstmt = conn.prepareStatement("SELECT * FROM table_name WHERE field_name LIKE '%' || ? || '%'");

String var = "substring";
pstmt.setString(1, var);



Use the '||' concatenation operator to combine % and the ?. This should resolve any issues while trying to perform LIKE searches with-in strings. Doing LIKE searches with % infront and back may cause some performance degradation, so you need to look at the explain plan of your query how it impacts performance. Moral of the story use % in front and back only when absolutely required.
navurinv

Java Tips

Posted by navurinv Dec 28, 2011

When you use a boolean condition variable always go for primitive data type.

Check the condition like below :  boolean flag ;

If(flag) – true

If(!flag) – false

 

 

 

// bad:

ArrayList list = new ArrayList();

list.add(somevar);

// good:

List list = new ArrayList();

list.add(somevar);

Filter Blog