Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Length vs. Length Method in Java
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings
The fast well prepared banner

Length vs. Length Method in Java

Object-oriented programming languages are popular among software engineers preparing for coding interviews; among them, Java is one of the most widely used languages by software developers. While preparing for tech interviews at FAANG and other tier-1 tech companies, it is important to brush up on your basics. In this article, we will discuss the difference between the length attribute and the length() method in Java.

Here’s what we’ll cover:

  1. What is the length variable in Java?
  2. What is the length() method in Java?
  3. Difference between length and length()
  4. Example problems on the length and length() in Java

What Is the “length” Variable in Java?

In Java, the number of elements an array can hold, or the size of the array, is called its length. You can use this length attribute or variable to find the size of an array in Java by using the dot operator with the array name.

For example, Array.length will give you the length of the array named “Array.”

What Is the “length()” Method in Java?

length() in Java is a final method, which is applicable for string objects. You can use it to find the number of characters in a string.

For example, string.length() will return the number of characters in “string.”

Differences Between “length” and “length()” in Java

Here’s a summary of the key differences between length and length():

Code Example:

public class IK{

    public static void main(String[] args)

    {

        // a is an integer array of size 10-

        int[] a= new int[10];

        System.out.println("Size of an array a is "+ a.length);

 

        // Variable Str is a String object.

        String Str= "InterviewKickstart";

        System.out.println("Size of the String is "+ Str.length());

    }

}


Output

Size of an array a is 10

Size of the String is 18

Problems on length vs. length() in Java

Q. What will be the output of the following code?

Code 1: 

public class IK{

    public static void main(String[] args)

    {

        String[] S= { "ONE", "TWO", "THREE", "FOUR" };

        System.out.println(S.length);

    }

}


Output: 4


Explanation: In the above code, the variable “S” is an array of type strings. So, S.length can be used to get the length of the given array. Hence, the output is 4.

Code 2:

public class IK{

    public static void main(String[] args)

    {

        String[] S= { "ONE", "TWO", "THREE", "FOUR" };

        System.out.println(S.length());

    }

}


Output: 

error: cannot find symbol

symbol: method length()

location: variable S of type String[]


Explanation: Method length() is defined for Strings. In the above code, variable S is an array of type string, and thus method length() can not be used on it. 

Code 3:

public class IK{

    public static void main(String[] args)

    {

        String[] S= { "ONE", "TWO", "THREE", "FOUR" };

        System.out.println(S[0].length());

    }

}


Output: 3

Explanation: In the above code, S[0] is a String object; it has stored the string "ONE," and hence the length() method can be used on it to get its length.

Recommended Reading

  1. Split() String Method in Java
  2. How to Reverse a String in Java
  3. Overriding in Java
  4. Collections Sort in Java

Ready to Nail Your Next Coding Interview?

If you’re looking for guidance and help with getting your prep started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

----------

Article contributed Omkar Deshmukh


Last updated on: 
October 27, 2023
Author

Dipen Dadhaniya

Engineering Manager at Interview Kickstart

Attend our Free Webinar on How to Nail Your Next Technical Interview

Square

Length vs. Length Method in Java

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar