banner
Onei

Onei

github
nintendo switch
discord server
steam
pixiv
bento
follow

☕ Java Introduction

🏗️ Java Technology Stack#

  • Basic Programming
  • Object-Oriented Programming
  • Arrays
  • Exception Handling
  • Common APIs
  • Collections + Map
  • IO Streams
  • Multithreading
  • Network Programming
  • Regular Expressions
  • Lambda Expressions + Functional Interfaces
  • Reflection
  • GUI

📌 Java Directions#

  • JavaSE
  • JavaME
  • JavaWEB
  • JavaEE

🔰 Java Features#

  • Multithreading
  • Platform Independent
  • Object-Oriented
  • Automatic Garbage Collection

Java is a statically typed interpreted language.
Static Type specifies the data type when creating a variable.
Dynamic Type does not require specifying the data type when creating a variable; the data type is determined when assigning a value at runtime.
Strong Type requires explicit type conversion to convert between types.
Weak Type automatically converts data types based on the context of the program.
Compiled programming languages are executed by a compiler.
Interpreted programming languages are executed by an interpreter.

javacjava

xxx.java ----->xxx.class ----->Hello World

Windows/Linux/MacOS

  • Execution: When JVM starts, it loads the class to be executed into the static area of the memory.
    • Once loaded, JVM automatically calls the main method of the class (whenever a method is called, a stack frame is created in the stack memory and pushed onto the stack).
    • If the main method calls other methods:
      • If the method being called is in another class, the corresponding class is loaded into the static area first, and then a stack frame is created in the stack memory.
      • If the method being called is in the current class, a stack frame is directly created in the stack memory.
    • If the called method also calls other methods, the stack frames are continuously created and pushed onto the stack until the top of the stack is reached. Once a method is completed, its stack frame is popped from the stack and the execution continues in the previous stack.
    • The program lifecycle ends when the main stack frame is popped from the stack.

💾 Structure#

Memory Storage#

Electronic bit: 8 bits = 1 byte

The highest bit is the sign bit.

Memory Partitioning#

Static Area/Method Area#

Stores static resources such as static variables, methods, and runtime class files.

VM Stack#

A space created based on the stack data structure.
Used for method execution.
Characteristics: Last In First Out (LIFO).

  • Top of the stack: The last element added to the stack.
  • Bottom of the stack: The first element added to the stack.
  • Stack Frame: An element in the stack data structure.

Push: The process of adding a stack frame to the stack space.
Pop: The process of removing a stack frame from the stack space.
Whenever a method is called, a stack is created in the stack memory and pushed onto the stack. The method starts executing, and after it finishes executing, the stack frame is popped from the stack and the stack is destroyed.

Heap Memory#

Stores class objects, instance variables, and instance methods.

🧾 Data Types#

Primitive Data Types#

  • Numeric Types
    • Integer Types
      • byte: 8 bits
      • short: 16 bits
      • int: 32 bits
      • long: 64 bits
    • Floating-Point Types
      • float: 32 bits
      • double: 64 bits
  • Boolean Type: boolean
  • Character Type: char

Note ⚠️

  • byte: Range from -128 to 127
  • short: Range from -32768 to 32767
  • int: Range from -2147483648 to 2147483647
  • long: Must be suffixed with L or l
  • float: Must be suffixed with F or f. The storage capacity of float (32 bits) is much larger than that of long (64 bits) due to the scientific notation used for float.
  • double: Must be suffixed with D or d, or can be omitted.
  • boolean: Only true and false
  • char: Enclosed in single quotes and can only contain a single character. The range of char is 0 to 65535 and does not include negative numbers. It supports Unicode encoding and can be represented by a hexadecimal integer value from 0 to f: \u0000 to \uffff

Reference Data Types#

  • Class
  • Array
  • Interface

Strings#

\ is the escape character.
+ is the string concatenation operator.

💱 Type Conversion#

