h1

How String’s SubString works in Java

June 11, 2009

Java handles sub-strings in a way that isn’t immediately obvious and which can have some nasty side effects if you don’t realize what is going on under the hood. When the substring method is used it appears to return a new String but it actually isn’t a new String at all. What the method does is create a new String descriptor that maintains a pointer to the original String and the start and end index – in other words the relevant part of the old String. This is all well and good most of the time and works because Strings are immutable. It can, however, lead to memory exhaustion problems in some cases. The cause of the potential memory exhaustion problem is simple. The newly created sub-string is maintaining a reference to the old String -“ even when the old String appears to have no more references it won’t be garbage collected if there are references to the sub-string. Imagine for a moment that the old String was several megabytes in size and the sub-string was just the first few characters. Those few characters are actually consuming many megabytes of memory! The example program at the bottom of the page shows this in action.

Interestingly the behaviour described above even happens with a completely empty String (e.g. substring is called with the same start and end index) so it’s quite easy to end up with an apparently empty String using vast chunks of the system memory. A way to avoid this problem is to internalize the sub-string. This will provide a reference to the String held in the internal cache and is created if necessary. The String in the internalized cache is a real String so only takes up the amount of space you would expect.

It is also worth pointing out that sub-strings aren’t internalized automatically even if the original String was internalized. You might have expected that if the original String was internalized sub-strings would be as well but that is not the case. The reason for this behaviour is probably because intern always creates a complete new String in the cache whereas substring can simply use references into an old String. For long Strings and their sub-strings the currently method is probably the most efficient, this method is even more efficient if neither the original String nor it’s sub-string descendants are long lived which is often the case in real world applications.

public class Substring {

public static void main( String[] args ) {
Substring substring = new Substring();
substring.memoryUsageExample();
substring.internExample();
}

private void memoryUsageExample() {
Runtime runtime = Runtime.getRuntime();

System.gc();
// Show the base memory load
System.out.println( "Memory Usage (1): " + ( runtime.totalMemory() - runtime.freeMemory() ) );

// Build a huge String
StringBuilder builder = new StringBuilder();
for( int i = 0, n = 10000000; i < n; i++ ) {
builder.append( "x" );
}

String hugeString = builder.toString();
// At this point we have the StringBuilder and the String in memory
System.out.println( "Memory Usage (2): " + ( runtime.totalMemory() - runtime.freeMemory() ) );

builder = null;
System.gc();
// We should now only have hugeString in memory
System.out.println( "Memory Usage (3): " + ( runtime.totalMemory() - runtime.freeMemory() ) );

String littleString = hugeString.substring( 0, 5 );

// Internalize the littleString to remove the reference to the hugeString
// littleString = littleString.intern();

hugeString = null;
System.gc();

// Even though hugeString is dereferenced the memory usage is still high.
// Try commenting out the display of littleString below you will notice
// that the memory drops as you would expect.
System.out.println( "Memory Usage (4): " + ( runtime.totalMemory() - runtime.freeMemory() ) );

System.out.println( "Little String: " + littleString );
}

private void internExample() {
String test = "This is a test string";
test = test.intern();
String sub = test.substring( 10, 14 );

// False as you would expect
System.out.println( "Are the test string and sub-string the same? " + ( test == sub ? "True" : "False" ) );

// False but you might expect it to be true. After all the original test
// String was internalized so you would be forgiven for expecting it to
// internalize sub-strings as well.
System.out.println( "Is the sub-string internalized? " + ( "test" == sub ? "True" : "False" ) );

sub = sub.intern();

// True. String literals are automatically internalized and now our sub-string is
// the same String.
System.out.println( "Is the sub-string internalized now? " + ( "test" == sub ? "True" : "False" ) );
}

}

Reference:
http://www.crazysquirrel.com/computing/java/basics/substring.jspx

h1

Java String

June 11, 2009

In Java, String is derived data type. String is a class. String is not array of characters.String variables are Objects. The important feature of String is Immutability. Strings are immutable, which means contents of the string cannot be changed after they are initially constructed. There are some concepts people need to understand when using String objects. when we create strings by initializing the variables and the values are available at compile time, then java uses a technique called Interning to reduce the space used by duplicate strings.

