Java learns data storage, data types, variables, and operators

1.1 data type

1.1.1 data storage

Program: a collection of commands, usually an executable file

1 data must be stored before operation

2 storage mode

Memory: lines extending infinitely at both ends

Hard disk: Spiral

3 storage units

Bit = bit = bit

Byte = 8bit  

-128 ~ 127 in order to save positive and negative numbers, only half of the precision can be lost, and the highest bit is used as the symbol bit

1 is a negative number and 0 is a positive number

Negative storage complement

For example, 2   : 0 0000010

For example -2: 1 0000010 (original code) - > 1 1111101 (inverse code) - > 1 1111110 (complement code)

Inverse code: 1 becomes 0, 0 becomes 1

Complement: inverse code + 1

Short = short integer=   2byte = 16bit

Int = integer = 4byte = 32bit   - 2147483648 ~2147483647

Long = long integer = 8byte = 64bit

1.1.2 hex

Binary: 0101010100011 there is no way to directly represent binary in Java

Octal: 0 ~ 7    , If the data in java starts with 0, it indicates octal 012

Decimal system: enter 1 after 10. The data in Java does not start with 0. They are all decimal   two thousand four hundred and fifty

Hexadecimal: full 16 into 1,a represents 10,b represents 11....f represents 15, and the data in Java starts with 0x, indicating hexadecimal    0x12

1.1.3 data type classification

Essence: Specifies the size of memory space occupied, generally expressed in bits and bytes

Reference data type

Class, array, interface

Basic data type

Numerical type

Integer type

byte,short,int,long

float

float,double

character

char

Boolean

boolean

Byte: byte   8 bits

Short: short 16 bit

Int: integer 32-bit

Long: long 64 bit

Float: single floating point, 32-bit

Double: double floating point 64 bit

Char: 16 characters

Boolean: Boolean 8 bits, 00000001 is true and 00000000 is false

ASCII code

0 : 48

1 : 49

A : 65

B : 66

a : 97

b : 98

1.1.4 naming rules

All the places we need to name apply

one   Variable name

two   Class name

three   file name

four   Folder name

five   Method name

Mandatory: only uppercase and lowercase letters, dollar sign $, numbers, underscores, And the number cannot start

Keywords and reserved words cannot be used, (only keywords and reserved words cannot be used, and keyword 1 is OK)

Keyword: it is a representative word that has been used in java

Reserved word: a keyword that is not currently used in java, but may be used in subsequent versions

Non mandatory: look at the text to know the meaning, hump naming method

Int a = 18;

Int x = 18;

Int age = 18;

1.1.5 use of data types

1.1.5.1 integer type

package src;

public class sjsy {
	public static void  main(String[] arg){
		//Use byte to declare an 8-bit space and name the space b_1 put 11 in space
		byte b_1=11;//byte
		byte b_2=011;
		System.out.println(b_2);
		//Print the contents in b-1 space. Note that there are no double quotation marks
		System.out.println(b_1);
		int i_1=111419;//Integer, decimal
		int i_2=11111011;//Binary
		System.out.println(i_1);
		System.out.println(i_2);
		short s_1=7777;//Short 
		System.out.println(s_1);
		long l_1=111419555L;//Long integer. L/l should be added. Uppercase is recommended because lowercase and 1 are not easy to distinguish
		System.out.println(l_1);
	}

}

1.1.5.2 floating point type

package src;

public class day_02 {

	public static void main(String[] args) {
		double d_1=1.2;
				//D/d can be added, generally not
				System.out.println(d_1);
				float f_1=7.7f;
						//float needs to add f/F, because without f/F, it is a double decimal by default
						//Add F to indicate that 7.7 is the value of float type
						System.out.println(f_1);
						//Cast, cast double type to float type
						float f_2=(float)1.2;
						System.out.println(f_2);
	}
}

1.1.5.3 character type

package src;
/**
 *Character type: char, expressed in single quotation marks, and there is only one character in the single quotation marks
 *Java The character types in are encoded in Unicode
 *short Short 16 bit - 32768 ~ 32767
 *char Character type 16 bits 0 ~ 65535, no negative number
 *
 * @author Human disqualification
 *
 */
public class day_03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		char c_1='a';
		int i_1=c_1;
		System.out.println(i_1);
		System.out.println(c_1);
		char c_2='Zhang';
		//Spaces count
		char c_3=' ';
		System.out.println(c_2);
		System.out.println(c_3);
		s
	}
}

1.1.5.4 escape character

package src;
/**
 * \Convert meaningful characters into meaningless characters
 * @author Human disqualification
 *
 */

public class day_04 {
	public static void main(String[] args){
		char c_1 ='\'';
		//String type, double quotation marks
		String str="guanqianlong1114";
		System.out.println(c_1);
		System.out.println(str);
		//tab key
		char c_2='\t';
		//Save \ to write two \, convert the escape character
		char c_3='\\';
				//Newline \ nrepair c_3 assignment wrap
				c_3='\n';
				System.out.println(c_3);
		
	}

}

1.1.5.5 Boolean

package src;
/**
 * java Boolean type in, with only two values of true/false
 * Occupy one byte: all 0 is false,00000001 is true
 * Boolean types cannot participate in any form of conversion
 * It is generally used for process control and judgment
 * @author Human disqualification
 *
 */

public class day_05 {

	public static void main(String[] args) {
		boolean f_1=true;
		f_1=false;
	}
}

1.1.6 data type conversion

package src;
/**
 * Boolean types do not participate in conversion
 * Automatic type conversion: low precision to high precision is automatic type conversion, also known as implicit conversion
 * byte->short->int ->long ->float -> double
 *             char->int ->long ->float -> double
 * Cast type conversion: high precision to low precision is cast type conversion, also known as display conversion
 * Format: low precision type name = (low precision type) high precision value; May cause data errors
 * int i1=12
 * 
 * byte b1=(byte)i1;
 * @author Human disqualification
 *
 */

public class day_06 {

	public static void main(String[] args) {
		byte b1=123;
		int i1=b1;
		short s1=(short)i1;
		int i2=111419;
		byte b2=(byte)i2;
		System.out.println(b2);
		System.out.println(s1);
	}
}

1.1.7 hybrid operation

package src;

public class day_07 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		byte b3 = 10;
		double d3 = 30;
		// In mixed operations, the result is the highest type of operation
		double result = d3 - b3;
		int i3 = 20;
		int i4 = 3;
		// 6. No remainder
		int i5 = i3 / i4;
		// 6.0 because the i3/i4 result is 6 of int type, and then convert the 6 automatic type to double type, which is 6.0 instead of 6.66666
		double i6 = i3 / i4;
		System.out.println(i5);
		
		byte b_1 = 1;
		short s_1 = 2;
		// When any one or more of byte, short, char and int are mixed, the result is of type int
		int s_2 = s_1 + b_1;
	}

}

1.2 constants and variables

1.2.1 constants and literals

package _02_Var;
/**
 * Constant: the value cannot be changed throughout the life cycle
 * 
 * Literal: values cannot be changed and reused throughout the life cycle
 * @author Human disqualification
 *
 */

public class Var_01 {

	public static void main(String[] args) {
		//Literal quantity, cannot be reused
		//But there are types and space, but space is temporary
		//Integer defaults to int, and decimal defaults to double
	
	}

}

1.2.2 variables

1.2.2.1 declaration

1.2.2.2 variable classification

1.2.2.3 variable call

