Objective:
In the previous article looping constructs and conditional statements in Java, we have seen different types of loops and conditional statement that are used in java. In this article we will learn Arrays and strings in Java.
Arrays:
An array is the data structure which hold the sequential value of the same type.You can create an array by using the new operator with the following syntax:
arrayRefVar = new dataType[arraySize]
The way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for 10 integer elements and assigns the array to the anArray variable.
// create an array of integers
anArray = new int[10];
If this statement is missing, then the compiler prints an error like the following, and compilation fails:
ArrayDemo.java:4: Variable anArray may not have been initialized.
The next few lines assign values to each element of the array:
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // and so forth
Each array element is accessed by its numerical index:
System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);
Alternatively, you can use the shortcut syntax to create and initialize an array:
int[ ] anArray = {
100, 200, 300,
400, 500, 600,
700, 800, 900, 1000
};
Here the length of the array is determined by the number of values provided between braces and separated by commas.
Example 1:
for(int i=0;i<a.length;i++)
The above for loop is known as the declaration of the array.
Example2:
Strings:
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings.
String is the class in java to store string data.
You can assign string data simply by defining the string object and assign the string like this:
String str;
String str=”apps2fusion”
String str= new String(“apps2fusion”);
you can concatenate two strings using + symbol.
For Example:-
String str1=”Hello”;
String str2=”world”;
String str3=str1+str2;
System.Out.println(“concatenated string is: “+str3”)
//Output:
HelloWorld
Example of string:
String functions:
-
length(): Return the length of the string.
{
String s=”Rahul”
String l=s.length();
System.out.println(l);
}
//Output: 5
-
charAt(int): Returns a character at specified postion. Index of the string starts from 0.
-
concat(string str): It concatenates the string object. It is same as using “+” operation for concatenation.
{
String s=”Rahul Jain”
String s1=s.concat( “ India”);
System.out.println(s1);
}
//Output: Rahul Jain India
-
equals(String str): checks whether string object and str and return true if they are same else return false.
-
Equlsignorecase(string str): same as above function except that this function ignores the case and checks for the equality of the strings.
-
indexOf(String str): Returns the index of the specified string in the string object.
-
lastIndexOf(string str): same as above functions but checks from back of the string.
-
replace(char ch,char ch1): Replaces the character ch with ch1 in the string.
{
String s=”Rahul Jain”
String s1=s.replace(‘a’,’z’);
System.out.println(s1);
}
//Output: Rzhul Jzin
-
toLowerCase( ): converts the given string to Lowercase.
{
String s=”Rahul”;
String s1=s.toLowerCase();
System.out.println(s1);
}
//Output: rahul
-
toUpperCase( ): converts the given string to Uppercase.
{
String s=”rahul”;
String s1=s.toLowerCase();
System.out.println(s1);
}
//Output: Rahul
-
trim( ): Removes the leading and trailing spaces of the string.
Example:
The above program shows the strings in Java.
Comments
team of volunteers and starting a new initiative in a community
in the same niche. Your blog provided us valuable information to work on.
You have done a outstanding job!
RSS feed for comments to this post