Java Variables and Data Types

We’ll cover the following

  • Introduction
  • Strongly Typed Language
  • The Simple Types
  • Declaring Java Variables
  • Syntax
  • Declaring two or more variables
  • Examples
  • Dynamic Initialization


This chapter examines three out of two Java’s most fundamental elements: Variables and Data Types. As with all modern programming languages, Java supports
several types of data. You may use these types to declare variables and to create

Java variables makes Java programs general.

Variables are the key to creating programs that can perform calculations. A variable is defined by the combination of an identifier, a type, and an optional initializer.

All variables have a scope, which defines their visibility, and a lifetime.

Strongly Typed Language

Java is a strongly typed language. Let’s see what this means.

Indeed, part of Java’s safety and robustness comes from this fact.

First, every variable has a type, every expression has a type, and every type is strictly defined.

Second, all assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility.

The Java compiler checks all expressions and parameters to ensure that the types are compatible.

The Simple Types

Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float,
double, and boolean. These can be put in four groups:
■ Integers This group includes byte, short, int, and long, which are for whole-
valued signed numbers.
■ Floating-point numbers This group includes float and double, which represent
numbers with fractional precision.
■ Characters This group includes char, which represents symbols in a character
set, like letters and numbers.
■ Boolean This group includes boolean, which is a special type for representing
true/false values.

We will see these one by one in next chapters.

Declaring Java Variables

In Java, you must explicitly declare all variables before using them.

If you misspell a variable name, the compiler can detect your mistake and display a compiler error.


The basic form of a variable declaration is this:

type name;

Here are some examples:

int x; 
String firstName; 
double radius;

In these examples, variables named x, firstName, and radius are declared.

The x variable holds integer values, the firstName variable holds String values, and the radius variable holds double values.

The variable declarations end with semicolons.

Variable names follow the same rules as other Java identifiers.

A variable name can be any combination of letters, numerals, or underscores and dollar signs – but must start with a letter.

We can start variable names with lowercase letters and capitalize the first letter of individual words within the name.

The following are some legal Java variable name in camel case:

  • firstName
  • salesTaxRate

Declaring two or more variables

You can declare two or more variables of the same type in a single statement by separating the variable names with commas.

For example:

int x, y, z;

Here three variables of type int are declared, using the names x, y, and z.

As a rule, we should avoid declaring multiple variables in a single statement.

Our code is easier to read and maintain if you give each variable a separate declaration.

int a, b, c;            // declares three ints, a, b, and c.
int d = 4, e, f = 5;    // declares three more ints, initializing
                        // d and f.
byte z = 22;            // initializes z.
double pi = 3.14159;    // declares an approximation of pi.
char x = 'x';           // the variable x has the value 'x'.


A variable is a named memory location that may be assigned a value by your program.

The value of a variable may be changed during the execution of the program.

The next program shows how a variable is declared and how it is assigned a value.

The program also illustrates some new aspects of console output.

   Here is another short example.
   Call this file "".
public class Main {
  public static void main(String args[]) {
    int num; // this declares a variable called num

    num = 100; // this assigns num the value 100

    System.out.println("This is num: " + num);

    num = num * 2;

    System.out.print("The value of num * 2 is ");

When you run this program, you will see the following output:

This is num: 100
The value of num * 2 is 200

The first new line in the program is shown here:

int num; // this declares a variable called num

This line declares an integer variable called num.

Java requires that variables be declared before they are used.

Java defines several data types, including integer, character, and floating-point. The keyword int specifies an integer type. In the program, the line

num = 100; // this assigns num the value 100

assigns to num the value 100.

In Java, the assignment operator is a single equal sign.

The next line of code outputs the value of num preceded by the string “This is num:”.

System.out.println("This is num: " + num);

In this statement, the plus sign causes the value of num to be appended to the string that precedes it, and then the resulting string is output.

The next line of code assigns num the value of num times 2.

Java uses the * operator to indicate multiplication.

After this line executes, num will contain the value 200.

Here are the next two lines in the program:

System.out.print ("The value of num * 2 is ");
System.out.println (num);

Several new things are occurring here.

First, the built-in method print() is used to display the string “The value of num * 2 is “.

This string is not followed by a new line.

This means that when the next output is generated, it will start on the same line.

The print() method is just like println(), except that it does not output a new line character after each call.

Both print() and println() can be used to output values of any of Java’s built-in types.

Dynamic Initialization

Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared.

For example, here is a short program that computes the length of the hypotenuse of a right triangle given the lengths of its two opposing sides:

// Demonstrate dynamic initialization.
public class Main {
  public static void main(String args[]) {
    double p = 3.0, b = 4.0;

    // h is dynamically initialized
    double h = Math.sqrt(p * p + b * b);

    System.out.println("Hypotenuse is " + h);

Here, three local variables-p, b, and h-are declared.

The first two, p and b, are initialized by constants. However, h is initialized dynamically to the length of the hypotenuse using the Pythagorean theorem.

The program uses another of Java’s built-in methods, sqrt(), which is a member of the Math class, to compute the square root of its argument.

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java Identifiers

Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy

One comment

Leave a Reply

Your email address will not be published.