How to assign same value to multiple variables in Java?
UPDATED: 02 January 2016
Tags:
J2SE
Following excerpt shows assignment of same value to multiple variables in Java.
Source code
Output
Source code
/** * Example: Assign same value to multiple variable. * * @author javaQuery * @date 2nd January, 2016 * @Github: https://github.com/javaquery/Examples */ public class AssignSameValue { public static void main(String[] args) { int red = 0, green = 0, blue = 0; String color = "white"; // args[0] if ("white".equalsIgnoreCase(color)) { /* assign same value to red, green, blue */ red = green = blue = 255; } else if ("black".equalsIgnoreCase(color)) { /* assign same value to red, green, blue */ red = green = blue = 0; } else { /* other else if...else for more color combinations */ } System.out.println("Color code:"); System.out.println("red = " + red + ", green = " + green + ", blue = " + blue); } }
Output
Color code: red = 255, green = 255, blue = 255
Tags:
J2SE
0 comments :