November 27, 2011

Anonymous Arrays Code Example

Anonymous Arrays in Java are those arrays that are constructed without specifying the size nor assigned a reference. Anonymous arrays are created on-the-fly when needed and cannot be re-accessed (since there is no reference available to the array object), therefore they become eligible for garbage-collection as soon as the code that uses it completes it.

package info.icontraining.core;

public class AnonymousArrays {

   public static void main(String[] args) {
  
      int[] arr1 = {1, 2, 3, 4, 5};
  
      for(int i: arr1) {
         System.out.print(i);
      }
  
      for(int i: new int[]{1, 2, 3, 4, 5} ) {
         System.out.print(i);
      }  
   }
}

November 26, 2011

Writing Unicode characters in Java

Create the following class in the src folder of the Java Project in Eclipse


package info.icontraining.core;

public class CharExample {

   public static void main(String args[]) {
 
      char c='\u0915';
      System.out.println(c);
   } 
}


To enabled Unicode character display in the console of Eclipse, make the following 3 configurations in Eclipse:

1) Open the "Run" menu > Choose "Open Run Dialog ..." > Choose your application and the particular class (in this case, CharExample) > Click on the "Common" tab > Choose "Console Encoding" as "Other" and "UTF-8" from the drop-down list

2) Next, go on the "Arguments" tab > Copy the string "-Dfile.encoding=UTF-8" in the VM arguments text area (without the double quotes). Click "Apply" and then "Close".

3) Finally, go to the "Window" menu > Choose "Preferences ..." > Next, choose "General" > Then "Workspace" > In the "Text File Encoding" select "Other" and "UTF-8" from the drop-down list