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

- Java Blogs -

What is Java String? How to Declare String Array in Java with Example

Java String is one of the most libraries by Java developers. For many functions and various purposes Java String objects are used by the programmers. Java String is defined under java.lang package. All strings that are created in Java are of String class type. String functions of Java are immutable that means they cannot be instantiated. In this article we are going to discuss the way that is used to declare Java String arrays to create and use String objects that can store Strings. There are some common and standard ways that are used to declare Java Strings and in the next section of this article we are going to discuss these methods.

What is Java String?

Java String is a class that contains many functions that are used to operate and processes string variables. In Java Strings are handled as an array, that can store a fixed number of values and these values can be easily manipulated. We can use a Java array as an array of immutable objects. In order to use a String array, we must know the way in which they are declared. Following are a few of the standard methods that are used to declare Java array:

1). Simple Declaration of String Arrays 

Just like integer arrays the String array is also declared by using square brackets. The square bracket that is used to declare the arrays can be put at two places and both of them are accepted by the Java compiler. In a first way we put the square bracket after the name of the variable that is used to store String object as shown in the following syntax:

String testarray[];

In another way to declare the String array we put the square bracket after the data type that                     is String, just like shown below:

String[]  TestArray;

Above two declarations are considered similar by the Java compiler and have the same effect. In above examples we have just declared the two variables without any initial value or array size. Some of the modern Java compilers may warn you for not initializing the array. Here a single pair of the brackets is used that symbolizes one-dimensional array.

Read: What Is The Process To Run A Java Program In CMD?

2). String Array Declaration with Specific Size

As an array is the data structure that can store multiple values of similar data type so sometimes it is also referred to as lists. At the same time Array differs by lists as they are of some specific size, while lists size can be changed and controlled at runtime. Due to fixed size a String array instance can hole only a fixed number of elements. This size cannot be changed until a new instance will be created. Following syntax is used to declare a Java String array with fixed size:

String[]  TestArray=new TestArray[10];

In above example a String array, named TestArray is declared of size 10. The String object or variable TestArray is not null but contain individual content with the value Null after this declaration. The size of the array that is declared under square bracket is fixed and cannot be changed throughout the variable or object lifecycle. But in case if in the middle of the program we need to change the size of the array then we can do it in the following way:

String[] testarray=new testarray[10];
testarray=new string[20];

Here in such declaration the first line is used to declare the Java String array of size 10. Size of such array can only be changed, if it will be assigned to a new array, one problem here can be that you may lose pre-stored array data. In the following example you can see this:

String[] TestArray=new String[10];
TestArray =new String[20];

Here in the above code no output will be displayed on the screen because the new array has been initialized before the print statement, so ole values will be overwritten. Here we would also like to tell you that in case of arrays the square brackets can also be used to display individual array content. Like in the below case:

String [] TestArray=new TestArray[3];

In Java indexing starts with zero, while in case of other languages the indexing starts with another number like one. When array index starts with zero then the last array index is usually total array size minus one. Like if the array size is three then its last index will be one less than the size of the array. In any case if the array index will be any negative number then it will throw an exception. Like in the following case the exception will be thrown as:

Read: Free Jquery SLider Carousal Plugin

String [] TestArray=new TestArray[3];

In above case the exception will be thrown as shown below:

Exception: class java.lang.ArrayIndexOutOfBoundException: -1

This exception will be thrown in the case if array index will be negative. Moreover if the array index will go beyond the maximum index then still the same error will be thrown by the statement.

3). String Array Declaration with some initial value

We can declare an array with some initial value. In such cases at the time of array or string array declaration some values will be specified that will be called initial values of the array elements. In the following example you can see the values:

String motorArray[]={"Maruti","Fiat","Hyundai","Honda"};

In the case when arrays are initialized with some values then in such cases you need not specify the size of the array. The values that you will use to initialize the array will determine the array size easily. Like in above case the size of the String array will be four as the total number of values stored in the array are four. In the case when the print statement will be used along with index values then all values that are stored in the corresponding index will be displayed in particular order. Like in above example, the value Maruti is stored at the index 0, while value Honda is at index 3.

Hence we can say that there are above-mentioned three ways to declare String arrays in Java. Though Strings can be used in many cases and for each case the way in which they are initialized and used is different and will depend on the requirement. Unlike List Arrays have a specific size, so you will have to use the size in order to determine String size. To process the String arrays you may need for loop or iterations.

Read: Serialization of Java Objects to XML Using XML Encoder/Decoder

String Class Functions of Java

There are some ready to use String class functions in Java. These functions can be directly used by String class objects. Some functions of the String class use arguments and return values, while others can be used directly and do not return any value. These functions are called pre-defined functions and can be directly used by the user for String class objects. Following are a few such functions of the String class that can be used by anyone:

Here each of the listed function is used for the specific purpose. Like the purpose of each one is defined below:

  • String Concatenation: To concatenate two Strings this function is used and the syntax for this function is str.concat(String object). Through this function values of the two String objects will be concatenated
  • String Length: If you will have to identify the length of a String then you can use this function. Syntax of this function is str.length(). Here you need not pass an argument in the function, just by specifying the string object name for which the function is being called. Here the String length can be calculated automatically and you can know the length of String
  • String Comparison: To compare two Strings that they contain the similar values or not you can use this method. The comparison is being done to check that whether two Strings contains same values or not we use this method. The syntax of this one is str.CompareTo(String), here as the argument you may either pass any String or String object with which the value will be compared. If the two Strings are found identical then value 1 will be returned by this function otherwise 0 will be returned.
  • Lowercase to UpperCase: In case if you want to convert the String literals from lower case to upper case then you can use this function. This is also a predefined function that can be used to convert from lower case to upper case. The two syntaxes for this are str.toLowerCase and str.toUpperCase.
  • The method ends with: This function is basically used to check whether any String ends with a specific value or not. In other words, you can check that whether String has any particular suffix or not. If the suffix is found then the function returns True else it returns false.


You can say that String is one of the most useful classes of Java. To deal with various types of String cases many String functions are available that can be used directly, moreover, String declaration can also be done by many types as per requirement.

Read: Java Programmer Resume Template Sample - Guide for Fresher & Experienced

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

-0 day 14 Jun 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

-0 day 14 Jun 2024

Salesforce Course


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

Upcoming Class

-0 day 14 Jun 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

-0 day 14 Jun 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

-0 day 14 Jun 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

7 days 21 Jun 2024

DevOps Course


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

Upcoming Class

1 day 15 Jun 2024

Hadoop Course


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

Upcoming Class

7 days 21 Jun 2024

Python Course


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

Upcoming Class

-0 day 14 Jun 2024

Artificial Intelligence Course

Artificial Intelligence

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

Upcoming Class

15 days 29 Jun 2024

Machine Learning Course

Machine Learning

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

Upcoming Class

28 days 12 Jul 2024

 Tableau Course


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

Upcoming Class

7 days 21 Jun 2024

Search Posts


Receive Latest Materials and Offers on Java Course