Memory Tricks on How to Remember When to Use Length, Length(), and Size() in Java

Tremaine Eto
Jan 12 · 5 min read
Original Photo by Tachina Lee on Unsplash; Java logo fair use; text by Tremaine Eto.

If you ever want to really bring the fun to a Zoom party, then you can quiz your friends on when you use length vs length() vs size() in Java. Admittedly, this kind of conversation would probably only go well in niche programmer friend circles, but alas.

But seriously: in school or on the job, it’s not always easy to remember which one to use when you simply want to get how long or how many elements are in something.

This may seem like an intro-to-programming concept, but honestly I wouldn’t be surprised if most of the people you ask don’t know exactly. After all, it’s mostly something developers’ IDEs would fill out or something they usually just Google — or even just guess by trial-and-error.

Quiz

Actually, let’s test out how much you know right now with a little quiz for fun. For each one, write down or type whether you can use length, length(), or size(). Then, comment on this story with how many you got right! Answers will be at the end of this article.

  1. int[] nums = new int[20];
  2. String str = "this is fun";
  3. char[] chars = new char[2];
  4. HashMap<String, Integer> map = new HashMap<>();
  5. PriorityQueue<String> priorityQueue = new PriorityQueue<>();

I hope that gave you a good understanding of where you stand!

In this article, we’ll go over when you use each one and hopefully include some mnemonic devices to help you remember should you ever need it; after all, connection is a key element in memorization.

length

length is used when you want to get the length of arrays. Examples of this would be if the object is:

  • int[]
  • String[]
  • Double[]
  • etc.

Example

So let’s say you’re given an array of integers called ages. You can get the number of ages in the array with the following code:

Basically, in Java, length is a final variable that’s just available for use for this fixed-value array, and so you can call it.

How to remember length

I think a solid way to remember this is that if your object has square brackets — [] — in it, then you don’t need round brackets (or parentheses, in other words). Just one set of them will do.

You can remember the keyword length too because the square brackets are straight-edged like the letter l in length.

If you can’t remember those, the tricks for remembering length() and size() will provide you with some process of elimination.

length()

length() is used when you want to get the number of characters in a String. As far as common uses go, that’s pretty much the only use.

Example

Let’s say you’re given a username, and you have to determine how many characters are in it.

String essentially has a length() method implemented where the return value is precisely the number of characters in the String.

How to remember length()

Harking back to the inverse of how to remember length, in this case we do not have any brackets at all in our initial Object of String. There is no square bracket or round brackets at all, and it feels a bit…lonely.

In the previous section’s case with arrays with square brackets already, I said that we don’t need any more brackets; just one set will do. Here, we do need more brackets in order to form that one set that will do. Thus, we call the length() method to bring us to one set total.

size()

size() is used for a couple of useful built-in data structures in Java. Namely, it’s most commonly used for, among other data structures:

  • HashSet
  • HashMap
  • ArrayList

Examples

First, let’s show how you call size() for HashSet.

In this example, we filled a HashSet with four Integer values, and we use size() to output its size of 4.

Now, we’ll look at how you’ll call size() for HashMap.

In this example, we filled a HashMap with five mappings, and we use size() to output its size of 5.

By the way, wondering why used .put() in this example using HashMap but .add() in the previous example using HashSet? This other mnemonic device article I wrote in The Startup that might interest you:

size() for ArrayList works the exact same way as the above two examples.

How to remember size()

The way to remember size() is that when you’re using a built-in data structure, use size(). In other words, the first letter of structure is s, which so happens to be the first letter in size(). The arrays and Strings we dealt with for length and length() aren’t data structures, but size() works for pretty much every structure whether you’re working with HashMap, HashSet, ArrayList, PriorityQueue, Stack, LinkedList, and on and on.

Answers to Quiz

  1. You can use length. Remember that if you already have square brackets [], we don’t need any more (round) brackets!
  2. You can use length(). We’re first of all working with a String which might be enough to get to remember that it has its own method, but if that’s not enough, see that our variable had no brackets — we need one to create a set of brackets, so we need the () in length().
  3. You can use length. We’re working with an array here that already has square brackets, so we don’t need any more brackets.
  4. You can use size(). We’re working with a built-in Java data structure, so remember that you can use size() which also starts with a s.
  5. You can use size(). Even though you may not see PriorityQueue in the wild too often, we’re still working with a built-in Java data structure, so remember that you can use size() which also starts with a s.

Why I think these can help

I know these seem sort of silly and at times a bit of a reach, but just re-read them and at least see where I’m coming from.

I honestly think that any sort of building of connections is important when it comes to memorizing something that was actually somewhat arbitrarily chosen at the time these were implemented. I wouldn’t tell you something that I don’t use!

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store