Tutorials References Exercises Bootcamps Videos Menu
Sign Up Create Website Get Certified Upgrade

Java Tutorial

Java HOME Java Intro Java Get Started Java Syntax Java Output Java Comments Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Math Java Booleans Java If...Else Java Switch Java While Loop Java For Loop Java Break/Continue Java Arrays

Java Methods

Java Methods Java Method Parameters Java Method Overloading Java Scope Java Recursion

Java Classes

Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Constructors Java Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java Inner Classes Java Abstraction Java Interface Java Enums Java User Input Java Date Java ArrayList Java LinkedList Java HashMap Java HashSet Java Iterator Java Wrapper Classes Java Exceptions Java RegEx Java Threads Java Lambda

Java File Handling

Java Files Java Create/Write Files Java Read Files Java Delete Files

Java How To

Add Two Numbers Count Words Reverse a String Sum of Array Elements Area of Rectangle Even or Odd Number

Java Reference

Java Keywords Java String Methods Java Math Methods

Java Examples

Java Examples Java Compiler Java Exercises Java Quiz Java Certificate

Java Declare Multiple Variables

Declare Many Variables

To declare more than one variable of the same type, you can use a comma-separated list:


Instead of writing:

int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);

You can simply write:

int x = 5, y = 6, z = 50;
System.out.println(x + y + z);

Try it Yourself »

One Value to Multiple Variables

You can also assign the same value to multiple variables in one line:


int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);

Try it Yourself »

Test Yourself With Exercises


Fill in the missing parts to create three variables of the same type, using a comma-separated list:

 x = 5 y = 6 z = 50;

Start the Exercise