Grab Deal : Flat 30% off on live classes + 2 free self-paced courses! - SCHEDULE CALL

- Java Blogs -

Difference between Array Length vs String Length () Function in Java

Java is an object-oriented programming language used to write robust and secure apps for businesses. Today, this is taken as the most demanding programming platforms available so far and highly secure too that is considered as the primary requirement for businesses. At the same time, the aspiring minds looking for a career in Java should have depth understand of each of the OOPs concepts and basic programming syntax. Here, we will focus on two most popular functions i.e. Array. length Keyword and String.length() Function and how are they different in Java.

Let us dive deep into the topics with a basic introduction to variable and the function ahead ….

A Java array can hold an arbitrary number of elements and it depends on how the array object is created? If one wants to perform any operation on array then he must know the length of an array, like in case if you want to sort the numbers that are stored in an array, then he must know the length of the array in order to execute the for loop for full array length.

String.legth () is a function in Java that is used to know the length of String. It returns the number of characters that are stored in the String Variable. It returns the length of the String that is equal to the number of 16-bit Unicode String characters.

This article will help you know about these two Java keywords and after reading the post you will come to know about differences in both of them. The keywords are discussed with examples to make you clear about both of them.

Array length variable

As we have stated in the above-section that a Java array can hold any number of elements, so to perform any function on Array object you may need to identify the length of Java array. A Java object does not have length method, while arrays in Java have the final length field that contains the total number of characters, stored in that particular array. To execute any code on the arrays, you must know the length of that particular array. The syntax to know the length of an array is given below:

Read: Java ArrayList: What It Is & How To Create An Arraylist In Java


The variable of an array will return a numeric value, equal to the number of characters that are stored in that array.

An Example to Use the Array Length is given below –

/* To Store the Length of Array */
public class JavaArrayLength
 public static void main(String args[])
 String[] fruitArray={“Apple, Guava, Banana”};
int arrayLength = fruitArray.length;
System.out.println(“The length of Array is + ”, arrayLength);

The expected output of the above program is: 3

Here, in this code, the array length will be retrieved from the array. length variable that is of numeric type. As here the array has a pre-specified number of elements, so the length of the array is known, but in case if the array length will not be known then, in that case, the length has to be determined or retrieved. It can be done with the help of following code:

/* To find highest value element from array */
public class javaArrayHighValue{
 private static int highValue(int[] anArray){
  int highValue = anArray[0];
   int arrayLength = anArray.length;
   for (int i=1; i< - arrayLength -1; i++){ int value = anArray[i]; if (value > highValue){
return highValue;
Public static void main(String[] args)
Int[] testArray={190, 150, 200, 67};
System.out.println(“The maximum value is: ” +maxValue(testArray));

String Length Method of Java

In Java, String class has a function that is known as String length that is used to know the length of the String that is stored in the string array. Here the string that has the length of 16-bit Unicode can be identified by the function and displayed as the length of String array. The Syntax of the String object to know the length of its array is:

Read: React JS Interview Questions - Ace Your React Interviews in 2024

Public int length ()

No parameter is to be assigned in this function and it returns the length as an integer value.  You can understand the use of String.length function of Java with the help of the following program:

Public class String_lengthex{
  Public static void main(String[] args){
 String S1=”This is a new Program”;
String S2=”TestProgram”:
Int stlength=S1.length();
System.out.println(“Length of First String is: ” + stlength);
System.out.println(“Length of another string is: ” + S2.length());

The Output of the above program will be:

Length of First String is 21 Length of another string is: 11

In this way, string length function is used to know the total number of characters that are stored in the array. The length function of String class is an inbuilt function that returns an integer value. We can say that user need not write a separate function to know the String array length, just be using the built-in function it can be easily identified and used for programs.

Difference Between length and length () in Java

You might be well familiar with both of these Java keywords. Here we are going to point out the specific differences between both of these keywords so that you do not get confused:

Read: What is Exception in java? Type of Exception Handling in Java

Length: It is used to calculate the array size as shown in the above program. You may use length keyword along with array to know the length of an array. They are one of the earliest Java primitives. It is a final public field. Being an immutable field user cannot access the field normally.

Length (): This String function is used to know that length of the String array.

In Java, there are total three keywords to know the array size. One is length that is used with Arrays, other is the length () that is used with Strings and one more is the size () that is used with ArrayList:

  • In Java, Arrays do not use methods and are one of the earliest primitive used to know the length of Array variables. Array Length field cannot be accessed as any normal field.
  • String class objects are immutable and so we cannot use the final field with the String objects.
  • In the case of String, class accessor methods have to be used by the class objects. Length () method of the interface for String APIs. It is not designed as a field of String class and even it can ensure consistency and compatibility.
  • Length function provides the exact capacity of the array. In case if you have declared an array of size 10 and give or assign only 5 values to it then still the size of an array will be returned as 10. There is another function say size () that returns 5 in such case means the total number of array elements in the array.
  • These variables and functions are designed by different developers at the different time and have various purposes.

Related Post

  1. Java Tutorial 
  2. Java Interview Questions
  3. Java Developer Career Path
  4. Java Developer Resume

Final Words

Here, we can say that Array has the length variable that can be used to determine the length of an array, while length function is used with the String class object that can be used to determine the String length. Array variables are mutable, that is once the size of these variables is declared it can be changed during the program or code, while String objects are immutable that means once declared they cannot be modified. In case, if you will modify the String objects, then you will have to create the new one and the older one will not be modified.

Read: A Complete Guide to Advanced Java Programming

fbicons FaceBook twitterTwitter lingedinLinkedIn pinterest Pinterest emailEmail


    JanBask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.

  • fb-15
  • twitter-15
  • linkedin-15


Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

4 days 19 Jul 2024

QA Course


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

4 days 19 Jul 2024

Salesforce Course


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

1 day 16 Jul 2024

Business Analyst Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst Course

Upcoming Class

5 days 20 Jul 2024

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

5 days 20 Jul 2024

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

4 days 19 Jul 2024

DevOps Course


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

8 days 23 Jul 2024

Hadoop Course


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

4 days 19 Jul 2024

Python Course


  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

12 days 27 Jul 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

4 days 19 Jul 2024

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

39 days 23 Aug 2024

 Tableau Course


  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau Course

Upcoming Class

4 days 19 Jul 2024

Search Posts


Receive Latest Materials and Offers on Java Course