For example

//Declare two Strings s1 and s2, Initialize them with the same value.
// so s1 and s2 should be two different objects of String Class But
// s1 and s2 both point to the same reference in the Heap, which means
// only one object will exist for “JAVA” and it is referred by s1 and s2.
// This is called automatic Interning of String
String s1 = "JAVA";
String s2 = "JAVA";

This can be proved by using the == operator. The equality operator(==) when used with primitive data types, checks whether the data or values are the same but when it is used with Objects, they check the equality of the Identity (whether they belong to the same instance) but not the equality of the state or data of the object.To check the equality of the data, we need to use the overrided equals() method of the object.

String s1 = "JAVA";
String s2 = "JAVA";

/* The equality operator (==) checks whether the String instance are equals, that is whether both s1 and s2 refer to the same memory instance. so in this case, it returns true as both point to the same reference */
if(s1==s2) System.out.println(”Same Instance”);
else System.out.println(”Different Instance”);

so you may have a question, if i change the content of the String through s1, will the change be visible from s2? There is the catch, as i told you before, String is Immutable, so the contents cannot be changed after the initial construction. so what happens when you assign a new value to the existing string.

String s1 = "JAVA";
String s2 = "JAVA";
s1 = "SUN";

In the above scenario, a new string object will be constructed with the value “SUN” and its reference is stored in s1.Similaryly, when we call any of the methods of the String class, a new String object is constructed and returned. Java does not perform interning,if the String values are determined at the RunTime. if you want to explicitly perform the interning, then we can call the intern method of the String.

char[] chars = {'A', ' ', 'S', 't', 'r', 'i', 'n', 'g'};
//the String org has a value available at compile time
String org = "A String";
//the value of aRuntimeString is not known till the run time, as it constructed from characters during execution String aRuntimeString = new String(chars)
//The equality operator will return false, because the aRuntimeString will a different instance even though it has the same value.
//The Interning of string didnt happen as the value was known at the Runtime
if(org == aRuntimeString) System.out.println("Same Instance");
else System.out.println("Different Instance");
//we can explicitly ask for interning by calling the intern method
// This method checks the intern table in the heap for the same value, if it exists then that reference is returned.
aRuntimeString = aRuntimeString.intern();
//Now both org and aRuntimeString will be referencing the same String object, since intern was done
// here the equality operator returns true
if(org == aRuntimeString) System.out.println("Same Instance");
else System.out.println("Different Instance");

If you want avoid interning when strings are created, then create string using the Constructor.

//In this Case, two different Object of Strings are Created.
String s1 = new String("NIIT");
String s2 = new String("NIIT");
//it will return false as both of them belong to different instances
if(s1==s2) System.out.println("Same Instance");
else System.out.println("Different Instance");
//equals methods checks equality of the contents of the String
if(s1.equals(s2)) System.out.println("String values are Same");
else System.out.println("String values are different");

References http://javatechniques.com/blog/string-equality-and-interning/
http://mindprod.com/jgloss/interned.html

h1

Java Practice – Immutable Object

June 9, 2009

An immutable object is one whose externally visible state cannot change after it is instantiated. The String, Integer, and BigDecimal classes in the Java class library are examples of immutable objects — they represent a single value that cannot change over the lifetime of the object.

Mutuable Objects are ones whose state or data can be changed at any point of time. StringBuffer is an example of Mutable object.

Benefits of immutability

Immutable classes, when used properly, can greatly simplify programming. They can only be in one state, so as long as they are properly constructed, they can never get into an inconsistent state. so the advantages are

* You can freely share and cache references to immutable objects without having to copy or clone them;
* Immutable classes generally make the best map keys.
* they are inherently thread-safe, so you don’t have to synchronize access to them across threads.

Better Candidate for Caching

Because there is no danger of immutable objects changing their value, you can freely cache references to them and be confident that the reference will refer to the same value later. Similarly, because their properties cannot change, you can cache their fields and the results of their methods.

