How to Declare Multiple Variables in a Loop in Java


The example demonstrates how to declare multiple variables in a loop by java declaration block.

Source Code

package com.beginner.examples;

import java.util.Arrays;

public class DeclareMultipleMariablesInLoopExample {

	
	public static void main(String[] args) {
		
		String test = "Hello world!";
		
		char[] chs = test.toCharArray();
		
		//Declare multiple variables in a loop
		for(int start=0,end=chs.length-1;start<end;start++,end--)
		{
			//Switch heads and tails
			char temp = chs[start];
			chs[start] = chs[end];
			chs[end]=temp;
		}
		
		System.out.println(Arrays.toString(chs));
		

	}

}

Output:

[!, d, l, r, o, w,  , o, l, l, e, H]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments