Home > Java > javaTutorial > How to declare variables in java

How to declare variables in java

(*-*)浩
Release: 2019-05-27 16:27:30
Original
8518 people have browsed it

In Java programming, every declared variable must be assigned a type. When declaring a variable, you should first declare the variable type, and then declare the variable name. The following demonstrates how variables are declared.

How to declare variables in java

double salary; 
int age; 
Boolean op;
Copy after login

The first item is called the variable type, and the second item is called the variable name. A semicolon is required as it is the end of a Java statement.

Different variables of the same type can be declared on one line or on different lines. If they are to be declared on the same line, separate the different variables with commas,

For example:

int studentNumber,people;
Copy after login

You can assign a value to the variable while declaring it, or you can assign a value after declaring it.

For example:

int a=1; //声明时赋值 
int a; a=1; //声明后赋值
Copy after login

Note: During the running of the program, the value in the space changes, and this memory space is called a variable. For the convenience of operation, this space is given a name, called a variable name, and the value in the memory space is the variable value. Therefore, after applying for memory space, the variable may not have a value. If you want the variable to have a value, you must put a value in it.

For example:

"int x;" A variable is defined but no value is assigned, that is, memory space is applied for but no value is placed; "int x=5; " Not only did it apply for memory space, but it also put a value, which is 5.

Note: For variables without assignment, the system will initialize them according to the following default values.

Declaring a variable is just like defining a variable, but the name is different. It has the same meaning as calling method C in Java and calling it function.

The above is the detailed content of How to declare variables in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template