Automatically Thread Safe, No Need to Synchronize

Most thread-safety issues arise when multiple threads are trying to modify an object’s state concurrently (write-write conflicts) or when one thread is trying to access an object’s state while another thread is modifying it (read-write conflicts.) To prevent such conflicts, you must synchronize access to shared objects so that other threads cannot access them while they are in an inconsistent state. This can be difficult to do correctly, requires significant documentation to ensure that the program is extended correctly, and may have negative performance consequences as well. As long as immutable objects are constructed properly (which means not letting the object reference escape from the constructor), they are exempt from the requirement to synchronize access, becuase their state cannot be changed and therefore cannot have write-write or read-write conflicts.

Makes the Best for Keys in Collection

Immutable objects make the best HashMap or HashSet keys. Some mutable objects will change their hashCode() value depending on their state (like the StringHolder example class in Listing 2). If you use such a mutable object as a HashSet key, and then the object changes its state, the HashSet implementation will become confused — the object will still be present if you enumerate the set, but it may not appear to be present if you query the set with contains(). Needless to say, this could cause some confusing behavior.

Here is an Example below

Guidelines for writing immutable classes

Writing immutable classes is easy. A class will be immutable if all of the following are true:

* All of its fields are final
* The class is declared final
* The this reference is not allowed to escape during construction
* Any fields that contain references to mutable objects, such as arrays, collections, or mutable classes like Date:
o Are private
o Are never returned or otherwise exposed to callers,if returned a defensive copy of it should be returned
o Are the only reference to the objects that they reference
o Do not change the state of the referenced objects after construction


class ImmutableArrayHolder {

 private final int[] theArray;

 // Right way to write a constructor -- copy the array
 public ImmutableArrayHolder(int[] anArray) {
 this.theArray = (int[]) anArray.clone();
 }

 // Wrong way to write a constructor -- copy the reference
 // The caller could change the array after the call to the constructor
 //public ImmutableArrayHolder(int[] anArray) {
 //  this.theArray = anArray;
 //}

 // Right way to write an accessor -- don't expose the array reference
 public int getArrayLength() { return theArray.length }
 public int getArray(int n)  { return theArray[n]; }

 // Right way to write an accessor -- use clone()
 public int[] getArray()       { return (int[]) theArray.clone(); }

 // Wrong way to write an accessor -- expose the array reference
 // A caller could get the array reference and then change the contents
 //public int[] getArray()       { return theArray }
}

 

References :

http://www.javapractices.com/topic/TopicAction.do?Id=29
http://www.ibm.com/developerworks/java/library/j-jtp02183.html
http://www.javaranch.com/journal/2003/04/immutable.htm

h1

Levenshtein Distance Algorithm

June 9, 2009

Levenshtein Distance (or) Edit Distance

Levenshtein distance (LD) is a measure of the similarity between two strings, which we will refer to as the source string (s) and the target string (t). The distance is the number of deletions, insertions, or substitutions required to transform s into t.

For example,

    * If s is “test” and t is “test”, then LD(s,t) = 0, because no transformations are needed. The strings are already identical.
    * If s is “test” and t is “tent”, then LD(s,t) = 1, because one substitution (change “s” to “n”) is sufficient to transform s into t.

The greater the Levenshtein distance, the more different the strings are.

Levenshtein distance is named after the Russian scientist Vladimir Levenshtein, who devised the algorithm in 1965. If you can’t spell or pronounce Levenshtein, the metric is also sometimes called edit distance.

The Levenshtein distance algorithm has been used in:

    * Spell checking
    * Speech recognition
    * DNA analysis
    * Plagiarism detection

The Algorithm is as follows

Set n to be the length of s.
Set m to be the length of t.

If n = 0, return m and exit.
If m = 0, return n and exit.

Construct a matrix containing 0..m rows and 0..n columns.

