Course Content
Java Basics
Java Basics
Java's Syntax
The Syntax We'll Use in This Course
Let's take a moment to discuss the syntax we'll be utilizing in this course.
The syntax of the Java language can be very intimidating for most beginners because, to write a basic main class, we have to type a bunch of extra and intimidating words.
public static void...
Does this look understandable for a newbie?
But in reality, there's nothing complicated about it. It's just this syntax that's defined as the entry point of the application. The Java compiler will look for this syntax, and then, we can run the code.
You can simply remember how it should look, but below is a brief explanation of what each part is responsible for:
Main
public class Main { public static void main(String[] args) { } }
public class Main
- A class is where we'll be working. Classes are extensively used in Java since it's an Object-Oriented Programming (OOP) language. We'll explore what a class is and how to work with it in a separate course;- The
public static void main
line is what you'll frequently interact with. Let's break down each word:public
is an access modifier that allows themain
method to be accessed from anywhere in the program;static
means that themain
method belongs to the class itself and can be invoked without creating an instance of the class;void
indicates that themain
method doesn't return any value;String[] args
is an array of strings used to pass command-line arguments to the program.
- Java code is always enclosed within curly braces
{ }
, which represent the body of our code; - After each line of code, a semicolon
;
must be placed. Otherwise, the compiler will generate an error. This helps structure our code.
Note
Terms like method, class, instance, string, array, and many others mentioned above might not be familiar to you, but don't worry because you'll learn all of this in this and the following courses. For now, focus on understanding the syntax and that this method represents specific entry points in an application. We'll be working extensively with this
main
method throughout the course.
Output Operation
We can print a message to the console using the command System.out.println()
.
Note that we need to write this command inside the curly braces of the main method. Let's look at an example:
Main
package com.example; public class Main { public static void main(String[] args) { System.out.println("Message for you"); } }
As we can see, this code displays a message for us. Using the System.out.println()
command, we can annotate relevant code snippets and output information that the user will see in the console. We can output any information to the console; the text we want to display should be enclosed in double quotation marks (""
).
Remember that text is displayed using double quotation marks (" "
). Single quotation marks (' '
) represent a completely different data type, which we will learn about later.
An example of the output of any custom text:
Main
package com.example; public class Main { public static void main(String[] args) { System.out.println("Custom message for you"); } }
Well done!
Congratulations, now you know your first Java command. There's a lot more to learn ahead, but the first step is the most important and challenging one.
Thanks for your feedback!