Saturday, July 28, 2012

How to Set Heap Memory in Eclipse


A)Go to Windows Preferences
B)Go to Java Installed JREs
C)On that click jre6 or jre7 whichever is available and then Edit will get enabled
D)Go Inside Edit and write -Xms512m -Xmx1024m in Default VM Arguments
E)For Most The Limit Does Not Exceed 1GB or 1024Mega Bytes, at the max 2GB
F)Can’t declare in Decimals, Will show the error Invalid maximum heap size : - Xmx1.5g



Add caption





[Real Time Example] of String vs(StringBuilder or StringBuffer)

String variables are used whereever the use of variables is for throughout the application and hence that is why Sun by default has given String as immutable so that String variables used for various calculations in the program can be reused again and again.
But Whereas in the Case of Large SQL Statements you have to only use the statement once in the program and hence those variables memory space can be deleted and retrieved back if we use StringBuffer or StringBuilder, using there delete method,

Therefore LargeSql Statements must always be used in StringBuffer or StringBuilder.
whereas All the reusable variable be declared as String Variables

How to create Your Own Exception in Java ?


1)Create a class called (ExceptionCaught) extending Exception Superclass
2)Override its Constructor with a String or an int variable
3)Override the Object classes toString method with the instance String or int variable of the class and use it by returning back the value.It Will be useful when System.out.println(e) is called.
4)Whereever you feel like Checked Exception might occur in Your Main Class (Use throws ExceptionCaught for methods since you already know it is a Checked Exception) just use throw to throw your Own Custom defined Exception and that throw new ExceptionCaught will call our Custom Defined Exception Constructor and Steps 2-3 follows.
5)Now in catch block, when you write System.out.println(e), it automatically goes to
System.out.println(e.toString()) and thus getting the result (which is already stored in the Step3).
(A)
package pack2;                                                                              
public class ExceptionCaught  extends Exception{                                                                                
            private String InJail;                                                     
            ExceptionCaught (String CaughtIn){                                         
                        InJail = CaughtIn;                                                     
            }                                                                           
            public String toString(){                                                  
                        return "Inside ExceptionJail is [[["+InJail+"]]]";                     
            }                                                                           
}                                                                              
                                                                               
(B)
package pack2;
public class ExceptionDemo{
      private static String name;
      static void UsualMethod(String a) throws ExceptionCaught{
            if(a.equals("3"))
                  throw new ExceptionCaught(a);
      }
      public static void main(String args[]){
            try
            {
              for(int suspectedthiefs=1;suspectedthiefs<=5;suspectedthiefs++)
              {
                    name = ""+suspectedthiefs;
                    UsualMethod(name);
                    System.out.println(" "+suspectedthiefs+", not a thief");
              }
            }
            catch (Exception e) {
                  System.out.println(e + " and he remaining have been detained for further Questioning");
            }          
      }
}

The Result is
----------------
1 not a thief
2 not a thief
Inside ExceptionJail is [[[3]]]and remaining have been detained for Questioning

CANNOT MAKE A STATIC REFERENCE TO THE NON-STATIC FIELD NAME



fixed by Calling private String name as private Static String name;

 
Now on putting the instance variable as static we will get the Correct Result
package pack2;
import java.lang.*;
import java.util.*;
public class What_static_void_main {
/*
Note that Only on Declaring the instance variable as static the program gets compiled and runs, otherwise even if we declare the variable as  public the program will not get compiled
*/
      private static String name="";
      public void setOrderlyName(String name){
        this.name = name;
      }
      public String getOrderlyName(){
        return name;
      }    
      public static void main(String[] args) {
            What_static_void_main ws = new What_static_void_main();
            ws.setOrderlyName("Balaji");
            System.out.println(name);
      }
}

 


CANNOT CALL A NON-STATIC METHOD INSIDE A STATIC METHOD


package pack2;
import java.lang.*;
import java.util.*;
public class What_static_void_main {
      private String name;
      public static void main(String[] args) {
          public void setOrderlyName(String name){
              this.name = name;
            }
            public String getOrderlyName(){
              return name;
            }
      }
}


next step is....


NOW THE FOLLOWING ERROR OCCURS
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
      void is an invalid type for the variable setOrderlyName
      Syntax error on token "(", ; expected
      Syntax error on token ")", ; expected
      Cannot use this in a static context
      Syntax error on token "String", @ expected
      getOrderlyName cannot be resolved to a type
      Syntax error, insert "enum Identifier" to complete EnumHeaderName
      Syntax error, insert "EnumBody" to complete BlockStatement

      at pack2.What_static_void_main.main(What_static_void_main.java:7)

StringBuilder vs StringBuffer

StringBuilder was added in JDK 1.5 and is not synchronized and hence is fast, while StringBuffer comes from earlier versions and is synchronized and therefore is slower.Otherwise all the methods of StringBuilder are the same as StringBuffer.
package pack2;
import java.lang.*;
import java.io.*;
import java.util.*;

public class BuilderVsBuffer {
    public static void main(String[] args) {
        int loops = 123456;
        long t;
        {
            StringBuilder sb = new StringBuilder();
            t = System.currentTimeMillis();
            for (int i = loops; i-->0;) {
                sb.append(" ");
            }
            System.out.println("StringBuffy.. I am very fast You See I have taken Only "+ (System.currentTimeMillis() - t) + " MilliSeconds" );
        }
        {
            StringBuffer sb = new StringBuffer();
            t = System.currentTimeMillis();
            for (int i = loops; i-->0;) {
                sb.append(" ");
            }
            System.out.println("unforntly, u are the Gen 1.5x , but ppl trust me Since i'm thread safe(have a acount in synchronized Locker) @" + (System.currentTimeMillis() - t) + "MilliSeconds");
        }
    }
}
The Result is
----------------------
StringBuffy.. I am very fast You See I have taken Only 31 MilliSeconds
unforntly, u are the Gen 1.5x , but ppl trust me Since i'm thread safe(have a acount in synchronized Locker) @62MilliSeconds