Initialize the first row to 0..n.
Initialize the first column to 0..m.

  Examine each character of s (i from 1 to n).
  Examine each character of t (j from 1 to m).
  If s[i] equals t[j], the cost is 0.
 If s[i] doesn’t equal t[j], the cost is 1.
  Set cell d[i,j] of the matrix equal to the minimum of:
  a. The cell immediately above plus 1: d[i-1,j] + 1.
  b. The cell immediately to the left plus 1: d[i,j-1] + 1.
  c. The cell diagonally above and to the left plus the cost: d[i-1,j-1] + cost.

After the iteration steps are complete, the distance is found in cell d[n,m].

The Java Implementation of the above algorithm is as follows

public class EditDistance {
 
 private int getMinimum(int a, int b, int c)
 {
  return Math.min(a,Math.min(b, c));
 }
 
 public int findCost(String s1,String s2)
 {
  
  //Find the length of strings
  int s1Length = s1.length();
  int s2Length = s2.length();
  //declare two dimension array of string length + 1
  int matrix[][] = new int[s1Length+1][s2Length+1];
  
  //If Length of any one of the String is zero then return the length of the other as Cost
  if(s1Length==0) return s2Length;
  if(s2Length==0) return s1Length;
  
  //Initialize the first row with 0 to s1Length
  for(int i=0;i<=s1Length;i++)
   matrix[i][0] = i;
  
  //Initialize the first Column  with 0 to s2Length
  for(int i=0;i<=s2Length;i++)
   matrix[0][i] = i;  
  
  //Iterate through the Matrix
  for(int i=1;i<=s1Length;i++)
  {
   for(int j=1;j<=s2Length;j++)
   {
    
    int cost;
    //Check the Character at each location, if they are same then the Cost is Zero else Cost is One
    if(s1.charAt(i-1)==s2.charAt(j-1))
     cost=0;
    else
     cost=1;
    
    //Set the value of matrix[i][j] based on the logic of the minimum value
    //  of 1) Value of the Cell on the Top + 1
    //     2) Value of the Cell on the Left + 1
    //     3) Value of the Cell on the Left Top Diagonal + cost
    matrix[i][j] = getMinimum(matrix[i][j-1]+1, matrix[i-1][j]+1, matrix[i-1][j-1]+cost);
   }
  }
  
  // Used for debugging the Matrix
  /*
  for(int i=0;i<=s1Length;i++)
  {
   for(int j=0;j<=s2Length;j++)
    System.out.print(matrix[i][j]  + " ");
   System.out.println();
  }  
  */
  return matrix[s1Length][s2Length];
 }
public static void main(String[] args)
 {
  EditDistance d = new EditDistance();
  System.out.println("Distance is " + d.findCost("worse", "world"));
 }
}

 Reference Links:

http://www.merriampark.com/ld.htm

http://en.wikipedia.org/wiki/Levenshtein_distance

h1

Outlook Macro – To Add Text to the Mail message

May 26, 2009

There was requirement for my friend,  where he needs to write a macro which will pop up a form when someone clicks on new mail or reply or forward.

So i did some research and came up with a code, shown below.

 

ThisOutlookSession (code for OutlookSession)
———————————————- ———-

Option Explicit

Dim myTrapper As MyWrapper

Private Sub Application_Startup()
   Set myTrapper = New MyWrapper
End Sub

Private Sub Application_Quit()
   Set myTrapper = Nothing
End Sub

—————————————————-

MyWrapper Class Module
———————-

Option Explicit

Dim WithEvents objInspectors As Outlook.Inspectors
Dim WithEvents objInsp As Outlook.Inspector
Dim WithEvents objMsg As Outlook.MailItem

Private Sub Class_Initialize()
   Set objInspectors = Application.Inspectors
End Sub

Private Sub Class_Terminate()
   Set objInspectors = Nothing
   Set objInsp = Nothing
   Set objMsg = Nothing
End Sub

Private Sub objInspectors_NewInspector(ByVal Inspector As Inspector)
   If (Inspector.CurrentItem.Class = olMail) Then
       Set objMsg = Inspector.CurrentItem
       Set objInsp = Inspector
   End If
End Sub

Private Sub objInsp_Close()
   Set objMsg = Nothing
End Sub

Private Sub objMsg_Open(Cancel As Boolean)
   Set UserForm1.SharedInsp = objInsp
   UserForm1.Show
