Java data type and conversion

1. Naming rules and specifications of Java identifiers

Naming rules for identifiers:

  1. It consists of 26 English letters in case, 0-9_ Or $composition

  2. Number cannot start

  3. Keywords and reserved words cannot be used, but they can be included

  4. java is strictly case sensitive and has unlimited length

  5. The identifier cannot contain spaces

Naming conventions for identifiers:

  1. Package name: when composed of multiple words, all letters are lowercase: AAA bbb. ccc

  2. Class name and interface name: when composed of multiple words, the first letter of all words is capitalized: aabbbcc

  3. Variable name and method name: when composed of multiple words, the first word is lowercase, and the second word is capitalized at the beginning of each word: aaaBbbCcc

  4. Constant name: all letters are capitalized. When there are multiple words, each word is connected with an underscore: XXX_YYY_ZZZ

2. Escape characters commonly used in Java

\t : A tab stop to realize the alignment function

\n : Line feed

\\ : One\

\" : One"

\' : One'

\r : A carriage return

3. Notes

Comments are used to annotate and will not be executed by the compiler, which improves the readability of the code. Programmers need to have a good habit of writing comments

java provides three annotation types: single line annotation multi line annotation document annotation

Document annotation: the annotation content can be parsed by the tool javadoc provided by JDK to generate a set of description documents of the program in the form of web page files

Document comment generation command (pay attention to the encoding format, otherwise the encoding mapping error will be reported): javadoc -d folder - parameters - parameter file name

/**
* @author Xiao Ming
* @version 1.o
*/
public class HelloWorld{
	//Single-Line Comments 
	public static void main(String[] args){
        /*
		multiline comment 
		......
		......
		*/
        System.out.print("Hello World");

	}
}

4. Variables

**Concept: * * is equivalent to the representation of a data storage space in memory. The value stored in the variable can be accessed through the variable name, and the value can be changed

matters needing attention:

  1. Variables must be declared before use
  2. A variable represents a storage area in memory (different variables, different types, and different storage spaces)
  3. Variables cannot have the same name within the same scope
  4. Three elements of variables: variable name, value and data type
int a; //reputation
a = 100; //assignment
System.out.println(a); //Output the value of variable a

5. Use of + in the program

  1. When the left and right sides are numeric, add
  2. When one of the left and right sides is a string, the splicing operation is performed
System.out.println(10 + 20); //30
System.out.println("10" + 20); //1020
//Note the following two output results. Addition is performed from left to right
System.out.println("hello" + 10 + 20); //hello1020
System.out.println(10 + 20 + "hello"); //30hello

6.java data type

1. Basic data type

  • Integer type:
typebyteBinary rangeValue range (decimal)
byte1 byte-2^7 ~ 2^7-1-128 ~ 127
short2 bytes-2^15 ~ 2^15-1-32768 ~ 32767
int (common)4 bytes-2^31 ~ 2^31-1-2147483648 ~ 2147483647
long8 bytes-2^63 ~ 2^63-1-9223372036854775808 ~ 9223372036854775807
byte num1 = 10;
short num2 = 1000;
int num3 = 10000000;
//The integer constant of java is of type int by default, and the constant of type long must be followed by "L" or "L"
long num4 = 100000000000000L;
  • Floating point (decimal) type:
typebyteValue range
float (single precision)4 bytes-3.403E38 ~ 3.403E38
Double (double precision)8 bytes-1.798E308 ~ 1.798E308
//The floating-point constant of java is double by default. The floating-point constant must be followed by "F" or "F"
float num5 = 1.1111F;
double num6 = 1.11111111111111;

(1) Floating point constants have two representations

Decimal form: 12.0f

Scientific counting form: 1.34e2[1.34 times the power of 10] 1.34e-2[1.34 divided by the power of 10]

(2) Floating point usage trap

As follows: there is a difference between the decimal obtained by calculation and the direct assignment, which is a loophole in the computer itself. Special attention should be paid to when making comparison operation

double d1 = 2.7;
double d2 = 8.1 / 3;
System.out.println(d1); //2.7
System.out.println(d2); //2.6999999999999997
  • character:
typeOccupied storage spaceRange
char2 bytesSingle character, e.g. 'a', 'I'
//A character constant is a single character enclosed in single quotes' '. For example:
char c1 = 'a';
char c2 = 'you';
char c3 = '\t'; //The "\" here represents the escape of, which is actually a character
char c4 = 98;

