for loop examples

Hope you had already gone through loops in java to get basic understanding of loop in Java.

Below are few examples which will make you hands on, and make you aware real use of loops. First let us consider solving below problem.

For loop example problem: Take two different arrays each of size 10. Concatinate each number of first array with each number of second array, and display each combination. Hint:Output generates 100 different numbers.
Solution: First declare & initialize two array a and b. Now each number of first array need to be concatinated with each number of second array. We need two different for loops, one to iterate thru first array, another to iterate thru second array. However, these two loops need to be nested, i..e outer for loop iterates thru first array, inner for loop iterates thru second array.

public class LoopExample{
public static void main(String args[])
{
int a[] = { 23,54,89,14,83,5,29,3,93,32};
int b[] = { 21,43,26,19,3,8,25,2,8,24};

for(int i=0;i < a.length;i++)
{
for(int j=0;j < b.length;j++)
{
String tmp = a[i]+""+b[j];
System.out.println(tmp);
}
}
}
}

Also this problem can be solved using while loop or do while loops.

Another Problem: Repeat above problem, but print only the numbers which are multiples of 3.
Solution: As shown above, after the number gets created, it is in String format, to convert it to int, use Integer.parseInt() method, Further check if it is multiple of 3, using modulo operator.

public class LoopExample{
public static void main(String args[])
{
int a[] = { 23,54,89,14,83,5,29,3,93,32};
int b[] = { 21,43,26,19,3,8,25,2,8,24};

for(int i=0;i < a.length;i++)
{
for(int j=0;j < b.length;j++)
{
String tmp = a[i]+""+b[j];
int val = Integer.parseInt(tmp);
if(val%3==0)
{
System.out.println(tmp);
}
}
}
}
}

Note that each iteration does not create any entry on the Stack, but a method call creates an entry in Stack. For more details, refer what is stack memory and when an entry is added to stack memory
You may also like to read:
Core Java Interview Questions
List of Operators in java
Java Interview Questions
What are Wrapper classes
More for loop examples

String object creation with and without new

What is the difference between below statements?
1. String str = “Hello”;
In this case, “Hello” memory is allocated during compile time itself, and str refers to “Hello” String literal address. Also note that

String str_a = “Hello”;
String str_b = “Hello”;
Both of these refers to same String object, and another object does not get created for str_b.

2. String str1 = new String(“Hello”);

In case of new version, “Hello” is dynamically allocated during run time, and “Hello” gets memory allocate on Heap.
String str1 = new String(“Hello”);
String str2 = new String(“Hello”);
Both of these refers to different String object, and another object gets created for str2

Below is an example showing the same


public class Stringnew {
public static void main(String args[])
{
	String str_a = "Hello";
	String str_b = "Hello";

	String str1 = new String("Hello");
	String str2 = new String("Hello");

	if(str_a==str_b)//== checks address location
	{
		System.out.println("if(str_a==str_b)");
	}
	else
	{
		System.out.println("else (str_a==str_b)");
	}

	if(str_a.equals(str_b))//equals() checks content
	{
		System.out.println("if str_a.equals(str_b)");
	}
	else
	{
		System.out.println("else str_a.equals(str_b)");
	}

	if(str1==str2)//== checks address location
	{
		System.out.println("if str1.equals(str2)");
	}
	else
	{
		System.out.println("else (str_a==str_b)");
	}

	if(str1.equals(str2))//equals() checks content
	{
		System.out.println("if str1.equals(str2)");
	}
	else
	{
		System.out.println("else str1.equals(str2)");
	}
}
}

Output:
if(str_a==str_b)
if str_a.equals(str_b)
else (str_a==str_b)
if str1.equals(str2)

You may also like to read:
Difference between == and equals()
Can String be inherited?
How to override toString()

Difference between == and equals() method for a String

Result of using == and equals() method between two String objects is different.
== checks whether both String objects are referring to same address location.
equals() method checks whether the contents of both String objects are same.

public class Stringequals{

public static void main(String args[])
{
	String s1 =  new String("Hello");
	String s2 = new String("Hello");

	String str="Hello";

	String str1="Hello";

	//s2 and s3 refer to same address location
	String s3 = s2;

	if(s1==s2)//== checks address location
	{
		System.out.println("if(s1==s2)");
	}
	else
	{
		System.out.println("else (s1==s2)");
	}

	if(s2==s3)//== checks address location
	{
		System.out.println("if(s2==s3)");
	}
	else
	{
		System.out.println("else (s2==s3)");
	}

	if(s1.equals(s2))//equals checks contents of s1, s2
	{
		System.out.println("if s1 equals s2");
	}
	else
	{
		System.out.println("else s1 equals s2");
	}

	if(str==str1){
		System.out.println("if(str==str1)");
	}
}
}


Output:
else (s1==s2)
if(s2==s3)
if s1 equals s2
if(str==str1)

You may also like to read:
Inherit From String class
How to override toString() method
Difference between String and StringBuilder
String object creation with and without new