End Sub

—————————————————-

UserForm Code
—————-

Create Four CheckBoxButtons and one Command Button
————————————————–

Public SharedInsp As Outlook.Inspector

Dim msgItem As Object

Dim msg As String

Private Sub CommandButton1_Click()
      If (msg <> “”) Then
       Set msgItem = SharedInsp.CurrentItem
       msgItem.HTMLBody = “<center><b><u>” & msg & “</u></b></center>” & msgItem.HTMLBody
       Set msgItem = Nothing
       Unload Me
   Else
       MsgBox “Please Choose one of the Options”
   End If
End Sub

Private Sub OptionButton1_Click()
   msg = “Confidential”
End Sub

Private Sub OptionButton2_Click()
   msg = “Internal Material”
End Sub

Private Sub OptionButton3_Click()
   msg = “Highly Confidential”
End Sub

———————————————–

 

Some of the Other events in the mail are

Private Sub objMailItem_AttachmentAdd(ByVal Attachment As Attachment)

End Sub

Private Sub objMailItem_AttachmentRead(ByVal Attachment As Attachment)

End Sub

Private Sub objMailItem_BeforeAttachmentSave(ByVal Attachment As Attachment, Cancel As Boolean)

End Sub

Private Sub objMailItem_BeforeDelete(ByVal Item As Object, Cancel As Boolean)

End Sub

Private Sub objMailItem_Close(Cancel As Boolean)

End Sub

Private Sub objMailItem_Forward(ByVal Forward As Object, Cancel As Boolean)

End Sub

Private Sub objMailItem_Open(Cancel As Boolean)

End Sub

Private Sub objMailItem_PropertyChange(ByVal Name As String)

End Sub

Private Sub objMailItem_Read()

End Sub

Private Sub objMailItem_Reply(ByVal Response As Object, Cancel As Boolean)

End Sub

Private Sub objMailItem_ReplyAll(ByVal Response As Object, Cancel As Boolean)

End Sub

Private Sub objMailItem_Send(Cancel As Boolean)

End Sub

Private Sub objMailItem_Write(Cancel As Boolean)

End Sub

 

Reference Link :

http://www.cimaware.com/resources/article_57.html

h1

Vi Commands

April 1, 2009

Vi(Visual Editor)

It is one of nightmare for beginners and god for unix guru’s. Here is a list of commands which is used in Vi to do various tasks

Cutting and Pasting/Deleting text

 

Specify a buffer to be used any of the commands using buffers. Follow the ” with a letter or a number, which corresponds to a buffer.
D
Delete to the end of the line from the current cursor position.
P
Paste the specified buffer before the current cursor position or line. If no buffer is specified (with the ” command.) then ‘P’ uses the general buffer.
X
Delete the character before the cursor.
Y
Yank the current line into the specified buffer. If no buffer is specified, then the general buffer is used.
d
Delete until where. “dd” deletes the current line. A count deletes that many lines. Whatever is deleted is placed into the buffer specified with the ” command. If no buffer is specified, then the general buffer is used.
p
Paste the specified buffer after the current cursor position or line. If no buffer is specified (with the ” command.) then ‘p’ uses the general buffer.
x
Delete character under the cursor. A count tells how many characters to delete. The characters will be deleted after the cursor.
y
Yank until , putting the result into a buffer. “yy” yanks the current line. a count yanks that many lines. The buffer can be specified with the ” command. If no buffer is specified, then the general buffer is used.

 

Inserting New Text

 

A
Append at the end of the current line.
I
Insert from the beginning of a line.
O
(letter oh) Enter insert mode in a new line above the current cursor position.
a
Enter insert mode, the characters typed in will be inserted after the current cursor position. A count inserts all the text that had been inserted that many times.
i
Enter insert mode, the characters typed in will be inserted before the current cursor position. A count inserts all the text that had been inserted that many times.
o
Enter insert mode in a new line below the current cursor position.

 

Moving the Cursor Within the File

 

