
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the differences between length and length () in Java?
The length is an instance variable of an array in Java whereas length() is a method of String class.
length
- An array is an object that holds a fixed number of values of the same type.
- The length variable in an array returns the length of an array i.e. a number of elements stored in an array.
- Once arrays are initialized, its length cannot be changed, so the length variable can directly be used to get the length of an array.
- The length variable is used only for an array.
Example
public class ArrayLengthTest { public static void main(String args[]) { int array[] = {1, 2, 3, 4, 5, 6, 7}; System.out.println("Length of an array is: " + array.length); } }
Output
Length of an array is: 7
length()
- The length() method is a static method of String class.
- The length() returns the length of a string object i.e. the number of characters stored in an object.
- String class uses this method because the length of a string can be modified using the various operations on an object.
- The String class internally uses a char[] array that it does not expose to the outside world.
Example
public class StringLengthMethodTest { public static void main(String args[]) { String str = "Welcome to Tutorials Point"; System.out.println("Length of String using length() method is: " + str.length()); } }
Output
Length of String using length() method is: 26
- Related Articles
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between Java classes and Java objects?
- What are the differences between JRadioButton and JCheckBox in Java?
- What are the differences between recursion and iteration in Java?
- What are the differences between GridLayout and GridBagLayout in Java?
- What are the differences between JTextField and JTextArea in Java?
- What are the differences between StackOverflowError and OutOfMemoryError in Java?
- What are the differences between the TableCellRenderer and TableCellEditor in Java?
- What are variable length (Dynamic) Arrays in Java?
- What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?\n
- What are the differences between JFrame and JDialog in Java?\n
- What are the differences between a class and an interface in Java?
- What are the differences between a MouseListener and a MouseMotionListener in Java?
- What are the differences between a JComboBox and a JList in Java?

Advertisements