Java-basic-syntax
Java-basic-syntax
Wee
k Lesson 1: Basic Syntax
1-2
This Module is written for you to serve as your guide in learning to code a
program using java language. As a beginner it is very important to understand
the set of rules in using the different symbols or reserved words to make
statements or expressions in java language correct or valid.
After going through this module, you are expected to:
● describe syntax;
What’s In
Before we start our lesson, this are the Basic terminologies in Java that
you may encounter in this module:
1. Class: The class is a blueprint of class objects and status.
2. Identifier: The name of method, class, datatypes.
3. Method: The behavior of the program. It is where the logics are written, data is
manipulated, and all the actions are executed.
4. Variable: a piece of memory that can contain a value.
5. Object: The object is an instance of a class, have Behavior and state.
2
What’s New
Activity 1: Syntax
I. Objective:
● describe syntax:
II. Instructions:
Sample Syntax:
Class NameofClass { 🡪in naming the main class name the first letter
should be in Upper Case. If several words are used to form a name of the
class, each inner word's first letter should be in Upper Case.
After the name put open curly brace ({)
public static void main(String[] args) { 🡪 main method
System.out.println(“type the text you want to display”); 🡪 always close
your statement with semi colon (;) and always put double quote in
displaying your text
} 🡪 close main method using close curly braces
} 🡪 close main class using open curly braces
3
}
What is It
Syntax
Syntax is the set of rules that define what the various combinations of symbols
mean. This tells the computer how to read the code. Syntax refers to a concept
in writing code dealing with a very specific set of words and a very specific order
to those words when we give the computer instructions. This order and this strict
structure is what enables us to communicate effectively with a computer. Syntax
is to code, like grammar is to English or any other language. This syntax is why
we call programming coding. Even amongst all the different languages that are
out there. Each programming language uses different words in a different
structure in how we give information to get the computer to follow our
instructions.
4
*/
public class JavaApplication2 { 🡪 main class
public static void main (String[] args) { 🡪 main method
/* main method */ 🡪 comment in java
System.out.println(“JGMNHS”); 🡪 statement in java
} 🡪 close curly braces
} 🡪 close curly braces
1. Package
A package in Java is used to group related classes. Think of it as a folder in
a file directory. Use packages to avoid name conflicts, and to write a
better maintainable code. Packages are divided into two categories:
e.g:
package projectname;
e.g.: // System.out.println("GFG!");
b. Multi-line Comment
e.g.: /*
System.out.println("GFG!");
System.out.println("Alice!");*/
5
The name of a program file should exactly match the class name with an
extension of .j ava. The name of the file can be other names if the
program does not have any public class. Assume you have a public class
GFG.
e.g.:
4. Case Sensitivity
Java is a case-sensitive language, which means that the identifiers AB, Ab,
aB, and ab are different in Java.
e.g. :
System.out.println(a);
System.out.println(A);
12
11
e.g.:
5. Class
Class should always start with (public, private, protected or default) to be
followed by its identifier or class names, class name first letter should be
in Upper Case. If several words are used to form a name of the class, each
inner word's first letter should be in Upper Case then use open and close
curly braces.
e.g. :
6
public class A {
}
private class B{
}
protected class B{
}
class B{
}
6. Method
Method should always start with access modifier like (public, private,
protected or default) then use the reserve word void and to be followed
by method names, it should start with a Lower Case letter. If several words
are used to form the name of the method, then each inner word's first
letter should be in Upper Case then use open and close parenthesis
e.g.: Method
void myMethodName(){
7. Main Method
7
}
8. Identifier
e.g.
int a = 13; 🡪 lowercase a is not equal to Uppercase A
int A = 14;
int _a = 2; 🡪 identifier can begin with an underscore or any combination of
characters.
int int = 12; 🡪 error because int is reserved word
//Get the sum of a and A
System.out.println(a+A); 🡪 output of the program is 27
9. White-spaces in Java
e.g.:
Output:
8
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while
All statement in java must end with semicolon and every block must end
with open and close parentheses like class and method.
e.g:
int a = 13; 🡪 this is a statement and it end with semicolon (;)
e.g:
Program Error
It is an error of language resulting from code that does not conform to the
syntax of the programming language or does not conform to the output of
the program; "program errors can be recognized at compilation time or
during the testing phase of the program.
Two type of program error
9
int x = 10;
System.out.println(x) 🡪 this part is syntax error because every
statement must end with semicolon (;).
}
}
10
11