Monday, September 17, 2012

StringBuilder() Constructor with no characters in it

public class StringBuilderConstructor {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
       
        //(a)
        System.out.println("Initial Capacity : " + sb.capacity());
        System.out.println("Initial Size : " + sb.length());
       
        sb.append("1234567890123456");
      
        //(b)
        System.out.println("Capacity After Sixteen Additions : " + sb.capacity());
        System.out.println("Size After Sixteen Additions : " + sb.length());
       
        sb.append("7");
      
        //(c)
        System.out.println("Capacity When it exceeds Sixteen Numbers : " + sb.capacity());
        System.out.println("Size When it exceeds Sixteen Numbers : " + sb.length());
       
        sb.append("12345678901234567");
      
        //(d)
        System.out.println("Capacity When it Equals ThirtyFour Numbers : " + sb.capacity());
        System.out.println("Size When it is less than or Equal to ThirtyFour Numbers : " + sb.length());       
   
    }
}

The  Answers Are :
----------------------
(a)
Initial Capacity : 16
       Initial Size : 0

 
(b)
Capacity After Sixteen Additions : 16
       Size After Sixteen Additions : 16
 
(c)
Capacity When it exceeds Sixteen Numbers : 34
       Size When it exceeds Sixteen Numbers : 17
 
(d)
Capacity When it exceeds ThirtyFour Numbers : 34
Size When it is less than or Equal to ThirtyFour Numbers : 34

Sunday, September 16, 2012

StringBuilder Constructors

StringBuilder has 4 Constructor types for it

They are
1) StringBuilder()
2) StringBuilder(CharSequence seq)
3) StringBuilder(int capacity)
4) StringBuilder(String str)

After Writing the Program, in the Middle of the Constructors press CTRL + SPACE button toghether
Now We Can able to See which are and all the types of constructors available in StringBuilder.








Saturday, September 15, 2012

How to Convert Ascii to Character in Java ?

        (a)  
        For Example for Converting ascii to character in Java use the following code
        char characterValue = (char)asciiValue;
       
        class CharacterValue
        {
               public static void main(String[] args) {
               int asciiValue='100';
               char characterValue = (char)asciiValue;
               /*
                  The Above Statement Shows that the ascii value will be converted into its
                  Corresponding Character Value and hence the char character = (char)ascii;
                  will print out the value as 'd'
              */
               System.out.println("CharacterValue for ascii 100 is : " + characterValue);
               }
        }
       
        Answer : 
        CharacterValue for ascii 100 is : 'd'

        (b)
        For Example for Converting character to ascii in Java use the following code
        int asciiValue = (int)characterValue;
       
        class AsciiValue
        {
               public static void main(String[] args) {
               char characterValue='d';
               int asciiValue = (int)characterValue;
               /*
                  The Above Statement Shows that the character value will be converted into its
                  Corresponding Ascii Value and hence the int asciiValue = (int)characterValue;
                  will print out the value as '100'
              */
               System.out.println("AsciiValue for character 'd' is : " + asciiValue);
               }
        }
       
        Answer : 
        AsciiValue for character 'd' is :100

Saturday, September 8, 2012

Sample code for Sorting String in ascending order

Try out these Programs

(a)
package pack1;
import java.util.*;
import java.lang.*;
import pack1.Orderly;
public class Example{
   public static void main(String args[]){
        String name = "";
        ArrayList list=new ArrayList();
        Orderly orderly = new Orderly ();
        try
        {
              list=orderly.displayRecordNew();
              Collections.sort(list);
              /*
                 Sorts the array list
                 If We have to Sort the Name in Descending Order then we have to uncomment the 
                 below commented lines.
                 Comparator r = Collections.reverseOrder();
                 Collections.sort(list,r);
              */
              Iterator l1 = list.iterator();        
              while(l1.hasNext()){
                  orderly  = (Orderly)l1.next();
                  name = orderly.getOrderlyName();
                  System.out.println("Name is " + name);
              }
        }
        catch(Exception e)
        {
        }
   }
}

