Date:

Share:

Java Variables – Codeamy: Learn Programming

Related Articles

Variables are a box or container in the memory of the device for storing some data values ​​in it.

In Java, we have different types of Java variables for different data types are as follows:

  1. int – It stores integers without decimal points.
  2. float – It stores floating point numbers.
  3. blacken – It stores within it a single character and characters surrounded by individual quotes like ‘A’ or ‘b’.
  4. wire – It stores the sequence of characters or we can say text and is surrounded by double quotes like “codeamy” or “divya”.
  5. Boolean – It stores only two values ​​that are true or false.

Announcement of variables

To create java variables, we must follow a syntax in which we must specify the type of container and assign it data or value.

Syntax for variables

Variable type_name = value;

When type is one of the types as we learned above like int, float, char etc. variable_name is a unique name for a container such as X or Divya. So we can identify it uniquely. The equal sign (=) is used to assign the value to variable_name.

int divya = 10;

We can also declare a variable without assigning a value, and assign later.

int x;

x = 10;

System.out.println (x); // Output: 10

We can also replace the value assigned as

int x = 10;

x = 20;

System.out.println (x); // Output: 20

Display of variables

To print or view the value of Java variables. The Java language will provide us with the println () method.

For example,

int divya = 27;

System.out.println (“Roll No.” + divya);

To combine the string with variables, we use the + operator.

String lastName = “Soni”;

String firstName = “Divya”;

Name string = first name + last name;

System.out.println (name);

We also use + to combine one or more variables.

But in the case of numbers it will add the variables. Let’s see the example below.

int a = 10;

int b = 20;

System.out.println (a + b); // The output will be 30 and not 10 20

Declare many variables

We can declare many variables in one line but with the same type with the use of a comma (,) between them.

for example,

int x = 10, y = 20, z = 30;

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

Final in Java

final is a keyword or word stored in java. It is also marked as permanent. If you do not want to change your assignment value to variableName, enter the final keyword before the statement.

final int x = 01;

x = 02; // Output: This will generate an error that you will not be able to reassign the final variable.

Java IDs

All variables are identified by a unique name. These unique names are known as identifiers. There are some rules for setting a valid ID otherwise it creates a compilation error.

Rules for IDs in Java

  1. The first character must be a letter (AZ or az), digits, a dollar sign ($) or an underscore (_).
  2. Any Java variable must start with a letter or an underscore which means it cannot start with digits (0-9).
  3. Saved words or keywords are not used as variable names. For example, String int = “veer”; Is invalid because int is a reserved word.
  4. Variable case sensitive names for both uppercase and lowercase letters are treated differently.
  5. Blank space or white space in variable names are not allowed.

In the article above, We learn about Java variables and also learn about Java IDs, I think the whole article clarifies your doubt. In the next tutorials, I will share details about Java data types.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles