Java - How to find length of an Array

We can find the length of an array by using length variable.

1. length  variable:-

length is a final variable applicable applicable for arrays.

length variable represent the size of an array.

 example:

                                   int [ ] x = new int [6]; 

 System.out.println(x.length);    o/p    is  6 .

                                int [ ] x =new int [10];

System.out.println(x.length);    o/p    is  10 .

System.out.println(x.length());  

                                            Compile-Time Error:cannot find symbol
                                                 Symbol: method length()
                                                  location: class int [ ]

But Do not Apply length( ) method here.Because length( ) is applicable for String.

2.length (  ) method:-

length( ) is a final method appicable for String Objects/

length( ) return the numbers of character present in the String.  

example:

String s="javabychetan";

System.out.println(s.length());   o/p is     12 

System.out.println(s.length);  

                                             Compile-Time Error:cannot find symbol
                                                    Symbol: variable length
                                                     location: class java.lang.String


 

Back:Part 5. Shortcut method for Array Declaration,Creation,Initialization into a single line

Next:Chapter 5:Types of Variables

Home:

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com