//In java, the essence of char is an integer. When outputting, char is the character corresponding to unicode code
//You can directly assign an integer to char. When outputting, it will be output according to the corresponding unicode characters
System.out.println((int) c1); //97
System.out.println(c4); //b

//char type can be operated, which is equivalent to an integer, because it corresponds to unicode code
System.out.println(c1 + 10); //107
System.out.println('you' + 100); //20420
  • Boolean:
typeOccupied storage spaceRange
boolean1bit (bit)Store only true or false
boolean isPass = true; //The value of boolean in java can only be true or false, not 0 or other values b, and an error will be reported
if (isPass == true) {
	System.out.println("Pass the exam");
} else {
	System.out.println("Fail the exam");
}

2. Reference data type

  • class
  • interface
  • Array ([])

3. Introduce the following character coding table:

  • ASCII (one byte of ASCII encoding table, 128 characters in total)
  • Unicode (the fixed size encoding of Unicode encoding table uses two bytes to represent characters. The unification of letters and Chinese characters takes two bytes, which wastes space)
  • utf-8 (1 byte for variable size coded letters and 3 bytes for Chinese characters)
  • gbk (it can represent Chinese characters and has a wide range. Letters use one byte and Chinese characters use two bytes)
  • gb2312 (it can represent Chinese characters, and the range is less than gbk)
  • big5 (traditional Chinese, preferred by Taiwan and Hong Kong)

7. Basic data type conversion

1. Automatic type conversion

When a java program performs assignment or operation, the type with low precision is automatically converted to the data type with high precision, which is called automatic type conversion

int a = 'A';
double d = 10000;

The data types are arranged according to the precision:

char --> int --> long -->float --> double

byte --> short --> int --> long -->float --> double

Automatic type conversion details:

When there are multiple types of data mixed operation, the system will first automatically convert all data into the data type with the largest capacity, and then calculate

When we assign a high-precision data type to a low-precision data type, an error will be reported, and on the contrary, automatic type conversion will be carried out

(byte,short) and char are not automatically converted to each other

byte b = 100;
//Error: char c = b;
char c = (char)b;

byte, short and char can be calculated, and they will be converted into int type first

boolean does not participate in the conversion

Automatic promotion principle: the type of expression result is automatically promoted to the largest type in the operand

2. Cast type

The reverse process of automatic type conversion is to convert a data type with large capacity into a data type with small capacity. The cast character () should be added when using, but it may cause precision reduction or overflow, so special attention should be paid when using

int i = (int) 1.9;
System.out.println(i);

int j = 100;
byte b = (byte) j;
System.out.println(b);

Cast details:

  • When the data is converted from large to small, the pre conversion is needed
  • The strong conversion symbol is only valid for the most recent operand, and parentheses are often used to raise the priority
//Error: int x = (int) 10 * 3.5 + 8 * 1.3;
int x = (int) (10 * 3.5 + 8 * 1.3);
  • char type can save the constant value of int, but cannot save the variable value of int, so it needs to be forced
  • byte and short types are treated as int types when they are operated again
char c1 = 1000;
int i = 100;
//Error: char c2 = i;
char c2 = (char) i;

Basic data type and String type conversion

Introduction: in program development, we often need to convert basic data type to String type, or convert String type to basic data type

3. Basic data type to String type

Syntax: add the value of basic type + ""

int num1 = 2000;
double num2 = 3.1415926;
boolean b = true;
// Basic data type -- > string
String str1 = num1 + "";
String str2 = num2 + "";
String str3 = b + "";
System.out.println(str1 + "\n" + str2 + "\n" + str3);

4.String type to basic data type

Syntax: call parseXX method through wrapper class of basic data type

String str = "123";
byte num1 = Byte.parseByte(str);
short num2 = Short.parseShort(str);
int num3 = Integer.parseInt(str);
long num4 = Long.parseLong(str);
float num5 = Float.parseFloat(str);
double num6 = Double.parseDouble(str);
boolean bool = Boolean.parseBoolean("true");

How to convert char type to String type?

Cannot convert. The char type is a character. The string cannot be converted into a character anyway, but you can take one from it:

String str = "123";
char c = str.charAt(0); //In a computer, numbers start with 0, that is, 0 - > 1 - > 2
System.out.println(c); //1

Note: when converting a String type to a basic data type, ensure that the String type can be converted to valid data. For example, we can convert "123" to an integer, but we can't convert "hello" to an integer. If the format is incorrect, an exception will be thrown and the program will terminate

Keywords: Java

Added by PHP_Idiot on Tue, 08 Feb 2022 05:05:49 +0200