Data types and operators in java

Posted by andrewgk on Tue, 28 Sep 2021 12:45:01 +0200

Data types and operators in java

java variables and types

Variable refers to the variable amount of program operation, which needs to open up memory space to store some data
The type is to divide the types of variables. Different types have different attributes

Basic data type

Numerical type

integer
byte

byte variable name = initial value
Byte type represents an integer, but only takes 1 byte;
The data range is [- 128127]

public static void main(String[] args) {
        System.out.println(Byte.MAX_VALUE);
        System.out.println(Byte.MIN_VALUE);
    }

short

***short variable name = initial value***
short indicates that the type of the variable is an integer
Value range of short [- 3276832767]

public static void main(String[] args) {
        System.out.println(Short.MIN_VALUE);
        System.out.println(Short.MAX_VALUE);
    }

int

Syntax:
***int variable name = initial value***

int indicates that the type of the variable is an integer
In Java, the int type takes up 4 bytes (8 bits per byte)
The data range is - 2 ^ 31 - > 2 ^ 31-1, which is about - 2.1 billion to + 2.1 billion

public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
    }


If the operation result exceeds its data range, an overflow condition will occur

public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE+1);
        System.out.println(Integer.MIN_VALUE-1);
    }


The figure of 2.1 billion is easy to exceed in the current era of big data. In view of this situation, we need to use a wider range of data types to represent it. Therefore, long type is provided in Java

long

Syntax:
***long variable name = initial value***
long takes up 8 bytes and its data range [- 2 ^ 63, 2 ^ 63-1]

public static void main(String[] args) {
        long a=10l;
        System.out.println(a);
    }
public static void main(String[] args) {
        System.out.println(Long.MAX_VALUE);
        System.out.println(Long.MIN_VALUE);
    }

be careful:

  1. The basic syntax format is basically the same as that of creating int variables, except that the type is changed to long
  2. The initialization setting value is 10l, which represents a long integer number. 10l is also OK
  3. You can also initialize with 10. The type of 10 is int, and the type of 10L is long. It is better to use 10 L or 10 L
float
double

Syntax:
double variable name = initial value

public static void main(String[] args) {
        double num = 1.0;
        System.out.println(num);
    }

be careful:
In Java, the value of int divided by int is still int (the decimal part will be discarded directly)

public static void main(String[] args) {
        int a = 1;
		int b = 2;
		System.out.println(a / b);
    }


Although the double in Java is also 8 bytes, the memory layout of floating-point numbers is very different from that of integers, and the data range cannot be simply expressed in the form of 2 ^ n
Wai
The memory layout of Java's double type complies with IEEE 754 standard (like C language), and tries to use limited memory space to represent potentially infinite decimal points and potentials
There must be some precision error

public static void main(String[] args) {
        double num = 1.1;
        System.out.println(num * num);
    }

float

Syntax:
float variable name = initial value

public static void main(String[] args) {
        float num = 1.0F;
        System.out.println(num);
    }

float type occupies four bytes in Java and also complies with IEEE 754 standard. Because the data precision range is small, floating point numbers are generally used in engineering
double is preferred, and float is not recommended

character
char

char variable name = initial value

public static void main(String[] args){
	 	char ch='Oh';
        System.out.println(ch);
}

be careful:

  1. Java uses single quotation marks + single letters to represent character literals
  2. The character in a computer is essentially an integer. ASCII is used to represent the character in C language, while Unicode is used to represent the character in Java. Therefore, a character occupies two bytes and represents more types of characters, including Chinese
Boolean

boolean variable name = initial value

public static void main(String[] args) {
        boolean bool=false;
        System.out.println(bool);
    }

be careful:

  1. There are only two values for boolean variables, true for true and false for false
  2. Java boolean type and int cannot be converted to each other. There is no such usage as 1 for true and 0 for false
  3. boolean type. Some JVM implementations occupy one byte, and some occupy one bit. This is not explicitly specified

operator

Operator:|

***If both binary bits are 0, the result is 0, otherwise the result is 1***

public static void main(String[] args) {
        int a=1;
        int b=2;
        System.out.println(a|b);
    }

Operator:&

If both binary bits are 1, the result is 1, otherwise the result is 0

public static void main(String[] args) {
        int a=1;
        int b=2;
        System.out.println(a&b);
    }

Operator:^

***If the binary bits of two numbers are the same, the result is 0, and if they are different, the result is 1***

public static void main(String[] args) {
        int a=1;
        int b=2;
        System.out.println(a^b);
    }

Operator:~

If the bit is 0, it turns to 1, and if the bit is 1, it turns to 0

public static void main(String[] args) {
        int a=1;
        int b=~a;
        System.out.println(b);
    }

Operator: > >

The rightmost bit is not allowed, and the leftmost sign bit is filled (positive number is filled with 0, negative number is filled with 1)

 public static void main(String[] args) {
        int a=8;
        System.out.println(a>>1);//Shift 1 to the right
    }

Operator:<<

The leftmost bit is not needed, and the rightmost bit is filled with 0

public static void main(String[] args) {
        int a=8;
        System.out.println(a<<1);
    }

Operators: > > >

The rightmost bit is not needed, and the leftmost bit is filled with 0 (whether it is negative or not)

public static void main(String[] args) {
        int a=8;
        System.out.println(a>>>1);
        int b=-1;
        System.out.println(b >>> 1);
    }

Topics: Java