Today's Offer - Java Certification Training - Enroll at Flat 10% Off.

- 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: How to Call a Method in Java?

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[0]="Testing";
TestArray =new String[20];
System.out.println(TestArray);

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];
TestArray[0]="Hello"
TestArray[1]="World";
TestArray[2]="ThisTime"

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: Hibernate Interview Questions And Answers

String [] TestArray=new TestArray[3];
TestArray[-1]="cart"; 

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: What are the Benefits of loading jQuery from CDN

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.

Conclusion

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: Spring MVC Interview Questions and Answers

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

8 days 14 Dec 2019

DevOps

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

Upcoming Class

-1 day 05 Dec 2019

Data Science

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

Upcoming Class

-1 day 05 Dec 2019

Hadoop

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

Upcoming Class

0 day 06 Dec 2019

Salesforce

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

Upcoming Class

14 days 20 Dec 2019

Course for testing

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

Upcoming Class

18 days 24 Dec 2019

QA

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

Upcoming Class

3 days 09 Dec 2019

Business Analyst

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

Upcoming Class

0 day 06 Dec 2019

SQL Server

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

Upcoming Class

3 days 09 Dec 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on Java Course

Interviews