Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Java programming language is platform-independent and a secure programming language. With various applications, Java programming language has been in demand for the last two decades. The out-of-the-box features help java stand apart. In this article, we will understand the structure of a java program in detail. Following are the topics discussed in this blog:
It is used to improve the readability of the program. It consists of comments in Java which include basic information such as the method’s usage or functionality to make it easier for the programmer to understand it while reviewing or debugging the code. A Java comment is not necessarily limited to a confined space, it can appear anywhere in the code.
The compiler ignores these comments during the time of execution and is solely meant for improving the readability of the Java program.
There are three types of comments that Java supports
Single line Comment
Multi-line Comment
Documentation Comment
Let’s take a look at an example to understand how we can use the above-mentioned comments in a Java program.
// a single line comment is declared like this /* a multi-line comment is declared like this and can have multiple lines as a comment */ /** a documentation comment starts with a delimiter and ends with */
A provision in Java allows you to declare your classes in a collection called package. There can be only one package statement in a Java program, which must be at the beginning of the code before any class or interface declaration. This statement is optional, for example, take a look at the statement below.
package student;
This statement declares that all the classes and interfaces defined in this source file are a part of the student package. And only one package can be declared in the source file.
Many predefined classes are stored in packages in Java, an import statement is used to refer to the classes stored in other packages. An import statement is always written after the package statement but it has to be before any class declaration.
We can import a specific class or classes in an import statement. Take a look at the example to understand how import statement works in Java.
import java.util.Date; //imports the date class import java.applet.*; //imports all the classes from the java applet package
This section is used to specify an interface in Java. It is an optional section which is mainly used to implement multiple inheritance in Java. An interface is a lot similar to a class in Java but it contains only constants and method declarations.
An interface cannot be instantiated but it can be implemented by classes or extended by other interfaces.
interface stack{ void push(int item); void pop(); }
A Java program may contain several class definitions, classes are an essential part of any Java program. It defines the information about the user-defined classes in a program.
A class is a collection of variables and methods that operate on the fields. Every program in Java will have at least one class with the main method.
The main method is from where the execution actually starts and follows the order specified for the following statements. Let’s take a look at a sample program to understand how it is structured.
public class Example{ //main method declaration public static void main(String[] args){ System.out.println("hello world"); } }
Let’s analyze the above program line by line to understand how it works.
This creates a class called Example. You should make sure that the class name starts with a capital letter, and the public word means it is accessible from any other classes.
To improve the readability, we can use comments to define a specific note or functionality of methods, etc for the programmer.
The curly brackets are used to group all the commands together. To make sure that the commands belong to a class or a method.
When the main method is declared public, it can also be used outside of this class.
The word static means that we want to access a method without making its objects. As we call the main method without creating any objects.
The word void indicates that it does not return any value. The main is declared as void because it does not return any value.
Main is the method, which is an essential part of any Java program.
It is an array where each element is a string, which is named as args. You can pass the input parameter if you run the Java code through a console. The main() takes it as an input.
The statement is used to print the output on the screen where the system is a predefined class, out is an object of the PrintWriter class. The method println prints the text on the screen with a new line. All Java statements end with a semicolon.
This brings us to the end of this article where we have learned about the structure of a Java program. I hope you are clear with all that has been shared with you in this tutorial.
If you found this article on “Structure Of A Java Program” relevant, check out Edureka’s Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey. We have compiled the list of Top Java Interview Questions and developed a curriculum designed for students and professionals who want to be Java developers. The course is designed to give you a head start into Java programming and train you for core and advanced Java concepts and various Java frameworks like Hibernate & Spring.
If you come across any questions, feel free to ask all your questions in the comments section of “Structure of a Java Program” and our team will be glad to answer.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co