package _02_Var;
/**
 * Variable: data that can be changed to facilitate data operation and reuse of space
 * Variable declaration: data type variable name = value; int i=2;
 * Variable classification: 
 * 
 * Local variables: variables declared in methods are local variables 
 * 
 * Static variables: variables declared using static in the class body 
 * 
 * Member variable: a variable declared without static in the class body
 * 
 * Call of variable:
 * 
 * Local variable: in the method, you can write the variable name directly and call it. You can't call it outside the method
 * 
 * Static variable: class name. Static variable name; In the current class, the class name can be omitted
 * 
 * Member variable: object. Member variable name;
 * 
 * Scope: the scope of use of the variable, the variable declaration, going up, the first brace encountered, and penetrating down
 * 
 * @author Human disqualification
 *
 */

public class Var_02 {
	// Static variable
		static int a = 2;
		// Member variable
		int b = 10;

	public static void main(String[] args) {
		System.out.println(Var_02.a);
		System.out.println(a);
		// Create an int space, name it i, and assign it 10
		int i = 10;
		// Find the corresponding data according to i and print it
		System.out.println(i);
		System.out.println(i);
		// Change the value of the i space to 2
		i = 2;
		System.out.println(i);
		System.out.println(i);
		if (true) {
			int x = 2;
			System.out.println(x);
			System.out.println(i);
		}
		
	}

}

1.2.2.4 variable defaults

package _02_Var;
/**
 * Default value. Local variables have no default value
 * 
 * Static variables and member variables have default values
 * Integer: 0
 * Decimal: 0.0
 * Boolean: false
 * Reference type: null
 * @author Human disqualification
 *
 */

public class Var_3 {
static int b;

	public static void main(String[] args) {
		//The local variable has no default value, so it can't be used without assignment, and an error will be reported when it is used
		int a;
		int i =1;
		System.out.println(i);
		System.out.println(b);


	}

}

1.3 operators

1.3.1 arithmetic operators

package _03_Operator;
/**
 *  * Arithmetic operator
 * 
 * + - * / %
 * 
 * ++ --
 * 
 * ++ : Self + 1, take the value out of + 1 and put it back (assignment will occur)
 * 
 * Monocular (monocular) priority is higher than binocular (binary)
 * 
 * A unary operation is one that has an operand, such as i++
 * 
 * Binary is two operands: a*b
 * 
 * @author Human disqualification
 *
 */

public class Operator_01 {

	public static void main(String[] args) {
		int a = 10;
		int b = 3;
		// Because it's an integer, don't let the remainder be 3
		System.out.println(a / b);//division
		// 1
		System.out.println(a % b);//Surplus

		int s = 100;
		// The difference between i + + and + + i: i + + is assigned first and then + 1, and + + i is assigned first and then + 1
		// If it appears alone, there is no difference
		// s++;
		// ++s;
		// The code performs initialization from left to right, assigning values first and then++
		// s = s++ + 10;
		// s = 100 +10; s=101
		// s = 110
		s = s++ + s;
		// s = 100 + 101; s = 101
		System.out.println(s);

		int k = 100;
		// First + + and then assignment
		k = ++k + k;
		// k = 101 + 101; k = 101
		System.out.println(k);

		int m = 10;
		int e = 2 + m++;
		System.out.println(m);
		System.out.println(e);

		int p = 2;
		// Initialize the calculation from left to right
		p = 2 + p++ + ++p + p++ + ++p;
		// p = 2 + 2 + 4 + 4 + 6; p = 6
		System.out.println(p);

		int x = 10;
		x = 10 + x++ + ++x + (10 * x++) + ++x;
		System.out.println(x);
	}
}

1.3.2 relational operators

package _03_Operator;
/**
 * Relational operator: the result is Boolean, only true and false
 * 
 *  >  , <  , >= ,  <= , 
 *  
 *  == : Judge whether they are equal
 *  
 *  != : Judge inequality
 * @author Human disqualification
 *
 */

public class Operator_03 {

	public static void main(String[] args) {
		int a =10;
		int b=11;
		System.out.println(a>b);//flase
		System.out.println(a>=b);//flase
		System.out.println(a<b);//true

	}

}

1.3.3 logical operators

