Java Array:
This topic covers the concept of an array. Till now, you have learned the basics of Java by which you can create basic java programs. What if you want to store different numbers of the same type?
Will you declare and initialize step by step for each number? But that will be a very lengthy process, time consuming and memory taking as well. This is where the array comes into action.
Here are the topics if you directly want to jump:
Watch this Java video by Intellipaat:
What is Array?
- An array is a collection of data which is of similar type.
- Array Elements will be stored in contiguous memory locations.
- You can declare a number of variables in a single line instead of declaring individually. Example: int arr[]= {1,2,10,20};
- In the above example, arr[0]=1, arr[1]=2, arr[3]=10, arr[4]=20. It reduces the coding complexity and makes easy to access elements.
Java is being used widely, know in this blog on Cross-Platform Mobile Technology for Android and iOS using Java.
Array Declaration
Declaration of array involves creating a reference variable and declaring the data type for that reference variable.
Syntax:
[modifiers] <data type> <refVariable>[]; or
[modifiers] <data type>[] <refVariable>; or [modifiers] <data type> []<refVariable>;
Example:
Here, arr is a reference variable of integer array type. It stores 8-bytes of memory. Reference arr contain null or address of an array object.
Array Construction
Syntax: <refVariable>=new <datatype>[size];
Example:
- After declaration, arr1=new int[3];
This can store three elements in the arr1 address.
As the size is five, so it can store five elements.
- Length variable can be used with array reference variables to return the number of elements.
- Example:
arr1.length returns 3
arr2.length returns 5
Get Hired as a Java Developer with These Java Collection Interview Questions!
Array Initialization
Once an array is constructed then array elements can be initialized with default values as per the data type.
You can initialize array elements with your own values.
Example:
Int Arr1[]=new int[2];
Arr1[0]=10;
Arr2[1]=20;
Arr[0] represents the first element in the array, arr[1] represents the second element in the array, and so on if any, such as Arr[2] represents third and so on.
We can directly declare and initialize array elements such as int arr[]={1,2,3,4};
The size of this array will be automatically stored according to the array elements.
Array can be of n dimensions. The basic array dimensions we use are 1-D and 2-D. But it can also be 3-D, 4-D, etc.
Prepare for the next interview with these Hibernate Interview Questions.
What is 1-D Array?
int a[]=new int[6];
This is how 1-D array looks like, and we already know how to initialize.
Program to understand 1-D Array
class ArrayOne{
public static void main(String args[]){
// array declaration, construction and initialization
int a[]=new int[4];
a[]={1,2,3,4};
System.out.println(“Array Length”+” “+a.length); // print 4
for(int i=0;i<a.length;i++){
System.out.print(“ “+a[i]); // to print array elements
}
}
}
Output:
Array Length 4
1 2 3 4
Watch this Java video by Intellipaat:
What is 2-D Array?
2-D Array contains rows and columns depending upon the size provided.
Example:
int arr[][]=new int[2][3];
Say size1=2 and size2=3. Here, size1 represents number of arrays to construct and size2 represents number of elements in each array.
arr[0][0] |
arr[1][0] |
arr[0][1] |
arr[1][1] |
arr[0][2] |
arr[1][2] |
Here, two arrays are created each of which contain three elements.
Program to understand 2-D Array
class ArrayTwo{
public static void main(String args[]){
int arr[][]=new int[2][3];
System.out.println(“Enter array elements”);
arr[0][0]=1; arr[0][1]=2;
arr[0][2]=3; arr[1][0]=4;
arr[1][1]=5; arr[1][2]=6;
for(int i=0;i<arr.lenght;i++){
for(int j=0;j<arr.length;j++){
System.out.print(arr[i][j]+” “);}
}
}
}
Output:
Enter array elements
1 2 3 4 5 6
Note:
- If you try to access elements that is greater then or equal to the array length, the following exception will show:
Further, you will understand in the Exception Handling topic.
What is String?
- String is a collection of characters.
- In Java, String is a pre-defined class
- String is immutable. Therefore, it can’t be changed once the value is assigned to a string variable.
Example:
String name = “intellipaat”;
OR
char[] name={‘i’,’n’,’t’,’e’,’l’,’l’,’i’,’p’,’a’,’a’,’t’};
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
The Java String is immutable i.e. it cannot be changed but a new instance is created. For mutable class, you can use StringBuffer and StringBuilder class.
String Creation
There are two ways to create String object:
- By string literal
- By new keyword
By String Literal
It is created by using double quotes. The area in which string objects are stored is known as string constant pool.
Example:
String name=”intellipaat”;
By new keyword
There is another way to create string object using new keyword like as follows:
String name=new String(“intellipaat”);
Get 100% Hike!
Master Most in Demand Skills Now!
Java String class methods
The java.lang.String class contains number of methods which are used to perform various operations:
Method |
Description |
int length() |
Returns string length |
char charAt(int index) |
Returns char value for the specified index |
static String format(String format, Object… args) |
Returns formatted string |
String substring(int StartingIndex) |
Returns substring for given start index |
String substring(int StartingIndex, int EndingIndex) |
Returns substring for given start index and end index |
boolean contains(CharSequence s) |
Returns true or false after matching the sequence of char value |
boolean equals(Object another) |
Check the equality of string with object |
String toLowerCase() |
Returns string in lowercase. |
String toUpperCase() |
Returns string in uppercase. |
boolean isEmpty() |
Check string is empty or not. If yes than return true otherwise false |
String trim() |
Returns trimmed string omitting leading and trailing spaces |
String replace(char old_character , char new_character) |
Replaces all occurrences of specified char value |
String replace(CharSequence old, CharSequence new) |
Replaces all occurrences of specified CharSequence |
String split(String regex) |
Returns splitted string matching regex |
int indexOf(int ch) |
Returns specified char value index |
int indexOf(int ch, int fromIndex) |
Returns specified char value index starting with given index |
int indexOf(String substring) |
Returns specified substring index |
int indexOf(String substring, int fromIndex) |
Returns specified substring index starting with given index |