Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

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:

c1.png

for(int i=0;i<a.length;i++)

The above for loop is known as the declaration of the array.

Example2:

c2.png

 

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:

c3.png

String functions:

  1. length(): Return the length of the string.

{

String s=”Rahul”

String l=s.length();

System.out.println(l);

}

//Output: 5

  1. charAt(int): Returns a character at specified postion. Index of the string starts from 0.

  2. 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

  1. equals(String str): checks whether string object and str and return true if they are same else return false.

  2. Equlsignorecase(string str): same as above function except that this function ignores the case and checks for the equality of the strings.

  3. indexOf(String str): Returns the index of the specified string in the string object.

  4. lastIndexOf(string str): same as above functions but checks from back of the string.

  5. 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

  1. toLowerCase( ): converts the given string to Lowercase.

{

String s=”Rahul”;

String s1=s.toLowerCase();

System.out.println(s1);

}

//Output: rahul

  1. toUpperCase( ): converts the given string to Uppercase.

{

String s=”rahul”;

String s1=s.toLowerCase();

System.out.println(s1);

}

//Output: Rahul

  1. trim( ): Removes the leading and trailing spaces of the string.

 

Example:

c4.png

The above program shows the strings in Java.


Varun Kapila

Comments   

0 #1 s 128 2022-02-18 08:00
Hi there! This is my first visit to your blog! We are a
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!
Quote

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner