Difference between length and length() in java 2013

Go down

Difference between length and length() in java 2013

Post by Admin on Sun Sep 15, 2013 4:02 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on"><b>length()</b> - This is a <b>method</b> in java. It is a static method of String class. It returns the length of a string object i.e. number of characters stored in an object.</br> <b>length </b>- This is a <b>instance variable of array</b> of array in java. It returns the length of an array i.e. number of elements stored in an array.</br> length is used only for array where as length() is defined for String class.</br></br> <b>Why array use the static variable whereas string class use the method?</b></br>Once arrays are initialized, its length cannot be changes, so a length variable can directly be used to get the length of an array.</br>String uses the method because the length of a string can be modified using the various operations on an object. Also the String class internally uses a char[] that it does not expose to the outside world.</br></br> <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">public class LengthJava {<br /><br /> public static void main(String args[]) {<br /> String name = "Kumar";<br /> int array[] = { 1, 2, 3, 4 };<br /> System.out.println("Length of string using length() method - " + name.length());<br /> System.out.println("Length of array using length keyword - " + array.length);<br /> }<br />}</code></pre></br></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on"><b>length()</b> - This is a <b>method</b> in java. It is a static method of String class. It returns the length of a string object i.e. number of characters stored in an object.</br> <b>length </b>- This is a <b>instance variable of array</b> of array in java. It returns the length of an array i.e. number of elements stored in an array.</br> length is used only for array where as length() is defined for String class.</br></br> <b>Why array use the static variable whereas string class use the method?</b></br>Once arrays are initialized, its length cannot be changes, so a length variable can directly be used to get the length of an array.</br>String uses the method because the length of a string can be modified using the various operations on an object. Also the String class internally uses a char[] that it does not expose to the outside world.</br></br> <pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">public class LengthJava {<br /><br /> public static void main(String args[]) {<br /> String name = "Kumar";<br /> int array[] = { 1, 2, 3, 4 };<br /> System.out.println("Length of string using length() method - " + name.length());<br /> System.out.println("Length of array using length keyword - " + array.length);<br /> }<br />}</code></pre></br></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum