How to code in Java for beginners.
Hello World, Escape sequence and comments in Java.
In the previous lesson, we talk about how to set-up your
environment to start writing Java projects if you are trying it for the first
time.
In this section, we are going to talk about the most basic
concept in java. This are running your first code on the console, using escape
sequence and how to write comments in java.
Running Your First Java Project
The most popular first Java code we all write as a developer
is the Hello World! Code.
package com.techwithnate;
public class Main {
public static void main(String[] args) {
// This is a single line comment
System.out.println("Hello World");
}
}
NOTE: There is a short cut in typing System.out.println();
and this is sout + Tab or Enter key
Run
When using the “print” statement, It displays all statements
on the same line but when using the “println” statement, it displays different
output on different lines.
Escape Sequence.
It is used as a shortcut to perform specific tasks like
adding a tab space, printing on a new line and other more. Let’s dive in a
little of escape sequence.
1.
New Line escape sequence
System.out.print("Hello World\n");
System.out.print("I love codding");
I love coding is printed on a new line.
Check out the escape sequence below and how they work.
System.out.print("Hello World\n"); // This is a next line escape sequence
System.out.println("\tI love Programming"); // This is tab escape sequence
System.out.println("\\I love coding in Java\\"); // This is to print backward slash '\'
System.out.println("\"Java is good\""); // This prints a double quotation ""
System.out.print("I love codding");
Output.
Hello World
I love
Programming
\I love coding in Java\
"Java is good"
COMMENTS IN JAVA
Comments Help to explain lines of codes and also enlightens
other developers about the functionality of a certain line of code. Comments
are not printed on the console, it is ignored by the interpreter.
There are two types of comments in java.
1.
Single line comment
2.
Multi line comment
package com.techwithnate;
public class Main {
public static void main(String[] args) {
// This is a single line comment
/*
This
is
a
multi
line
comment
*/
}
}
CONGRATULATIONS FOR ENDING UP YOUR FIRST BASIC LESSON IN JAVA
Next, we are going to talk about Variables
and operators.
0 Comments