When to use .length vs .length()
They're two completely different things.
.length
is a property on arrays. That isn't a method call.
.length()
is a method call on String
.
You're seeing both because first, you're iterating over the length of the array. The contents of the array are String
, and you want to add up all of their lengths, so you then call the length on each individual String
in the array.
Difference between size and length methods?
size()
is a method specified in java.util.Collection
, which is then inherited by every data structure in the standard library. length
is a field on any array (arrays are objects, you just don't see the class normally), and length()
is a method on java.lang.String
, which is just a thin wrapper on a char[]
anyway.
Perhaps by design, Strings are immutable, and all of the top-level Collection subclasses are mutable. So where you see "length" you know that's constant, and where you see "size" it isn't.
What's the difference between length and length()?
.length;
directly accesses a field member.
.length();
invokes a method (i.e. an accessor) to access a field member.
in the case of String
, this is to be expected since it's immutable.
Why Java's String has the method length() rather than property length (like an array?)
Because length()
is defined in CharSequence
interface, which is implemented by String
. Interfaces can't define properties in Java. Also, this makes it more interchangeable with other types.
In many methods, you may rely only on CharSequence
interface instead of a real String
, then you can use an instance of CharBuffer
, Segment
, String
, StringBuffer
or StringBuilder
in the call site.
See also: CharSequence documentation.
When to use length and when to use length()
length
--- arrays (int[], double[], String[]
) ---- to know the length of the arrays.
length()
--- String related Object (String, StringBuilder etc
)to know the length of the String
size()
--- Collection Object (ArrayList, Set etc
)to know the size of the Collection
as length
is not a method its works on arrays not on objects.
size()
is a method which work on collection frameworks as i said up there .
now String is not a direct array and also not a Collection that's why we also need a different one which is length()
.
When to use type.length-1; and type.length(); in java
You want to use oldArray.length
usually in a for loop call, because as in your example,
for(int i = 0; i < oldArray.length; i++) {
//Executes from i = 0 to i = oldArray.length - 1 (inclusive)
}
Notice how i
goes from 0
up until oldArray.length - 1
, but stops exacty at oldArray.length
(and doesn't execute). Since arrays start at position 0
instead of 1
, old.Array.length is a perfect fit for the number that i
should stop at. If arrays started at position 1
instead, for loops would look something like this:
for(int i = 1; i <= oldArray.length; i++) {
//Executes from i = 1 to i = oldArray.length (inclusive)
}
oldArray.length - 1
is usually to access the last element:
int[] oldArray = {1,2,3,4,5};
int lastElement = oldArray.length - 1; // 4
oldArray[lastElement] // returns last element, 5
Although this is usually when you would use length - 1
vs length
, there are many other cases where you would also want one over the other, and thus there is no real specific answer. But don't worry, keep coding, you'll get this hang of this soon ;)
Related Topics
Java - Best Way to Grab All Strings Between Two Strings? (Regex)
Java: "Final" System.Out, System.In and System.Err
How to Replace a Character in a String in Java
Java Inheritance - Instance Variables Overriding
How to Calculate a Time Span in Java and Format the Output
Read Whole Text Files from a Compression in Spark
Multithreaded Quicksort or Mergesort
How to Use Regex in String.Contains() Method in Java
_ (Underscore) Is a Reserved Keyword
How to Add Checkboxes to Jtable Swing
Randomly Select an Item from a List
Any Way to Share Session State Between Different Applications in Tomcat
How to Call a Superclass Method Using Java Reflection
Eclipse Is Confused by Imports ("Accessible from More Than One Module")
Java Socket/Serialization, Object Won't Update
Parsing Xml with Regex in Java
Java 8 Date and Time: Parse Iso 8601 String Without Colon in Offset