Day 4 : Java Basic Syntax

Chi-Tse Chiang
2022-09-19 00:18:18

#Intro

At the beginning, we will introduce the basic syntax of Java, including variables, loops, functions, etc. We will enter object-oriented teaching almost in the last two weeks, introducing some class objects and related advanced concepts. This is done in the hope that users with no programming experience at all can learn a programming language from the basic to the advanced and use it as a tool in the future.
(This series of tutorials references W3Schools.)

In Java, every program starts with a class name, and every executable instruction must be placed inside that class. Additionally, the name of the .java file must match the class name.

One more thing to note: Java is a case-sensitive programming language, meaning that uppercase and lowercase letters are treated differently. This is something you need to pay particular attention to!

//Filename : Main.java

public class Main{
	public static void main(String[] args){
		System.out.println("Hello IThome");
	}
}

#Code Parsing

public class Main{}
Here public means and can be accessed by other classes at the same time, as described in more detail below.
Here public means and can be accessed by other classes at the same time, as described in more detail below.

public static void main(String[ ] args){}
main can be regarded as the main program (which must exist for every program), and all instructions to be executed will be placed in it.
Other keywords such as static and void will be introduced later, so for now, just remember the function of the program.

System.out.println(Hello IThome) ;
This is to inform the system to print the content in ( ), and output it to the screen using the println() command
This line is the content to be executed, and it is also placed in main (remember to place the content to be output in )
Because the end of a line, we must inform the computer that this line has ended, so we add ; at the end.

// This is a single-line comment
/* This is a multi-line comment*/

In any language, we will face projects with complex code that is divided into different files. Annotation is therefore an important step at this time, not only to help you quickly understand the content, but also to help others understand your code.

There are two ways to comment in Java:

1. // This is a single-line comment, and its scope is limited to the same line

2. /* */ This is a multi-line comment, and its scope can cover multiple lines

#Output Example

We can also perform mathematical operations using println().

System.out.println(126 + 919);          //Outputs : 1045
System.out.println(126 * 919);          //Outputs : 115794

In addition, there is another keyword, print(). The big difference is that the output will not wrap.
less ln, meaning new line.

System.out.print("Hello ");
System.out.print("World");        //Outputs : Hello World

System.out.println("Hello ");
System.out.println("World");      /* Outputs : Hello
                                               World  */

If there are any errors in the above content, please do not hesitate to let me know, thank you!!!!