02 - data types and operators

Posted by nkzle on Fri, 14 Jan 2022 21:07:50 +0100

Identifiers and keywords


Java names various variables, methods, classes, packages and other elements as identifiers.

Naming rules for identifiers

  • The identifier can be named by letters, numbers and underscores_ , dollar sign $, but cannot start with a number
  • Identifiers are named strictly case sensitive
  • Identifiers cannot be named with Java keywords and reserved words
  • The naming of identifiers should better reflect their functions, such as name and stuAge


Keywords are used to represent a data type or program structure. They cannot be used as variable name, method name, class name or package name.

There are many keywords, which need to be mastered slowly through the progress of learning.

variable and constant


Three elements of variables: variable type, variable name and variable value

For example, we need to stay in a hotel when we travel abroad. The hotel has different rooms, and each room has a different room number

Naming rules for variables

① Meet naming rules for identifiers

② Comply with hump naming rules (one word can be expressed in lowercase and multiple words can be capitalized)

For example: age # student name stuName

③ try to be as simple as possible, so as to see the name and know the meaning

④ there is no limit on the length of variable names


  • In the Java language, constants are defined by using the final keyword, and their essence is variables whose values can no longer be changed.
  • When a constant is declared, it is either directly assigned or assigned by a constructor
  • Constants cannot be assigned again in the program. If they are forcibly assigned, the program will throw an error message and refuse to accept new values
  • When naming constants, it is recommended to use all uppercase

For example: final double PI = 3.14159;

Data type and literal value

data type

Data types are divided into basic data types and reference data types.

Representation range of basic data type

The representation range of basic data types is as follows: (transferred from muke.com)

Literal value

(1) Integer literal value - > methods for representing integers in Java: binary, octal, decimal and hexadecimal

Binary: composed of 0 and 1,; 3 represents 011,; 8 represents 1000

Octal: numbers starting with 0, including 0-7

Hexadecimal: numbers starting with 0x or 0x, including 0-9, and letters A-F, a-f

(2) floating point literal

  • Floating point literals are of type double (double precision floating point literals) by default, or you can add D or D to the value
  • For example: 123.45, 123.45d, 123.45d
  • If you represent a float type (single precision floating-point literal), you need to add f or F after the literal
  • For example: 23.4f, 23.4f

(3) Character literal

  • Character literals are represented by a single character within single quotes
  • For example, 'a', 'b', '$'

How to define character variables?

 char a = 'a'; / / the output result is: a=a

char b = 65 / / the output result is: b=A

Why can character variables be assigned integers?

Integer represents ASCII code (American Standard Information Interchange Code), while 65 is represented as' A 'in ASCII code

Extension: Unicode encoding

char c = '\u005d'; / / 005d indicates hexadecimal

Unicode notation prefixed with \ u

(4) Boolean literal

Boolean types can only be defined as true and false

For example: boolean flag = true;

(5) String literal

  • 0 or more characters enclosed in double quotes
  • The 0 character is called an empty string

Variable synthesis case

//Variable synthesis case
public class VarDemo {
    public static void main(String [] args){
        //Define two integer variables
        int a,b;
        //The local variable a may not have been initialized
        //Note: variables cannot be output directly without assignment when declared
        a = 3;
        b = 5;//Assignment of two variables (initialization of variables)

        //About line feed
        //If you do not want to wrap a line and want to output on the same line, you can not add ln

        //Line breaks can also be done using line breaks \ n

        //Defines the character of a Chinese character
        char ch = 'in';
        //char Chinese = 'medium'; The variable name can also be Chinese, but this method is not recommended

        //Define floating point data
        double d1 = 123;//Floating point types can be assigned integers
        //The value of a variable of type double can be added with or without D
        double d2 = 123.45;
        double d3 = 123.45d;
        double d4 = 123.45D;
        //float f = 123.45; If the variable value of float type is decimal, f or F must be added, because the decimal is double by default
        float f1 = 123;
        float f2 = 123.45f;
        float f3 = 123.45F;

        //Scientific counting represents floating point data
        double num1 = 1.23e5;//Indicates 1.23 * 10 ^ 5. E or e can be used
        double num2 = .2;//Indicates 0.2
        double num3 = 1.23e-5;//Indicates 1.23 * 10 ^ - 5

Data type conversion

Data type conversion is divided into automatic type conversion and forced type conversion.

Automatic type conversion

Automatic type conversion is also called implicit type conversion.

Conversion order:

Cast type

If the data representation range of type A is larger than that of type B, assigning the value of type A to type B requires forced type conversion.

For example:

double   d = 123.4;

float     f = (float)d; 

Format of cast: (data type) value

Type conversion case

//Type conversion case
public class TypeExchange {
    public static void main(String [] args){
        //Conversion between char type and int type
        //char ch = 65536; The range of char type is 0-65535. If 65536 is assigned to ch, it will be out of range, and forced type conversion is required
        char ch = (char)65536;

        //The type conversion method is automatic type conversion (implicit type conversion)
        int n = ch;//The data representation range of int type is larger than that of char type, and ch can be directly assigned to n
        //Conversion between integer and floating point
        int x = 100;
        long y  = x;//The representation range of int type is smaller than that of long type. You can directly assign x to y
        x =(int)y;//The representation range of long type is larger than that of int type, and the assignment needs to be cast

        float f = 1000000000L;//Long type indicates that the range is less than float type. You can directly assign the data of long type to f
        System.out.println("f="+f);//The result is 1.0E9
        float f2 = 115616684646163L;
        System.out.println("f2="+f2);//The result is 1.15616686E14
        //The accuracy of the original data is lost. It can be seen from the data type conversion data diagram. The dotted line indicates that the accuracy may be lost during type conversion.



Expressions consist of operators and operands.

What is the representation of an expression?

① 5 (a constant, whether integer or floating-point number, is an expression)

② num (a single variable is also an expression)

③ num1+num2 (common expression, which is calculated by two or more numbers)


  • Arithmetic operators: +, -, *, /,%, + +--
  • Relational operators: >, <, > =, < =, = ==
  • Assignment operators: =, + =, - =, * =/=
  • Logical operators:!, & &||
  • Bitwise operators: ~, &, |, ^, > >, <, > > (unsigned shift right)

Topics: Java