(b)
package pack1;
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
class Orderly implements Comparable<Orderly>{
    public String nameDesig = "a";
    private String name;
    public void setOrderlyName(String name){
        this.name = name;
    }
    public String getOrderlyName(){
        return name;
    }
    //Overriding the compareTo method
    public int compareTo(Orderly o){
        return (this.name).compareTo(o.name);
    }       
    public ArrayList displayRecordNew() throws Exception
    {
        File file = new File("C://Users//Radio1//Desktop//Names.txt");
        FileInputStream fis = null;
        StringTokenizer st;

        ArrayList showRecord=null;           
        try {
            showRecord=new ArrayList();               
            fis = new FileInputStream(file);
            /*
               FileInputReader since we have to sort out the names in ascending order from the file                        Names.txt
            */
            BufferedReader d = new BufferedReader(new InputStreamReader(fis));
            String readingFromFile = "";
            String intoArrayList = "";
            while((readingFromFile = d.readLine()) != null)
            {
                /*
                   Now We are using StringTokenizer to read the names from the FileList since  the Names  are separated using comma in the File and the resultant one by one we will store it in the ArrayList which we will sort it out using Collections.sort(list); 

                   Since this class (Orderly) is imported in the class Example it will automatically sort out the List  and we will get the Names in Ascending Order on further iteration of the List
                */
                st = new StringTokenizer(readingFromFile, ",");
                while (st.hasMoreTokens())
                {
                    Orderly orderly=new Orderly();
                    intoArrayList = st.nextToken();
                    orderly.setOrderlyName(intoArrayList);
                    showRecord.add(orderly);
                }
            }
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return showRecord;
    }
}

(c)Names.txt will have the following contents
Seshadri,Lakshmi,Padma,Balaji,Koushick,Hari,Sudarshan,Anirudh,Sriram,Rajagopalan,Mahesh,Zombia,Karthick,Raghavendra,Mahati,Kamran

(d)Now on running the Example.java file we get the following as Result(Sorted Out)
Name is Anirudh
Name is Balaji
Name is Hari
Name is Kamran
Name is Karthick
Name is Koushick
Name is Lakshmi
Name is Mahati
Name is Mahesh
Name is Padma
Name is Raghavendra
Name is Rajagopalan
Name is Seshadri
Name is Sriram
Name is Sudarshan
Name is Zombia

Friday, August 3, 2012

Edit name of label in blogger


When I started writing this blog, I wanted to create a label called ‘Java’ but typed ‘java’ by mistake. I could not change the first letter to capitals at the label section. Follow the below to change Label names. There is no direct way to manage/edit labels.

  • Login into your blogger account
  • Click on posts, and select the posts for which you want to change the label.

  • Click on ‘Label Selected posts’ on the top and click the on the label which you want to change/delete and say 'ok'

 You would get a message ‘Label java has been removed from the selected posts’
  • With the posts still selected, Click on New Label

  • Add the new Label.


7Here are the new changes

Disable Right Click on Blogger blogs


Paste the below code to disable right click anywhere in your blog.
  • Login into your blogger account
  • Goto Layout -->Add a Gadget [ You could add the gadget anywhere ]
  • Select HTML/JavaScript.
  • Add title for the script as ‘Right Click Disabled’ and paste the below code.

<script language=javascript>
<!--

function clickonNetScapeVer4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
return false;
}
}
}

function clickonIEVer4(){
if (event.button==2){
return false;
}
}

if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown= clickonNetScapeVer4;
}
else if (document.all&&!document.getElementById){
document.onmousedown= clickonIEVer4;
}

document.oncontextmenu=new Function("return false")


// -->
</script>

What is the purpose of JVM (Java Virtual Machine)?


JVM stands for Java Virtual Machine. It acts as an intermediate layer between the Java program and the actual hardware. A ‘virtual’ machine in which the Java code is executed or runs the compiled Java code. The Java code is compiled and converted to byte code by the compiler. The JVM understands the compiled code and executes the java byte code. 


The main reason JVM was developed was for code portability thus providing platform independence. Let us understand portability – take the below HelloWorld.java, the program is first converted to byte codes by the compiler. These byte codes need to be understood by the machine to execute it. If JVM is present in the machine, it understands the byte code, takes care of memory management and interaction with the actual hardware. JVMs are developed for different hardware and software platforms. All that the user has to make sure is that the machine contains a JVM, irrespective of the platform where the code will run.


Below is an example and flow of compilation and execution.

Here is a sample code for HelloWorld.java

public class HelloWorld {
 
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
 
}

Let us see the steps in the compilation and execution of this java program.