Skip to content

Latest commit

 

History

History
55 lines (42 loc) · 1.74 KB

File metadata and controls

55 lines (42 loc) · 1.74 KB

04. First Java Program

create a file name First.java and add the following code

public class First {
    public static void main(String[] args) {
        System.out.println("Hello World!!!");
    }
}
  • compile the program with javac First.java
  • A First.class file will be generated
  • execute the program with java First and you will see the output.
  • When you type java followed by a class name in your terminal application, the system calls the main() method that you defined in that class, and executes its statements in order, one by one.

Creating java files:

  • It is a common practice to only have a single class per file.

  • File and the public class should have same name. In above example the class name and the file name is the same.

  • You can have multiple non public classes inside your java file. but when you compile, for each classes a .class file will be generated. you can do so with javac your_filename

    Copy the below code in a file. since we do not have any public class the filename can be anything. then compile the file using javac compiler.

    you can see four .class files being generated, for each classes.

    class Second {
        public static void main(String[] args) {
            System.out.println("Second: Hello World!!!");
        }
    }
    
    class Third {
    
        public static void main(String[] args){
            System.out.println("Third: Hello World!!!");
        }
    }
    
    class Fourth{
        public static void main(String[] args){
            System.out.println("Fourth: Hello World!!!");
        }
    }
    
    class First{
        public static void main(String[] args){
            System.out.println("First: Hello World!!!");
        }
    }