Constants and variables are two of the most basic elements in Java programs. The value of a constant cannot be changed, whereas the value of a variable can be changed while the program is running.

To learn more about Java basics, please refer to the following video ↓ ↓ ↓

Java foundation, Java constants and variables, Java300 set zero foundation for beginners video

constant

A quantity whose value remains constant throughout the operation of a program, is data that cannot be changed. For example, number 1, character “A”, floating point 3.2, and so on. In Java, constants include integer constants, Boolean constants, character types, and string constant values

Integer constants

By default, an int constant occupies 32 bits in memory. It is an integer value and can be represented as a long value when the required value exceeds 32 bits.

Boolean constants

Java Boolean constants have only two values, false and true.

Character and string constant values

A Java character constant value is a character enclosed in single quotes, such as ‘e’ or ‘e’. Note that single and double quotation marks in Java string constant values cannot be mixed. Double quotation marks are used to indicate strings. For example, “11” and “D” are single-character strings.

variable

Each variable and expression has a type that is determined at compile time. A type limits the values that can be assigned to a variable, limits the values that can be produced by an expression, limits the operations that can be performed on those values, and determines the meaning of those operations.

The naming conventions for variable identifiers are as follows:

  • The first character must be a letter, underscore (_), DOLLAR sign ($), or RMB sign (¥).
  • Keywords and reserved words cannot be used as identifiers.
  • The identifier consists of a number (0)9), capital letters (AZ), lowercase letters (a to Z), underscores (-), DOLLAR symbols ($), RMB symbols (¥), and all ASCII characters before hexadecimal 0xc0.
  • There is no limit to the length of an identifier.
  • Identifiers are case sensitive.

Variable assignment

There are two ways to initialize a variable: one is to assign a value directly upon declaration

Char usersex = 'male'; // Assign directlyCopy the code

One is to declare first and assign later

String username; Username =" username "; / / after the assignmentCopy the code

In addition, multiple variables of the same type can be defined or initialized at the same time, but they are separated by commas and separated by semicolons at the end of declarations.

String username,address,phone,tel; Int num1=12,num2=23,result=35; Declare and initialize multiple variablesCopy the code

Note the following when initializing variables in Java:

  • Variables are fields in a class or structure that are created by default with a default initial value of 0 if not explicitly initialized.
  • A variable in a method must be explicitly initialized, or you will get an error using the variable.

Today mainly to say these to everyone, there are other opinions and ideas of friends can leave a message to discuss oh, we study together, progress together!