Java Quick Start Notes-02 Java Basics (basic data types, variables and constants, operators, arrays, strings)

1. Java basic data types

The Java language provides 8 basic types. 6 numeric types (4 integers, 2 floating point types), 1 character type, and 1 Boolean type.
insert image description here

1. Integer type

Java's integers are signed , and they are in two's complement form in binary representation (the highest bit is 0 for positive and 1 for negative). unsignedThere is no unsigned type in C.

  • byte byte type
    size: 8 bit;
    range: -128 ~ 127;
    default value: 0;

  • int integer
    size: 32 bits;
    range: -2,147,483,648 (-2^31) ~ 2,147,483,647 (2^31 - 1)
    default value: 0;

  • short Short integer
    Size: 16 bits;
    range: -32768 (-2^15) ~ 32767 (2^15 - 1);
    default value: 0;

  • long Long integer
    Size: 64 bits;
    range: -9,223,372,036,854,775,808 (-2^63) ~ 9,223,372,036,854,775,807 (2^63 -1);
    default value: 0;

2. Floating point/decimal type

  • float floating point
    size: 32bit;
    precision: single precision;
    default value: 0.0f;

  • double floating point
    size: 64bit;
    precision: double precision;
    default value: 0.0d;

  • Floating point numbers can be expressed in scientific notation, such as:

float f1 = 3.14f;
float f2 = 3.14e38f; // 科学计数法表示的3.14x10^38
double d = 1.79e308;
double d2 = -1.79e308;
double d3 = 4.9e-324; // 科学计数法表示的4.9x10^-324

Note: When the variable type is float, you need to add f or F suffix to indicate its precision type.

3. Character type

Character type char represents a single 16-bit Unicode character;

  • char Character
    range: \u0000~ \uffff
    For example:char Char = 'A';

4. Boolean type

A Boolean type is true or false, nothing to say.
For example:boolean T = true;

The bits occupied by each data type, as well as the maximum and minimum values ​​can be viewed in a manner similar to the following.

insert image description here

5. Reference type

First of all, the reference type does not belong to the basic data type. The variable of the reference type is similar to the pointer of the C language, and the internal storage is an address. Java's reference is similar to the reference concept in C++, and it can be said that it was derived to get rid of the complicated thing of pointers.

The most commonly used reference type is the string type String, such as defining a string:String str = "string";

2. Variables & constants

1. Variables

Variable definitions:

  • Method 1 (definition only):

Type + variable name, such as: int a;

  • Method 2 (definition and assignment):

Type variable name = variable value, such as: int a=1;

Rules for variable naming:

  • Must start with an English letter followed by a combination of letters, numbers and underscores

2. Constants

Variables can be reassigned, and the value of a constant cannot be modified after it is initialized at the time of definition .

Define constants using keywords final, such as:

final double PI = 3.14; // PI是一个常量

3. Operators

The most basic operators in Java include:

  1. Arithmetic operators
    include addition, subtraction, multiplication and division, +, -, *, /, and self-increment and self-subtraction: ++, --.

  2. Relational Operators
    Relational means: ==equal, !=not equal, >greater than, >=greater than or equal to, <less than, <=less than or equal to.


  3. The classic bit operations in Java of bit operators in C are also available in Java, namely: &bit and, |bit or, ^bit exclusive or, ~bit inversion, <<left shift, >>right shift, and a >>>right shift to fill zero, let's ignore him.

  4. Logical operators
    include: &&logical and, ||logical or, !logical not.

  5. Assignment operators are derived
    from equal sign assignment combined with bitwise operators =, including the following:
    + =, - =, , * =, / =, % =, << =, >> =, &=, ^ =.| =

  6. Other operators
    such as the conditional operator: x?y:z, also known as the ternary operator.

The operators of Java are basically the same as those of the C language. If you have learned C, you can skip this part directly.

4. Arrays and strings

1. Array

The way Java defines an array is a bit different from C, and the definition method is as follows:

Type+[]+array name, such as: int[] array;//Define an integer array array

Using method() in C/C++ int array[]is also possible, but not recommended.

  1. Examples of definition operations:
  • Define and initialize an array:
int[] array = {
    
     1, 2, 3, 4, 5};
  • Define an integer array of size 10:
int[] array = new int[10];
  • Initialize with new operator:
int[] array = new int[] {
    
     1, 2, 3, 4, 5};
  1. Accessing Array Elements
    Use an index to access an element of an array:
int[] array = {
    
     1, 2, 3, 4, 5};
a = array[0]; // a = 1
  1. Java array features
  • All elements of the array are initialized to the default value, the integer type is 0, the floating point type is 0.0, and the Boolean type is false;
  • The size of an array cannot be changed after it is created.

2. string

  • The difference between characters and strings
    Java's character type charoccupies 16 bits, which is 2 bytes. One more byte than char in C, so it can store a wider range of Unicode characters. Unicode encoding is stored in two bytes whether it is English or Chinese. Introduction to Unicode: Unicode .

A string is a sequence of characters, containing at least 2 characters.

  • express

The Java string type Stringis a reference type, and the string is represented by double quotes "...". like:

String s = ""; // 空字符串,包含0个字符
String s1 = "A"; // 包含一个字符
String s2 = "ABC"; // 包含3个字符
String s3 = "中文 ABC"; // 包含6个字符,其中有一个空格

Double quotes represent a single-line string. When you need to represent a multi-line string including newlines, you can use three double quotes (starting from java 13), such as:

public class Main {
    
    
    public static void main(String[] args) {
    
    
        String s = """
                  Hello
                  world
                  !!!
                   """;
        System.out.println(s);
    }
}

The printed result is like this:

Hello
world
!!!

  • Escape characters
    When we need to display double quotes or other special characters such as carriage return and line feed (\r\n) in a string, escape characters are used, such as:
String Str = "this is \"this\""; //字符串Str的内容是: this is "this"

Because "..."adding double quotes between them will cause interference, here it is \"expressed as an escape character . Common escape characters are as follows:"\"

\"Represents a character "
\'Represents a character '
\\Represents a character \
\nRepresents a line break
\r Represents a carriage return
\t Represents Tab
\u####Represents a Unicode encoded character

  • Concatenating strings
    You can concatenate two strings using +symbols, for example:
public class Main {
    
    
    public static void main(String[] args) {
    
    
        String s1 = "Hello";
        String s2 = "world";
        String s = s1 + " " + s2 + "!";
        System.out.println(s);//hello world!
    }
}

Guess you like

Origin blog.csdn.net/qq_41790078/article/details/113090682