Boolean types cannot be converted.

  • Automatic type conversion: From lower precision to higher precision.
  • Forced type conversion: From higher precision to lower precision.

Automatic Type Conversion#

byte --> short --> int --> long --> float --> double

char --> int --> long --> float --> double

Forced Type Conversion#

The higher bits are truncated, which may result in loss of precision.
When converting an integer to a char, if the sign bit is not considered, the corresponding value is 65271.
When converting a negative integer to a char, the negative sign is ignored, and the corresponding binary is converted to the corresponding positive number, which is then compared with the ASCII code.

Mixed-Type Expressions#

When performing mixed-type expressions, the result is the maximum type among the types involved in the operation.
When only byte, short, int, and char are involved in the mixed operation, the result is always int (can be understood as the result of the mixed operation is the maximum type among the types involved in the operation, but the minimum is int).

💬 Comments#

Single-line comment:

// XXX

Multi-line comment:

/*
 * XXX
 * YYY
 */
/**
 * XXX
 * YYY
 */

Can generate Javadoc documentation.

📛 Naming#

Mandatory rules: Only allow uppercase and lowercase letters, $, _, and numbers (numbers cannot be used as the first character). Cannot use keywords and reserved words.
Optional rules: Camel case naming, meaningful names.

💩 Variables#

Global Variables#

Global variables generally refer to variables that can be directly accessed without secondary referencing.
Java does not have global variables.

Variable Declaration#

Allocate memory space using the data type, give the space a name, and assign a value to it.

Variable Classification#

There are four types:

  • Local Variables: Variables declared within a method.
    No initial value, need to be initialized.
  • Static Variables: Variables declared within a class body and are marked with static.
  • Instance Variables: Variables declared within a class body and are not marked with static. They are stored in the heap memory.
  • Constants: Variables marked with both static and final.

Default Values#

  • Integer: 0
  • Floating-Point: 0.0
  • Character: \u0000
  • Boolean: false
  • Reference: null

Scope#

To access a static variable, use the class name followed by the static variable name, considering the access modifier.

Distinguishing#

Use the class name to distinguish between local variables and static variables with the same name.
Use this to distinguish between local variables and instance variables with the same name.

Priority#

Local variables can have the same name as static variables or instance variables.
Static variables and instance variables cannot have the same name.

📐 Operators#

Arithmetic Operators#

Relational Operators#

> >= < <= == != The result is a boolean value.
== compares the values of primitive types and the memory addresses of reference types.

Logical Operators#

  • & && AND
    Supports AND operation. If both sides are numbers, AND operation can be performed.
    Compares each bit of the two numbers in binary. If both bits are 1, the result is 1; otherwise, the result is 0. The result is not greater than the smaller of the two numbers.
  • | || OR
  • ! NOT
  • ^ XOR: Returns true if the two sides are different.
    Performs XOR operation on two numbers in binary. Compares each bit. If the bits are different, the result is 1; if the bits are the same, the result is 0.
  • >> Right Shift: Shifts the bits to the right.
    Shifting to the right once is equivalent to dividing the number by 2 (the sign bit remains unchanged).
  • >>>
    Ignores the sign bit and does not consider positive or negative numbers. Only adds 0 to the left. Negative numbers may become positive.
  • << Left Shift: Shifts the bits to the left.
    Shifts the bits to the left once, deleting the leftmost bit (excluding the sign bit) and adding 0 to the right. Equivalent to multiplying the number by 2.

Assignment Operators#

+= -= *= /= %=

Automatic type conversion is performed during the calculation.

Assignment operators are right-associative. When multiple assignment operators have the same priority, they are calculated from right to left.

int result = x = y = z = 10; 

Due to the right-associativity of =, the calculation is performed as follows: z = 10;, y = z;, x = y;.

Ternary Operator#

xxx?yyy:zzz

  • xxx is the condition to be evaluated, which must be a boolean value.
  • yyy is the statement to be executed if the condition is true.
  • zzz is the statement to be executed if the condition is false.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.