How to Write Your First Java Application

How to Write Your First Java Application Photo
Author: Tatyana Milkina

Example of the First Java Application

For Java development, specialized tools like IntelliJ IDEA are commonly used. We'll explore how to create a project in IntelliJ IDEA in a later lesson. However, as a beginner, you can write Java code in any text editor like Notepad or Notepad++. Just make sure to save the file with a .java extension.

Here's an example of your first Java program in a file named MyFirstApp.java:

public class MyFirstApp {
    public static void main(String[] args) {
        System.out.print("Hello world!!!");
    }
}
This program prints the message "Hello world!!!" to the console.

Key Rules for Writing Java Applications

  1. Source File Structure
    A source file is the basic compilation unit. It contains definitions of one or more classes. For now, we'll stick to files with a single class.

  2. File Extension
    The compiler requires source files to have the .java extension.

  3. Class Naming
    By convention, the main class name should match the filename. For example, if your file is MyFirstApp.java, the class should be named MyFirstApp.

  4. All Code Must Reside in a Class
    Java is class-based, so all code must be enclosed within a class.

  5. Case Sensitivity
    Java is case-sensitive. Be consistent with uppercase and lowercase letters.

  6. Braces for Code Blocks
    The class definition and its members must be enclosed in {} curly braces.

  7. Entry Point: main() Method
    Every Java application starts with a main() method. Correct declarations include:

    public static void main(String[] args)
    static public void main(String[] args)
    public static void main(String... x)
    static public void main(String someArgs[])​
    While the compiler allows classes without amain() method, such classes cannot execute without it.
  8. Method Parameters
    Any required data for the method can be passed through parameters inside the parentheses. If no parameters are needed, use empty parentheses.

  9. Semicolons for Statements
    Every statement should end with a semicolon (;).

  10. Ignored Whitespace
    Spaces, tabs, and newlines are generally ignored by the compiler, except where necessary for syntax.

By following these rules, you can confidently write, compile, and run basic Java applications.

Курс 'Java для начинающих' на Udemy Курс 'Java для начинающих' на Udemy
Read also:
Comments