[Advanced Java Programming Road---Basic Grammar]

1. Identifier

Java's own defined keywords

There are a few things to note about Java identifiers:

  • All identifiers should start with a letter (A-Z or a-z), dollar sign ($), or underscore (_)
  • The first character can be followed by any combination of letters (A-Z or a-z), dollar signs ($), underscores (_) or numbers
    *Cannot use keywords as variable names or method names.
  • Identifiers are case sensitive
  • Examples of legal identifiers: age, $salary, _value, __1_value
  • Examples of illegal identifiers: 123abc, -salary, #abc

Second, the data type

1. Strongly and Weakly Typed Languages

**A strongly typed language is also known as a strongly typed language. **The use of variables is required to strictly comply with the regulations, and all variables must be defined before they can be used.

Java, .NET, C++, etc. are all mandatory type definitions. That is, once a variable is assigned a data type, if it is not converted, it will always be that data type.

Weakly typed languages ​​are also known as weakly typed definition languages. Contrary to strongly typed definitions. Like vb, php, etc. are weakly typed languages.

2. Data Type

Java data types are divided into two categories: primitive type s and reference type s

public static void main(String[] args) {
 // byte 
 System.out.println("basic type: byte Binary digits:" + Byte.SIZE); 
 System.out.println("Wrapper class: java.lang.Byte"); 
 System.out.println("Minimum: Byte.MIN_VALUE=" + Byte.MIN_VALUE); 
 System.out.println("Max: Byte.MAX_VALUE=" + Byte.MAX_VALUE);

// short 
System.out.println("basic type: short Binary digits:" + Short.SIZE); 
System.out.println("Wrapper class: java.lang.Short"); 
System.out.println("Minimum: Short.MIN_VALUE=" + Short.MIN_VALUE);
 System.out.println("Max: Short.MAX_VALUE=" + Short.MAX_VALUE);
  // int 
  System.out.println("basic type: int Binary digits:" + Integer.SIZE); 
  System.out.println("Wrapper class: java.lang.Integer");
  System.out.println("Minimum: Integer.MIN_VALUE=" + Integer.MIN_VALUE); 		  
  System.out.println("Max: Integer.MAX_VALUE=" + Integer.MAX_VALUE); 
  // long 
  System.out.println("basic type: long Binary digits:" + Long.SIZE);
  System.out.println("Wrapper class: java.lang.Long"); System.out.println("Minimum: Long.MIN_VALUE=" + Long.MIN_VALUE);
  System.out.println("Max: Long.MAX_VALUE=" + Long.MAX_VALUE); System.out.println(); 
    // float
     System.out.println("basic type: float Binary digits:" + Float.SIZE); 
     System.out.println("Wrapper class: java.lang.Float");
     System.out.println("Minimum: Float.MIN_VALUE=" + Float.MIN_VALUE); 
     System.out.println("Max: Float.MAX_VALUE=" + Float.MAX_VALUE); 
       // double 
       System.out.println("basic type: double Binary digits:" + Double.SIZE); 
       System.out.println("Wrapper class: java.lang.Double"); 
       System.out.println("Minimum: Double.MIN_VALUE=" + Double.MIN_VALUE); 
       System.out.println("Max: Double.MAX_VALUE=" + Double.MAX_VALUE); 
       	// char 
       	System.out.println("basic type: char Binary digits:" + Character.SIZE); 
       	System.out.println("Wrapper class: java.lang.Character"); 
       // Output Character.MIN_VALUE to the console as a numeric value instead of a character
        System.out.println("Minimum: Character.MIN_VALUE=" + (int) Character.MIN_VALUE); 
        // Output Character.MAX_VALUE to the console as a numeric value instead of a character 
        System.out.println("Max: Character.MAX_VALUE=" + (int) Character.MAX_VALUE); 
 1bit means 1 bit,
 1Byte represents a byte 

What is the difference between 32-bit and 64-bit computers?

32 bit operating systems can only use 32-bit cpu,while the 64-bit CPU Both 32-bit and 64-bit operating systems can be installed.
Addressing capability is simply the supported memory size capability.
64 Bit system can support up to 128 GB memory, while a 32-bit system can only support up to 4 G Memory. 
32 Bit operating systems can only install software designed with 32-bit architecture,
while the 64-bit CPU Both 32-bit software and 64-bit software can be installed and used.

3. Integer, float, character extension

Questions about decimal, octal, hexadecimal, etc.

 Decimal integer, such as: 99, -500, 0. 
 Octal integer, starting with 0, such as: 015.
 Hexadecimal number, 0 required x or 0 X Begin with: 0 x15 . 
int i=10; int i2=010; int i3=0x10;  
System.out.println(i); //10 
System.out.println(i2); //8 
System.out.println(i3); //16 1234567

What type is used for banking and financial business?

The data of floating-point float and double are not suitable in the field of financial calculation that does not allow rounding errors
The BigDecimal class is required to perform exact number calculations without rounding

Two useful classes under Java.math: BigInteger and BigDecimal, these two classes can handle numbers of arbitrary length
value. BigInteger implements arbitrary precision integer arithmetic. BigDecimal implements arbitrary-precision floating-point arithmetic.

Character type usage summary:

  • There are rounding errors in floating-point numbers, and many numbers cannot be represented exactly.
  • Avoid using floating-point numbers in comparisons (due to the limited word length, the numbers that floating-point numbers can accurately represent is limited, so they are also discrete. Floating-point numbers generally have rounding errors, many numbers cannot be accurately represented, and the result can only be close, but not equal to)

Single quotes are used to denote character constants. For example 'A' is a character which is different from 'A' which means a string.
The char type is used to represent characters in the Unicode encoding table.

2 bytes = 16 bits
2 to the 16th power = 65536

public static void main(String[] args) { 
char c1 = 'a'; char c2 = 'middle'; 
	System.out.println((int) c1); //97 
	System.out.println((int) c2); //20013

Unicode has encodings from 0 to 65535, they are usually represented by hexadecimal values ​​from 'u0000' to 'uFFFF'
(prefixed with u for Unicode)

char c3 = '\u0061'; 
System.out.println(c3); //a

3. Type conversion

Conversion from low level to high level (according to capacity)

Low ------------------------------------> high 
byte,short,char—> int —> long—> float —> double

Conversion rules:

  • Cannot typecast to Boolean type
  • Cannot convert object type to object of unrelated class
  • Casting must be used when converting a type with a large capacity to a type with a small capacity
  • An overflow or loss of precision may result during conversion.

Because the byte type is 8 bits, the maximum value is 127, so when int is cast to byte type, the value of 128 will cause overflow

Fourth, variables, constants

What is a variable: a quantity that can be changed!
The data in the memory space is manipulated through variables, and the variables refer to this storage space! The storage space is deterministic, but the value in it is indeterminate.


People will forget, but time will not!!

Tags: Java

Posted by vij on Tue, 27 Sep 2022 01:52:25 +0930