^B
Scroll backwards one page. A count scrolls that many pages.
^D
Scroll forwards half a window. A count scrolls that many lines.
^F
Scroll forwards one page. A count scrolls that many pages.
^H
Move the cursor one space to the left. A count moves that many spaces.
^J
Move the cursor down one line in the same column. A count moves that many lines down.
^M
Move to the first character on the next line.
^N
Move the cursor down one line in the same column. A count moves that many lines down.
^P
Move the cursor up one line in the same column. A count moves that many lines up.
^U
Scroll backwards half a window. A count scrolls that many lines.
$
Move the cursor to the end of the current line. A count moves to the end of the following lines.
%
Move the cursor to the matching parenthesis or brace.
^
Move the cursor to the first non-whitespace character.
(
Move the cursor to the beginning of a sentence.
)
Move the cursor to the beginning of the next sentence.
{
Move the cursor to the preceding paragraph.
}
Move the cursor to the next paragraph.
|
Move the cursor to the column specified by the count.
+
Move the cursor to the first non-whitespace character in the next line.
-
Move the cursor to the first non-whitespace character in the previous line.
_
Move the cursor to the first non-whitespace character in the current line.
0
(Zero) Move the cursor to the first column of the current line.
B
Move the cursor back one word, skipping over punctuation.
E
Move forward to the end of a word, skipping over punctuation.
G
Go to the line number specified as the count. If no count is given, then go to the end of the file.
H
Move the cursor to the first non-whitespace character on the top of the screen.
L
Move the cursor to the first non-whitespace character on the bottom of the screen.
M
Move the cursor to the first non-whitespace character on the middle of the screen.
W
Move forward to the beginning of a word, skipping over punctuation.
b
Move the cursor back one word. If the cursor is in the middle of a word, move the cursor to the first character of that word.
e
Move the cursor forward one word. If the cursor is in the middle of a word, move the cursor to the last character of that word.
h
Move the cursor to the left one character position.
j
Move the cursor down one line.
k
Move the cursor up one line.
l
Move the cursor to the right one character position.
w
Move the cursor forward one word. If the cursor is in the middle of a word, move the cursor to the first character of the next word.

 

Moving the Cursor Around the Screen

 

^E
Scroll forwards one line. A count scrolls that many lines.
^Y
Scroll backwards one line. A count scrolls that many lines.
z
Redraw the screen with the following options. “z<return>” puts the current line on the top of the screen; “z.” puts the current line on the center of the screen; and “z-” puts the current line on the bottom of the screen. If you specify a count before the ‘z’ command, it changes the current line to the line specified. For example, “16z.” puts line 16 on the center of the screen.

 

Replacing Text

 

C
Change to the end of the line from the current cursor position.
R
Replace characters on the screen with a set of characters entered, ending with the Escape key.
S
Change an entire line.
c
Change until . “cc” changes the current line. A count changes that many lines.
r
Replace one character under the cursor. Specify a count to replace a number of characters.
s
Substitute one character under the cursor, and go into insert mode. Specify a count to substitute a number of characters. A dollar sign ($) will be put at the last character to be substituted.

 

Searching for Text or Characters

 

,
Repeat the last f, F, t or T command in the reverse direction.
/
Search the file downwards for the string specified after the /.
;
Repeat the last f, F, t or T command.
?
Search the file upwards for the string specified after the ?.
F
Search the current line backwards for the character specified after the ‘F’ command. If found, move the cursor to the position.
N
Repeat the last search given by ‘/’ or ‘?’, except in the reverse direction.
T
Search the current line backwards for the character specified after the ‘T’ command, and move to the column after the if it’s found.
f
Search the current line for the character specified after the ‘f’ command. If found, move the cursor to the position.
n
Repeat last search given by ‘/’ or ‘?’.
t
Search the current line for the character specified after the ‘t’ command, and move to the column before the character if it’s found.

 

Manipulating Character/Line Formatting

 

~
Switch the case of the character under the cursor.
<
Shift the lines up to where to the left by one shiftwidth. “<<” shifts the current line to the left, and can be specified with a count.
>
Shift the lines up to where to the right by one shiftwidth. “>>” shifts the current line to the right, and can be specified with a count.
J
Join the current line with the next one. A count joins that many lines.

 

