SoFunction
Updated on 2025-03-02

Java basics are proficient in annotations and data types, constants and variables

1. Comments

1. Introduction

Comments are a kind of text that explains the program, improves the readability of the code, helps us find errors, and will not affect the operation of the program. There is no need to worry about the executable code becoming more executable.

2. Type

1. Single line comments

Single-line comments are the most commonly used format://The content of the comment

The usage is as follows:

package day1;
public class Demo01 {
public static void main(String[] args) {
	("hello word");  //Output hello word	(3-1);           //The output result is 2}
}

2. Multi-line comments

When there are too many comments, you can use multiple lines to comment in the format:/Comment content/

The usage is as follows:

package day1;
public class Demo01 {
public static void main(String[] args) {
	("yuema");
	(3-1);
	/*("yuema");
	(3-1);*/   //This is how to use multi-line comments}
}

3. Document comments

Automatically generate comments for documents in format:/*Comment content/

The usage is as follows:

package day1;
public class Demo01 {
/**public static void main(String[] args) {
	("yuema");
	(3-1);
	("yuema");
	(3-1);
}*/                    //This is how to use document comments}

3. Things to note

In Java, multiple line comments cannot be nested, that is, /* */ cannot be nested. This code itself may also contain a /* delimiter.

2. Data type

1. Introduction

Java is a strongly typed language, meaning that a type must be declared for each variable.

Java has eight basic types: four integers, two floating point types, one char type, and one boolean type

2. Integer

Integers are used to represent values ​​without decimals, and are allowed to be negative.

Java provides four integers: int, short, long, and byte.

Type             Storage Requirements       Value Range
int                            4 bytes    -2147483648 ~ 2147483647
short                  2 bytes      -32768 ~ 32768
long           8 bytes      -9223372036854775808 ~  9223372036854775808
byte                        -128 ~ 127

Int is most commonly used. If a value is relatively large, use long. In addition, short and byte are used in specific application situations, such as underlying file processing or valuable arrays with storage space.

There are no unsigned int, short, long, byte types in Java.

3. Floating point type

Floating point type represents a value with a decimal point. There are two types of floating point in Java.

Type           Storage Requirements
float                                        Approximately +-3.40282347E+38F (the significant digits are 6~7 digits)
double       8 bytes                   Approximate +-1.79769313486231570E+308 (the significant digit is 15 bits)

  • The numerical accuracy of double type is twice that of float type, and most commonly used double type.
  • The float type is used in a specific place, such as a single-precision library or when storing large amounts of data.
  • The float type value is followed by a suffix F or f, for example: 3.14F/f. If there is no F/f suffix afterwards, the system will default to double type.

Warning: Floating point values ​​are not applicable to financial calculations that cannot accept rounding errors

(2.0-1.1);  //The system will print out0.8999999999999999Instead0.9。

type

char is used to represent characters

The literal value of char is expressed in single quotes, for example: ‘A’. Note: It is not represented in double quotes.

char indicates range: \u0000 to \uFFFF

type

The boolean type has two values: false and true, which are used to determine logical conditions.

Integer values ​​cannot be converted to boolean values.

3. Constants and variables

1. Constant

A constant is a quantity that cannot be changed, which is a constant, eternal and unchanging.

Classification of constants: integer constants, decimal constants, character constants, string constants, boolean constants, and empty constants.

package com;
public class Demo05 {
	public static void main(String[] args) {
		//();//Output statement, able to output content on the console		//Output integer constants on the console		(1);
		(12);
		(-12);
		//Output decimal constants on the console		(3.14);
		(12.5);
		(1.0);
		//Output character constants on the console		('a');
		('exist');
		('$');
		//There is only one character in the single quote mark of the character constant, and cannot be an empty character. The following is an example		//('');//Incorrect		//('abc');//Error		//Output string constants on the console		("a");  //String constants must be enclosed in double quotes, and they can be one, a string, or empty data.		("abc");
		("123");
		("");
		//Output boolean constants on the console		(true);  //There are only two values.		(false);
		//Output empty constant on console		//(null);//Empty constants cannot be placed in the output statement	}
}

In Java, you can use the final keyword to indicate constants:

package decom1;

public class changliang {
	public static void main(String[] args) {
		
		final double a=2.50;  // Final Once a variable is defined, the value of the variable cannot be changed.		double b=2.0;
		double c=3.0;
		
		("Output result:"+a*b+"and"+a*c);
	}

}

Output results: 5.0 and 7.5

2. Variables

Variables that change over a period of time are called variables.

Variable name names must be a sequence that begins with letters and consists of letters or numbers, and are case sensitive.

Keywords in Java cannot be used as variables.

After declaring a variable, the declared variable must be initialized.

Variable format:

Direct variables:

Data type Variable name = Data; (directly defined) int i = 0;

Indirect variables:

Data type Variable name; Variable name = Data; (indirect definition) int i; i=1;

package decom1;
public class bianliang {
	public static void main(String[] args) {
		//Define a byte variable		byte a =12;
		(a);
		//Define a short variable		short b;
		b=13;
		(b);
		//Define an int variable		int c=14;
		(c);
		//Define a long variable		long d=2;
		(d);
		//Define a float variable		float e=12.04F;
		(e);
		//Define a double variable		double f=1.0;
		(f);
		//Define a char variable		char g = 'A';
		(g);
		//Define a variable of boolean type		boolean h = true;
		(h);
		boolean i = false;
		(i);
		//Define 2 int type variables a, b		//int a = 12, b = 13;
		/*int a, b;
		a = 12;
		b = 13;*/
		//Define a variable of type int, with an initial value of 12		int k = 12;
		(a);//12
		//Modify the value of variable a to 13		k = 13;
		(a);//13				
	}
}

Things to note

  • When we assign values ​​to float type variables, we need to add F or f to the data.
  • When we assign values ​​to long type variables, it is recommended to add L or l afterwards.
  • When we want to use a certain variable, we must assign the value first and then use it, otherwise an error will be reported.
  • When we want to assign a value to a variable, we must consider the scope of the variable, otherwise an error will be reported.
  • In the same pair of curly braces, variables with the same name cannot be defined.

Article 4 Example:

package decom1;
public class cuowu {
	public static void main(String[] args) {
		byte i = (byte)130;
		(i);
	}

}

The output result is: -126

This is the article about proficient in comments and data types, constants and variables in basic Java knowledge. For more related Java data type content, please search for my previous articles or continue browsing the related articles below. I hope you can support me in the future!