[learning notes] self study java notes

Posted by kotun on Fri, 25 Feb 2022 03:37:15 +0100

Java notes

Java data type - basic type

Character type

Char

Character type, which takes up 2 bytes. Only a single character is allowed. Character quotation marks are forced to use single quotation marks

char a  = 'a';

Forced digital conversion of Char

  • Use Char to cast numbers to output Unicode values of corresponding characters

    🌰 :

    char c1 = 'a';
    char c2 = 'in';
    char c3 = '\u0061'; // Unicode code
    
    System.out.println(c1); // a
    System.out.println((int)c1); // 97
    System.out.println(c2); // in
    System.out.println((int)c2); // 20013
    System.out.println(c3); // a
    

String

String type. The space depends on how many characters are in the string. String quotes force the use of double quotes

String is a "class" rather than a "keyword". The implementation method of string is the array splicing of Java encapsulated char. Try not to use character splicing to define the string. If you use more places, it will eat resources and may cause memory overflow or memory leakage.

// Output "aaabbbccc", but eat resources very much. It is not recommended to write that
String chars = 'aaa' + 'bbb' + 'ccc';

Number type

Byte

Bit type, which takes up 1 byte of space, is generally not used because the loading range is too small (- 128 ~ 127)

Short

Short integer, which takes up 2 bytes of space, is generally not commonly used because the loading range is small (- 32768 ~ 32767)

Int

Integer, accounting for 4 bytes. Common data types range from negative 2.1 billion to positive 2.1 billion (basically enough)

Long

Long integer, accounting for 8 bytes of space, ranging from 19 digits

// A long integer is usually followed by an 'L' character to specify a long integer number
Long number = 3000L;

JDK7 is a new feature. You can add "" in the middle of numbers To make a visual distinction, and the underline will not be printed

🌰 :

int i = 10_0000_0000;

System.out.println(i); // 1000000000

Floating point type

Float

Floating point type, occupying 4 bytes, ranging from 10-38 to 1038 and - 1038 to - 10-38

// Floating point numbers are usually followed by an 'F' character to indicate that they are single floating point numbers
Float number = 1.3476F;

Double

Double floating point type, occupying 8 bytes of space, ranging from 10-308 to 10308 and - 10-308 to - 10-308

Attention - the precision problem of floating point type

⚠️ It is best to completely avoid using floating-point numbers for comparison ⚠️

⚠️ It is best to completely avoid using floating-point numbers for comparison ⚠️

⚠️ It is best to completely avoid using floating-point numbers for comparison ⚠️

  • Float type is a with rounding error. The type stored by the float type is approximate, close to but not equal to the value of the stored data

    🌰 :

    Float f = 0.1f;
    Double d = 1.0 / 10;
    System.out.println(f == d); // false
    
  • The data stored by Float type is limited and discrete

    🌰 :

    Float f1 = 323243243242432432432f;
    Float f2 = f1 + 1;
    System.out.println(f1 == f2); // true
    

Boolean value

Boolean

Boolean value, occupying 1 byte space, only true and false

Java data type - reference type

class

Interface

array

Integer base

Binary

// Start with 0b

octal number system

// Start with 0
int i = 010;
// Output 8
System.out.println(i);

hexadecimal

// Start with 0x
int i = 0x10;
// Output 16
System.out.println(i);

Type conversion

Java is a strongly typed language and sometimes requires type conversion

Type conversion has the order from low capacity to high capacity

Low ---------------------------------------------------- > High

Byte, short, char (2 bytes) - > int - > long - > float - > double

The type of floating-point number is higher than that of integer

  • Cast high -- > low (type) variable names
  • Auto convert low -- > High
int i = 128;
byte b = i; // The compiler reports an error indicating the wrong type
byte b = (byte)i; // The compiler will no longer report errors and i force the type to be converted to byte value

System.out.println(i); // 128
System.out.println(b); // -128 (memory overflow)

be careful

  • Boolean values cannot be converted (true and false have no meaning of conversion)

  • Cannot convert irrelevant object types

  • Cast occurs when a high-capacity type is converted to a low-capacity type

  • Memory overflow and precision problems may be encountered during conversion

  • Avoid overflow problems when operating on large numbers

    🌰 :

    int money = 10_0000_0000;
    int year = 20;
          
    ⚠️
    int total1 = money * year;
    System.out.println(total1); // -1474836480 because the range of int is overflowed
          
    ⚠️
    long total2 = money * year;
    System.out.println(total2); // -1474836480 because the calculation order is first operation and then assignment, the two int numbers still overflow after operation
          
    ✅
    long total3 = money * ( (long)year );
    System.out.println(total3); // 2000000000 after one of the numbers is designated as long type, the whole operation can be designated as high-capacity long operation
            
    

variable

local variable

// Method of defining local variables

// main method
public static void main(String[] args) {
  // Local variables must be declared and initialized
  int i = 1;
  char c = 'c';
}

// Other methods
public void add() {
  // local variable
  int i = 1;
  char c = 'c';
}


Instance variable

// Instance variable -- subordinate to object

public class Demo {
  
  // Instance variable does not initialize value
	String name;
	int age;
  boolean type;

	public static void main(String[] args) {
    // Reference instance variable
    // Variable type variable name = new
  	Demo demo = new Demo();
    System.out.println(demo.age); // 0 numeric type integer defaults to 0, floating point type defaults to 0.0
    System.out.println(demo.name); // Null all default values are null except the basic type
    System.out.println(demo.type); // False Boolean default false
	}
  
}

Class variable

public class Demo {
  
  // The preceding static is a class variable
  static double salary = 2500;
  public static void main(String[] args) {
    
    // Class variables can be obtained without creating an instance. Class variables are created with the life cycle of the class. When the class is destroyed, the class variables will also be destroyed
    System.out.println(salary);
    
  }
  
}

constant

// A constant is a special variable that cannot change its value after initialization
// Constant names are generally capitalized

// Both writing methods are OK, and the modifiers are not distinguished in order
static final int I = 20;
final static int P = 20;

Variable naming convention

⚠️ The principle of seeing the name and knowing the meaning ⚠️

Class member variable, local variable and method name: initial lowercase, named after small hump

🌰: maxValue,maxValue()

Constants: uppercase, underline, split

🌰: MAX_VALUE

Class name: capitalized, named after the big hump

🌰: MaxValue

operator

exponentiation

double pow = Math.pow(2,3);
System.out.println(pow); // 8. Meaning of 2 ^ 3

Bitwise Operators

A = 0011 1100;
B = 0000 1101;

A&B = 0000 1100;//Two numbers are all 1
A|B = 0011 1101;//Take two numbers with 1
A^B = 0011 0001;//Take two different numbers
~B = 1111 0010;//Reverse

<< // *2 shift left
>> // /2 shift right

IDEA shortcut

psvm = public static void main (String[] args) {}
sout = System.out.println("");

Topics: Java