Conversion between java basic data types and data types

Posted by gibigbig on Sun, 19 Dec 2021 23:19:12 +0100

Java basic syntax

notes

//Single-Line Comments 
public class student{
    /*
    Multiline comments, also known as code block comments, use/**/
    */
        /**
        Documentation Comments 
        */
    public static void mian(String[] args){
        
    }


}

Identifiers and keywords

identifier

All components in java need names. Class names, variable names and method names are called identifiers.

  1. So how are identifiers named? Or what is the naming convention for identifiers?

    • All identifiers should be in letters (A-Z or A-Z), $(dollar sign)_ (underscore) as the first character

    • The first character can be followed by letters (A-Z or A-Z), $(dollar sign)_ (underscore) or any combination of characters of numbers (0 ~ 9)

    • Keywords cannot be used as variable and method names

    • Identifier size sensitive (that is, for example, Studnet and student are two identifiers)

    • Of course, it can also be named in Chinese, but it is generally not recommended to use it in this way, nor is it recommended to use pinyin to name it, which is a little low

      //For example, some legal identifiers, such as stars, $abc12_ one
      //Illegal identifiers, such as: 5wang, & Wang, a #yuan
      /*
      For example, use Chinese as an identifier
      */
      public class Dog{
          public static void main(String[] args){
              String  name="Wangcai";
              System.out.printf(name);
          }
      }
      

keyword

Some common keywords are generally used in the learning process

abstrcatassertbreakbyte
casecatchclassconst
continuedefaultdoubleelse
enumextendsfinallyfloat
forgotoimplementsimport
instanceofintlongnative
newpackageprotectedpublic
returnstrictfpstaticsuper
switchsysnchronizedthrowthrows
transienttryvolatilewhile

data type

Strong data type language

Java is a strong data type language. All variables must be defined before they can be used;

Basic data type

java has eight basic data types

  1. Integer type (four)

    • byte 1 byte
    • short 2 bytes
    • int 4 bytes
    • long 8 bytes
  2. Floating point type (two)

    • float 4 bytes
    • double 8 bytes
  3. char 2 bytes

  4. boolean type occupies only one bit and its values are true and false.

    public class Dome01 {
        public static void main(String[] args) {
            //Eight basic data types
            //byte 1 byte
            byte e=12;
            //short type
            short e2=251;
            //int type
            int e3=90;
            //long type is generally distinguished by an L after the number
            long e4=90L;
            //Float type: float e5=5.3. Generally, the default type is double. All types are distinguished by F
            float e5=5.3F;
            //double type
            double e6=3.1415926;
            //char character type 2 bytes
            char e7='in';
            //Boolean types true and false occupy only one bit
            boolean e8=true;
            //String type is also a string type, which is not a basic type
            String str="wang";
        }
    }
    

    reference type

    • Class (String)
    • Interface
    • array

What are bytes

Bit: it is the smallest unit of data storage in the computer. 11001100 is an eight bit binary number

byte: it is the basic unit of data processing in the computer. It is customarily expressed in capital B.

1 B= 8 bit; That is, one byte is equal to eight bits

Characters: letters, numbers, words and symbols used by computers.

  • 1 bit represents one bit
  • 1 Byte represents a byte
  • 1024B=1KB
  • 1024KB=1M
  • 1024M=1G

Type conversion

Because java is a strongly typed language, type conversion is required when performing some operations

low----------------------------------->high
byte,short,char->int->long->float->double

In the operation, different types of data must be transformed into the same type first, and then the operation is carried out.

Force type conversion (convert large capacity types to small capacity types)

  1. Attention should be paid to memory overflow in high-precision conversion and low-precision conversion
  //Type conversion, cast type conversion format: (type)value type is the data type to be cast.
        //Since the value of byte is - 128 ~ 127,
        // If you cast the value of int to byte, you should pay attention to the memory overflow
        int i=128;
        byte b=(byte)i;
        System.out.println(b);//-128
        System.out.println(i);//128
  1. Convert floating point type to int There will be a lack of precision when typing,a=23,f1==a Returned is false
    
     float f1=23.5F;
            int a=(int)f1;
            System.out.println(f1==a);
    
    1. As mentioned above, there will be precision loss when converting floating-point numbers to integer types,
      that double Type conversion to float What about the type?
      As you can see, it is best to use the same floating-point number for comparison
       This is because when two different floating point types are compared double Automatically converted to float
       Conversion contains errors and is an approximate result
      
      		float f2=0.1F;
              double d1=0.1;
              double d2=1.0/10;
              System.out.println(f2==d1);//Return false
              System.out.println(d1==d2);//Return false
              //This also shows that floating-point numbers are approximate when calculating, so the bank uses BigDecimal
              float f3=2333333333333F;
              float f4=f3+1;
              System.out.println(f3==f4);//The returned result is true
      
//Character type is converted to integer type, why a is cast to integer 97?
//This is because characters are encoded according to unicode, and each character can be converted into an integer, with 6536 characters
		char c='a';
        int i2=(int)c;
        System.out.println(i2);//97
        System.out.println(c==i2);//true

Automatic conversion (from low precision to high precision)

		//Automatic conversion
		int i4=38;
        long l=i4;
        System.out.println(l==i4);//true

//When operating large numbers, pay attention to the overflow problem
        int money=1000000000;
        int year=20;
        int total=money*year;//Overflow during calculation
        System.out.println(total);//-1474836480
        long total2=money*year;//Automatic conversion. By default, it is calculated as int type. There are problems before conversion
        System.out.println(total2);//-1474836480, so it is the same value after conversion
        long total3=money*((long)year);//You can convert a number to long first
        System.out.println(total3);//20000000000

Topics: Java