Java Variable and Java Identifires - IndianTechnoEra
Latest update Android YouTube

Java Variable and Java Identifires

 

Java Variables

What is variables in Java?

Variables are containers for storing data values.

There are different types of variables in java, for example:

    String - stores text, such as "Hello". String values are surrounded by double quotes

    int - stores integers (whole numbers), without decimals, such as 123 or -123

    float - stores floating point numbers, with decimals, such as 19.99 or -19.99

    char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

    boolean - stores values with two states: true or false


Declaring (Creating) Variables

To create a variable, you must specify the type and assign it a value:

Syntax

type variableName = value;


type: java's type like int or String

variableName: name of variable like name, num 

= : Equal sign is used to assign values


Example

String name = "John";

int num = 23;


Variable and value

Create variable and assign value

int myNum = 15;


Create variable  then assign value

int myNum;

myNum = 15;


Create variable and assign, and re-assign value

int myNum = 15;

myNum = 20;  // myNum is now 20


Create multiple variable and assign value with sight data type

int a=12, b = 23;


Note: Final Variables

Provident from overwrite values use final keyword and then cant re-assign any variable


Example:

final int myNum = 15;

myNum = 20;   // error


Java Print Variables

It is used to combine both text and a variable, use the + character  To combine both text and a variable, 

Example

String name = "John";

System.out.println("Hello " + name);

int x = 5;

int y = 6;

System.out.println(x + y); // Print the value of x + y


Declare Many Variables

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

Example

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


One Value to Multiple Variables

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

Example

int x, y, z;

x = y = z = 50;

System.out.println(x + y + z);


Java Identifiers

All Java variables must be identified with unique names and These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: Better for names in order to create understandable and maintainable code:

Example

int minutesPerHour = 60;


Rules for naming variables are:

Names can contain letters, digits, underscores, and dollar signs

Names must begin with a letter

Names should start with a lowercase letter and it cannot contain whitespac

Names can also begin with $ and _ (but we will not use it in this tutorial)

Names are case sensitive ("myVar" and "myvar" are different variables)

Reserved words (like Java keywords, such as int or boolean) cannot be used as names

Example:

Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.