Variables, constants, and their data types

Keywords: encoding

The nature of variables

It represents an operable storage space. The location of the space is determined, but what value is placed in it is uncertain. We can access the corresponding storage space through the variable name, so that the value of operating the storage space is defined according to the size of the variable to be defined. The size of the storage space allocated by different types is different.
Variable contains variable name, data type, scope
Variables can only be used after they have been declared and initialized.

Type of variable

1. Local variable:
Variables defined in a method or statement block can only be used in a method or statement block
2. Member variable (instance variable):
Method external, variable defined inside the class, subordinate to the object, with the object's life cycle always
Is a member of an object, which is automatically initialized

  • int: 0
  • double: 0.0
  • char: \u0000
  • boolean:false

3. Static variables:
Defined by static, subordinate to a class, the life cycle is always accompanied by that of the class, from the loading to unloading of the class (the longest life cycle)

Integer variables

Name all in uppercase and underline

  • Integer variable: byte(1 byte), short (2), int (4), long (8 bits per byte) integer contains positive and negative numbers.
  • The representation of base: 0 (octal) 0x (HEX), 0b (binary)
  • The integer constant is of type int by default, so if l or L is added after the long number when defining a variable of type long, it means a constant of type long
public class virant {
	int a;//Member variables
	static int size;//Static variables
	final String STR="name";
	//STR="abcd"; / / constant cannot be reassigned
	//Constants are initialized at the time of definition.
	public static void main(String[] args) {
			int age;//Local variable, subordinate to statement block
		byte a=30;
		//short b=30000000; / / exceeds the length of short
		short b=30000;
		int c=3000000;
		//Long d = 3000000000; / / the default size of a constant is int, which exceeds the size of an integer constant
		long d=30000000000L;//Add L or l after it to transform it into a growth integer.


Floating point variables and constants

  1. float: 4 bytes. The mantissa is accurate to 7 significant digits. float type values have a suffix of f or F. floating point numbers without F/f are of double type by default. You can also add D/d to show that they are of double type
  2. double: 8 bytes
public class virant {
	public static void main(String[] args) {
		float a=3.14f;//If you do not add f, you will get an error, because floating-point constants are of double type by default
		double b=6.28;
		double c=628E-2;
		//Floating point numbers are imprecise and must not be used for comparison
		float f=0.1f;
		double d=1.0/10;
		System.out.println(f==d);//The result is false
		float d1=423432423f;
		float d2=d1+1;
			System.out.println("d1==d2");//The output result is d1==d2;


If you need to compare floating-point data, use the BigDecimal class to compare

BigDecimal bd=BigDecimal.valueOf(1.0);
		BigDecimal bd2=BigDecimal.valueOf(0.1);
		BigDecimal bd3=BigDecimal.valueOf(1.0/10);

Character variables and constants

char: represents characters in Unicode encoding, and can represent characters in all languages. (including Chinese characters)
Unicode has an encoding between 0-65535, usually represented by a hexadecimal value between \ u0000 - \ ufff (the prefix u represents Unicode)

char c="\u0061";//a

Escape character

System.out.println(""+'a'+'b');//Precede with a string to indicate that two characters are linked
		//If the empty string is not added, the character type will be automatically converted to int type numbers for addition

Variables and constants of type boolean

The values are true and false. (default is false)

Published 14 original articles, won praise 3, visited 850
Private letter follow

Posted by canabatz on Sat, 01 Feb 2020 22:55:43 -0800