Saving and Quitting

 

^\
Quit out of “VI” mode and go into “EX” mode. The EX editor is the line editor VI is build upon. The EX command to get back into VI is “:vi”.
Q
Quit out of “VI” mode and go into “EX” mode. The ex editor is a line-by-line editor. The EX command to get back into VI is “:vi”.
ZZ
Exit the editor, saving if any changes were made.

 

Miscellany

 

^G
Show the current filename and the status.
^L
Clear and redraw the screen.
^R
Redraw the screen removing false lines.
^[
Escape key. Cancels partially formed command.
^^
Go back to the last file edited.
!
Execute a shell. If a is specified, the program which is executed using ! uses the specified line(s) as standard input, and will replace those lines with the standard output of the program executed. "!!" executes a program using the current line as input. For example, "!4jsort" will take five lines from the current cursor position and execute sort. After typing the command, there will be a single exclamation point where you can type the command in.
&
Repeat the previous ":s" command.
.
Repeat the last command that modified the file.
:
Begin typing an EX editor command. The command is executed once the user types return. (See section below.)
@
Type the command stored in the specified buffer.
U
Restore the current line to the state it was in before the cursor entered the line.
m
Mark the current position with the character specified after the 'm' command.
u
Undo the last change to the file. Typing 'u' again will re-do the change.

 

EX Commands

The VI editor is built upon another editor, called EX. The EX editor only edits by line. From the VI editor you use the : command to start entering an EX command. This list given here is not complete, but the commands given are the more commonly used. If more than one line is to be modified by certain commands (such as ":s" and ":w" ) the range must be specified before the command. For example, to substitute lines 3 through 15, the command is ":3,15s/from/this/g".

:ab string strings
Abbreviation. If a word is typed in VI corresponding to string1, the editor automatically inserts the corresponding words. For example, the abbreviation ":ab usa United States of America" would insert the words, "United States of America" whenever the word "usa" is typed in.
:map keys new_seq
Mapping. This lets you map a key or a sequence of keys to another key or a sequence of keys.
:q
Quit VI. If there have been changes made, the editor will issue a warning message.
:q!
Quit VI without saving changes.
:s/pattern/to_pattern/options
Substitute. This substitutes the specified pattern with the string in the to_pattern. Without options, it only substitutes the first occurence of the pattern. If a 'g' is specified, then all occurences are substituted. For example, the command ":1,$s/Dwayne/Dwight/g" substitutes all occurences of "Dwayne" to "Dwight".
:set [all]
Sets some customizing options to VI and EX. The “:set all” command gives all the possible options. (See the section on customizing VI for some options.)
:una string
Removes the abbreviation previously defined by “:ab”.
:unm keys
Removes the remove mapping defined by “:map”.
:vi filename
Starts editing a new file. If changes have not been saved, the editor will give you a warning.
:w
Write out the current file.
:w filename
Write the buffer to the filename specified.
:w >> filename
Append the contents of the buffer to the filename.
:wq
Write the buffer and quit.
For more elaborate list of commands, visit the following site

http://www.unix-manuals.com/refs/vi-ref/vi-ref.htm

h1

Linux:Delete Older Files

March 25, 2009

One of my friend asked to me how to delete the old backup files which are older than 1 month in linux. so i just found some ways to do it

To List all the files which exactly Seven days olders
find /path/to/directory -type f -mtime 7 -exec ls -ltr {} \;
To List all the files which are older than 7 days
find /path/to/directory -type f -mtime +7 -exec ls -ltr {} \;
To List all the files which are lesser than 7 days
find /path/to/directory -type f -mtime -7 -exec ls -ltr {} \;

To Delete files which are listed by any of the command, we just need replace the ls command with rm command.

To delete all the files which are older than one month(30 days)
find /path/to/directory -type f -mtime +30 -exec rm -f {} \;
h1

Format Currency Data in BigDecimal

February 27, 2009

A code sample which shows how to format a currency value which is stored in a BigDecimal object. The Code uses the US Currency Locale and formats the value with comma seperation.


import java.util.*;
import java.math.*;
import java.text.*;
class  MyDecimalFormatter
{
 public void formatDecimal(BigDecimal b)
 {

      NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
      double doublePayment = b.doubleValue();
      String s;
      if(doublePayment < 0)
      {
	      s = n.format(doublePayment * -1);
	      s  = "-" + s;
      }
      else
     {
	      s = n.format(doublePayment);
      }
      System.out.println(s);
 }
 public static void main(String[] args)
 {
  BigDecimal payment = new BigDecimal("-1234523423.67");
  MyDecimalFormatter mf = new MyDecimalFormatter();
  mf.formatDecimal(payment);
 }
}

Thanks kamal
h1

WSAD (WebSphear Application Developer) IDE – Shortcuts

November 18, 2008
WSAD Shortcuts
 

 

Navigational Shortcuts
Ctrl+L Go to line number.
Ctrl+I Indent the highlighted text.
Ctrl+Q Goto last modified editor position
Ctrl+K Go to next occurrence of the selected text
Ctrl+Shift+K Go to previous occurrence of the selected text
Ctrl+F6 Navigate between open Editors (or, files in an editor)
Ctrl+F7 Navigate between open Views
Ctrl+F8 Navigate between open Perspectives
Ctrl+Shift+P Navigating to Matching braces
F12 Jump to the open Editor
Ctrl+Shift+W List all files open in an editor
Alt+left arrow Back (last editing position)
Alt+right arrow Forward (next editing position)

 

Coding Assistants
Ctrl+Space Brings up coding/context assistant.
Make required selection and press enter or, double click.
After selection, Javadoc will appear in hover Help.
Ctrl+Shift+M Add import
Ctrl+Shift+O Organize Imports
Ctrl+B Executes an incremental build of a project in the navigation view
Ctrl+E Goes to the next error.
Alt+Shift+M Extract Method
Alt+Shift+R Rename method/variable
Ctrl+Shift+F Format Code
Ctrl+Shift+E Delete till end-of-line
Search Shortcuts
F3 Open Declaration of Selected Element
F4 Open hierarchy
Ctrl+F Find/replace
Ctrl+H Brings up Java search with the selected item in the search table.
Ctrl+Shift+T
Or
Ctrl+Shift+H
Type Browser:- Search a class/interface by typing it’s name
(wildcards: ‘*’ and ‘?’ can also be used)
“Ctrl+Shift+H” brings up Hierarchical Browser… works much the same as Type Browser!
Ctrl+Shift+G Search References in workspace
Ctrl+O Search data-members or methods in the Class (works similar to Ctrl+Shift+T ,i.e, type search… but the scope is only the current Class file open in the editor)
Ctrl+Shift+U Search occurrences (of selected methodName or dataMember) within the same Class file.

 

New features in Workbench
1. Users can now customize the key bindings (Key short-cuts) using preferences.
Windows-> Preferences->Workbench-> Keys
2.
Editor now keeps the Navigation history. So if you open a second editor you can use Navigate ->Back option to go back. One can also use key shortcut Alt + Left arrow.
3. Text editor has improved to provide line numbers , current line highlighting , print margins etc.
4. There is a new Ant view (Window > Show View > Ant) that makes it easier to run Ant buildfiles.
5. There is a new Ant editor that makes it easier to edit Ant buildfiles. The Ant editor provides content assist, syntax highlighting, an outline, and error reporting
h1

Java Collections – Set

November 18, 2008

Set

Set is a type of Collection in java. It is similar to List but does not allow duplicate elements

to be inserted. Set is an Interface and the Concrete Implementations which uses the Set interface

is HashSet.

Example

//Create a Set Object

Set s = new HashSet();

//Add method adds a element into the Set. This method returns a boolean

//If duplicate is found, then the element is not inserted and it will return false

//if the element is unique, it will add the element to the set and returns true

bool a = s.add(new Integer(1));

if(a) { System.out.println(”Inserted”);}

else { System.out.println(”Duplicate Element. Not Inserted”);