- Java Introduction
- Java Getting Started
- Java Syntax
- Java Comments
- Java Variables
- Java Data Types
- Java Type Casting
- Java Operators
- Java Strings
- Java Math
- Java Booleans
- Java If ... Else
- Java Switch
- Java While Loop
- Java For Loop
- Java Break And Continue
- Java Arrays
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Scope
- Java Recursion
- Java OOP
- Java Classes And Objects
- Java Class Attributes
- Java Class Methods
- Java Constructors
- Java Modifiers
- Java Encapsulation
- Java Packages
- Java Inheritance
- Java Polymorphism
- Java Inner Classes
- Java Abstraction
- Java Interface
- Java Enums
- Java User Input (Scanner)
- Java Date And Time
- Java ArrayList
- Java LinkedList
- Java HashMap
- Java HashSet
- Java Iterator
- Java Wrapper Classes
- Java Exceptions - Try...Catch
- Java Regular Expressions
- Java Threads
- Java Lambda Expressions
- Java Files
- Java Create And Write To Files
- Java Read Files
- Java Delete Files
Java Syntax
Java Syntax
It seems like you want to continue discussing the previous chapter where we created a Java file named Main.java
and used the code to print "Hello World" to the screen. Here's a recap of the code snippet we used:
Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Example explained
-
Every line of code in Java must be inside a class:
- In Java, code is organized into classes, which serve as blueprints for objects. All code must be contained within a class, even if it's just a simple program.
-
Class names should start with an uppercase letter:
- By convention, class names in Java start with an uppercase letter. This makes it easier to distinguish classes from other identifiers like variables and methods.
-
Java is case-sensitive:
- Java treats uppercase and lowercase letters as distinct. For example, "MyClass" and "myclass" would be considered different identifiers in Java.
-
File name must match the class name:
- When saving a Java file, the filename must match the name of the class declared in the file. Additionally, the file should have a ".java" extension to indicate that it contains Java source code.
Practice Excercise Practice now
The Main Method
the main()
method is indeed required in every Java program. Here are some key points about the main()
method:
-
Entry Point: The
main()
method serves as the entry point for Java applications. When you execute a Java program, the Java Virtual Machine (JVM) starts by invoking themain()
method. -
Signature: The
main()
method must have a specific signature:- It must be declared as
public
, which means it can be accessed from outside the class. - It must be declared as
static
, which means it belongs to the class rather than to any instance of the class. - It must return
void
, indicating that it doesn't return any value. - It must be named
main
. - It must accept a single parameter of type
String
array (String[]
), typically namedargs
. This parameter allows command-line arguments to be passed to the program.
- It must be declared as
-
Syntax:
public static void main(String[] args) { // Main method body } -
Execution: When you run a Java program, the JVM looks for the
main()
method in the specified class and starts executing from there. It's where the actual logic of the program begins. -
Example Usage:
- In our previous example, we used the
main()
method to print "Hello World" to the console. This is a common use case to demonstrate the basic structure of a Java program.
- In our previous example, we used the
Overall, the main()
method is a fundamental part of Java programming, and you'll find it in every Java application. It provides a starting point for program execution and allows you to define the initial behavior of your application. If you have any questions about the main()
method or anything else related to Java, feel free to ask!
Practice Excercise Practice now
System.out.println()
nside the main()
method, you can use the println()
method to print a line of text to the screen. Here's a quick recap:
-
System.out.println(): In Java,
System.out
is an object that represents the standard output stream. It provides methods for printing data to the console. Theprintln()
method is one such method, which is used to print a line of text to the console. -
Usage: You can use the
println()
method to display messages, variables, or any other information to the user. It automatically adds a newline character at the end of the output, so each call toprintln()
starts a new line.
public static void main(String[] args) {
System.out.println("Hello World");
}
Note: The curly braces {}
marks the beginning and the end of a block of code.
Note: Each code statement must end with a semicolon.
Practice Excercise Practice now