Recap
A class, a main method, and a statement. Let us summarize these three elements:
A class: The class declaration in our example has three components:
- Optional modifiers like
public(the meaning of which you will learn later). - A name in noun form with the first letter capitalized, following Java naming conventions.
- A class body surrounded by curly braces,
{}.
At a minimum, a class must have a name and a set of curly braces for the JDK to compile it. However, to run it from the command line, it must also have a main method.
A main method: The main method must have:
- The
publicandstatickeywords (in any order). - A
voidreturn type. - The method name
main. - A parameter of type
Stringarray, represented byString[]. You can name this parameter whatever you want, althoughargsis typically used. - A method body containing optional statements enclosed between curly braces, which forms the
mainmethod's code block.
A statement: A statement is a single line of executable code that ends with a semicolon (;). For example, the System.out.println statement allows us to write to the console, which is why we see "Hello World" printed.
What is a Java program (a.k.a. application)?
A typical Java desktop program is a collection of one or more classes. Typically, one of these classes contains a main method, which serves as the program's entry point. A Java source file can contain more than one class definition; however, the file name must match the name of the public class. Each class definition is compiled into a separate .class file containing Java bytecode. The name of the .class file matches the name of the class defined in the .java file. All .java files must first be compiled into .class files before they can be run.
What is a library?
A library is a collection of pre-written code that you can use in your own programs.
- Libraries allow programmers to reuse tested code for common tasks instead of writing it from scratch.
What is an API?
API stands for Application Programming Interface. It is a set of rules and specifications that software programs follow to communicate with each other.
- An API defines the "contract" for how to interact with a system or library. It specifies what inputs are required and what outputs to expect.
Example: Think of a restaurant menu. The menu is the API. It lists the dishes you can order and what you need to provide (the name of the dish). You don't need to know how the kitchen (the system) prepares the food; you only need to follow the rules on the menu to get your meal.
In Java, the Math class provides an API for mathematical operations. For example, to find a square root, you use:
Task: Calling a method from the Math library API.
Math.sqrt(16.0);
The API for Math.sqrt specifies that you must provide a double value, and it will return the result as a double. You don't need to know the complex algorithms used to calculate the square root; you just need to know how to interact with the API.
What is a framework?
A framework is a pre-written set of code that provides a structure for developing applications. It offers a foundation on which you can build your own software, saving you from writing everything from scratch. Frameworks often include libraries, APIs, and other tools to help you develop your application more efficiently.
- A framework provides a rigid structure or "skeleton" for an application.
What is the difference between a library and a framework?
The key difference between a library and a framework is "inversion of control."
- With a Library, your code is in control and calls the library.
- With a Framework, the framework is in control and calls your code.
When you use a library, you are in control; you call the library's methods when you need them. When you use a framework, the framework is in control; it calls your code when it needs to.
For example, when you use a math library, you call the library's functions to perform calculations. When you use a web framework, the framework calls your code to handle incoming requests.
In short, your code calls a library, but a framework calls your code. We will NOT be working with framworks in this course.