package _03_Operator;
/**
 * &:Bit and, both sides are true, the result is true
 * |:Bit or, if one side is true, the result is true
 * !:Bit is not, take the opposite, true is false, false is true! true=false
 * ^:Bitwise exclusive or, the result is true only if the two sides are different, true^false=true,true^true=false
 *  ~ : Negate by bit and not by the hexadecimal bit of the value, 
 * 	 		~2 : 2 Binary 0 000 0010 of, each negative, 1 becomes 0, 0 becomes 1
 * 					1 111 1101  -> Inverse code - > 1 111 1100 - > source code - > 1 000 0011
 * 
 * @author Human disqualification
 *
 */


public class Operator_02 {

	public static void main(String[] args) {
		System.out.println(~2);
		// If & both sides are numbers, it becomes an and operation
		// Convert to binary. For each comparison, take 1 from 1, otherwise take 0
		// The final result will not exceed the smallest of the two numbers
		// 0 000 1000
		// 0 000 1001
		// 0 000 1000
		System.out.println(8 & 9);
		// false
		System.out.println(1>2 & 1<2);
		// true
		System.out.println(1>2 | 1<2);
		// true 
		System.out.println(1>2 ^ 1<2);
		// true
		System.out.println(!(1>2));
	}
}

1.3.4 short circuit and, or

public class Operator_04 {
/**
 *  && : In addition, if both sides of the short circuit and are true, the result is true. If the first judgment is false, the second judgment will not be executed, and false will be returned directly
 * 
 *  || : Or, short circuit or, if one of the two sides is true, the result is true. If the first one is true, the second judgment will not be executed and return true directly
 * 
 * @author Human disqualification
 *
 */



	public static void main(String[] args) {
		int a = 10;
		int b = 10;
		int e = 10;
		// The first condition is false, and the second condition is not executed
		boolean c = (a > b && a > b++);
		boolean c1 = (a > e& a > e++);
		
		System.out.println(b);
		System.out.println(c1);
		// If there are both & & and 𞓜 in an operation, then & & priority is greater than||
		boolean flag = true || false && false;
		System.out.println(flag);
	}
}

1.3.5 bitwise operators

package _03_Operator;
/**
 *  Displacement operation
 * 
 * 	<< : Shift left operation (the sign bit remains unchanged) is converted to binary, shifted to the left, and the rightmost is supplemented with 0
 * 		x << y  = x*2^y
 * 		One shift to the left equals two
 * 
 * >> : Shift right operation (sign bit unchanged) is converted to binary, shift right, and the leftmost complement 0. If it is a negative number, the leftmost complement 1
 * 		x >> y  = x/2^y
 * 		Moving right once is equal to dividing by 2
 * 
 * Interview question: how to quickly calculate the third power of 2
 * 		2<<2
 * @author Human disqualification
 *
 */

public class Operator_05 {

	public static void main(String[] args) {
		System.out.println(-2 << 2);
	}
}

1.3.6 assignment operator

package _03_Operator;
/**
 * Assignment operation
 * 
 * =
 * 
 * += , *= , /= , -= , %=
 * 
 * -= : The result of subtracting the right from the left is assigned to the left
 * 
 * i+=2;   Equivalent to i = i + 2;
 * @author Human disqualification
 *
 */

public class Operator_06 {

	public static void main(String[] args) {
		int i = 10;
		// Equal to i = i + 10;
		i+=10;
		System.out.println(i);
		i -=5;
		System.out.println(i);
		
		byte b = 2;
		// Equivalent to b = b + 1, and b+=1;
		b++;
		// b = b + 1;
		b+=3333;
		// Although + =, + + and B = b+xxx are equivalent, the operation of b+xxx requires forced type conversion
		// And + + and + = are automatically cast
		b=127;
		b++;
		System.out.println(b);
	}
}

Tags: Java

Posted on Sun, 26 Sep 2021 15:53:51 -0400 by naskoo