Take printing “hello world” as an example. Java doesn’t have a package management system, so some projects use Maven to manage external libraries. Maven command line usage:
Variable Declaration and Data Types
A variable is an identifier that points to an address in memory, generally used to store data. Data type indicates the type of a variable, and the type determines the amount of memory space this variable occupies. Assignment means storing data (value) into the address pointed by the variable. Subsequently, we can use this variable to retrieve the data.
publicclassLoopExamples { publicstaticvoidmain(String[] args) { // for loop for (inti=1; i <= 5; i++) { System.out.println("For loop: " + i); }
// while loop intcount=0; while (count < 5) { System.out.println("While loop: " + count); count++; }
// do-while loop intvalue=0; do { System.out.println("Do-while loop: " + value); value++; } while (value < 5); } }
Functions
The essence of a function is a closure with its own scope. We can use functions to define a block of code that receives zero or more inputs, executes the code inside the function body, and returns zero or more outputs when finished. Using functions allows us to extract common logic and simplify our code.
A class is an abstract structure that contains data and methods (functions). We can define it, and to use a class, we need to instantiate it, which essentially allocates specified memory space to save it. The size of a class depends on what data is defined internally (such as int), and the compiler will automatically decide how much space to allocate based on the data size. A class can have multiple instantiated objects, meaning there can be multiple different variables, but these variables all conform to the structure of this class.
An interface is a way to define behavioral specifications. It describes what methods (functions) a certain type should have but does not care about how these methods are implemented specifically. An interface is an abstract contract:
It only defines method names, parameters, and return values.
It does not include any specific implementation (logic code).
As long as a type implements all the methods defined in the interface, it is said to “implement the interface.”
To simplify development, programmers extract common logic and encapsulate it into functions or classes and methods to streamline development. In this ongoing simplification process, frameworks were born. However, in my understanding, a library is a collection of extracted common methods and classes, and users can add various different libraries according to their preferences; whereas a framework is more like defining a set of project specifications and templates, and then users develop based on the framework following the standards or structures defined by the framework. However, most of the time this boundary isn’t very clear, and the two terms are often used interchangeably. Java basically uses the Spring family of tools. The mainstream development is SSM, which is Spring, Spring MVC, MyBatis. Sometimes microservices are used with Spring Cloud. Before the advent of Spring Boot, the configuration of the Spring framework was very cumbersome, so the framework examples in this tutorial are based on Spring Boot.
Web
The programs we currently write are executed locally. To allow users around the world to also use them, they need to be made available on the network. A web framework encapsulates the language’s native network libraries and provides various methods for offering network services. The commonly used web framework in Java is Spring MVC.
Currently, we are using variables to store data, but variable data is stored in memory. Once the program stops, the data in memory will be reclaimed. The next time the program starts, the operating system may allocate another memory space. Therefore, we need persistent storage for our data, which requires the use of a database system. The commonly used database framework in Java is MyBatis.