[Java] Difference between length, length () and size ()

Content of this article

When getting the length of an array or a character string in Java, it gets confused as to whether it is length, length () or size (), so I summarized that area for study.

Conclusion

length length() size()
ArrayLength of StringLength of collectionNumber of elements of

Difference between length, length (), size ()

length

length is used to get the length of the array.

int[] a = new int [100];
a.length;    // 100
a.length();  //Compile error

Since the length field, which is a member of the array, is used, () is unnecessary.

length() length () is used to get the length of a string.

String str = "aiueo"
str.length;    //Compile error
str.length();  // 5

() Is required because the length method is used to get the length of the character string. Even in the case of full-width characters, it counts the number of characters correctly (humanly).

String zenkaku = "AIUEO"
zenkaku.length();  // 5

size()

You can get the number of elements in a collection (List, Set, etc.) with size ().

ArrayList<Integer> b = new ArrayList<>();
for(int i = 0; i < 100; i++){
  b.add(i);
}
b.size()  //100

size () seems to be a method defined in the Collection class. So you can use size () for all collections.

References

http://www.kab-studio.biz/Programing/JavaA2Z/Word/00000290.html

Recommended Posts

[Java] Difference between length, length () and size ()
[Java] Difference between == and equals
[Java] Difference between Hashmap and HashTable
[JAVA] Difference between abstract and interface
[Java] Difference between array and ArrayList
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java beginner] Difference between length and length () ~ I don't know ~
Difference between final and Immutable in Java
[For beginners] Difference between Java and Kotlin
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
Difference between vh and%
Difference between i ++ and ++ i
[Java] Understand the difference between List and Set
Difference between next () and nextLine () in Java Scanner
[Java] Difference between "final variable" and "immutable object"
Difference between product and variant
Difference between redirect_to and render
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
[Java] Difference between static final and final in member variables
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between Java EE and Jakarta EE?
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Terminal] Difference between irb and pry
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between assignment of basic type variable and assignment of reference type variable
[Java] Check the difference between orElse and orElseGet with IntStream
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Stack Overflow Error and Out Of Memory Error
Is short-circuit evaluation really fast? Difference between && and & in Java
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Java] What is the difference between form, entity and dto? [Bean]
Conversion between Kotlin nullable and Java Optional
Note: Difference between Ruby "p" and "puts"
Relationship between kotlin and java access modifiers
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable