Friday, 10 April 2020

How to Initialize Multiple Variables to same value in Java?

Step 1: Declare Variables.
String s1, s2, s3;

Step 2: Initialise all the variables to a value using = operator.
s1 = s2 = s3 = "Hello";

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  String s1, s2, s3;

  s1 = s2 = s3 = "Hello";

  System.out.println("s1 = " + s1);
  System.out.println("s2 = " + s2);
  System.out.println("s3 = " + s3);
 }
}

Output
s1 = Hello
s2 = Hello
s3 = Hello



You may like

No comments:

Post a Comment