Ads Sponsored By Google :)

Monday, 28 January 2013

difference between String|array length and|vs length() function|method in java with example


length in java string length in java array length in java length of string in java length of array in java
length of a string in java length function in java length method in java array length method in java
length method in java string length method in java example string length without using length method in java difference between length and length() in java difference between length and length method in java with example



 length variable Vs length():

          length:              
                      length variable is used for finding the size of an array.
                     It   is used for finding size of array. Consider an array a[] of String
        
         For Example:
                String a[]={“1”,”2”,”3”}
                System.out.println(a.length); //  a.length it returns size of array “a” as 3

               length():         
                       Length() is used for finding the size of String and size of file.

      For Example:
                String s2[]={ “Servlets”,”JavaJavax”,”Javax”,”Java”};
                System.out.println(s2[1]);//size of string as it returns 7


      For Example:

          // Demo.java
         public  class Demo {
         public static void main(String args[]){

         String s1[]={ “java”,”JavaJavax”,”Javax”};
         String s2[]={ “Servlets”,”JavaJavax”,”Javax”,”Java”};
         System.out.println(s1.length+”is length of s1 “); // s1.length is valid but s1[2].length is 
                                                                                 //invalid
         System.out.println(s2[2].length()+”is length of s2 “); //s2[2].length() is valid but  
                                                                                                //s2.length() is invalid
            }
        }


No comments:

Post a Comment

LinkWithin