0% found this document useful (0 votes)
2 views

Java-basic-syntax

This document serves as a guide for beginners learning Java programming, focusing on basic syntax rules and terminology. It covers essential concepts such as classes, methods, variables, and the distinction between syntax and logical errors. Additionally, it provides examples of correct and incorrect code structures to illustrate proper syntax usage in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java-basic-syntax

This document serves as a guide for beginners learning Java programming, focusing on basic syntax rules and terminology. It covers essential concepts such as classes, methods, variables, and the distinction between syntax and logical errors. Additionally, it provides examples of correct and incorrect code structures to illustrate proper syntax usage in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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;

● differentiate logical error and syntax error;

● apply java syntax in IDE

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:

● Analyze and review the given syntax and actual code

● Answer the guide questions

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

Actual Code sample 1:

public class FirstCode {


public static void main(String[] args) {
System.out.println("My First Program”);
}
}

Actual Code sample 2:

public class Firstcode


public static void main(String[] args) {
System.out.println("My First Program”)
}

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.

Basic Syntax in Java


Sample Syntax structure of java
1. package
2. comment(optional)
3. main class {
4. method (optional)
5. main method {
6. Comment (optional)
7. statement
8. close curly braces for main class
9. close curly braces for main method

e.g.: Java code


package javaapplication2; 🡪 java package
/**
*
* @author Faith 🡪 comment in java

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:

● Built-in Packages (packages from the Java API)

● User-defined Packages (create your own packages)

e.g:

package projectname;

2. Built-in Packages (packages from the Java API)Comment in Java


Comments can be used to explain Java code, and to make it more
readable. It can also be used to prevent execution when testing
alternative code.

There are three types of comments in Java.

a. Single line Comment

e.g.: // System.out.println("GFG!");

b. Multi-line Comment

e.g.: /*

System.out.println("GFG!");

System.out.println("Alice!");*/

c. Documentation Comment or a doc comment.

e.g.: /** documentation */

3. Program file name in Java

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.:

File name: Operators

public class Operators { 🡪 the class should match to filename which is


Operators
public static void main(String[] args) {
System.out.println("Hello”);
}
}

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. :

int a = 12; 🡪 identifier a

int A = 11; 🡪 identifier A

System.out.println(a);

System.out.println(A);

Output of the program:

12

11

e.g.:

System.out.println("Alice"); // valid syntax

System.out.println("Alice"); // The first letter of System must be capital


letter

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

public void myMethodName(){

private void myMethodName(){

protected void myMethodName(){

void myMethodName(){

7. Main Method

Java program processing starts from the main() method which is a


mandatory part of every Java program. It is located inside the main class.

e.g.: main method

public class Sample{ 🡪 Main Class

public static void main(String[] args) { 🡪 main method

7
}

8. Identifier

All identifiers can begin with a letter (A to Z or a to z) or an underscore _.


The first character of identifiers can have any combination of characters.
Most importantly identifiers are case-sensitive. A keyword cannot be used
as an identifier since it is a reserved word and has some special meaning.

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

A line containing only white-spaces, possibly with the comment, is known


as a blank line, and the Java compiler totally ignores it.

e.g.:

String s = “ ”; 🡪 this identifier contain white-spaces


System.out.println(s); 🡪 when you run the program it will run successfully
but it will show no output.

Output:

10. Java Keywords

Basic Keywords or Reserved words are the words in a language


that are used for some internal process or represent some predefined
actions. These words are therefore not allowed to use as identifier,
variable names or objects.

abstract assert boolean break


byte case catch char

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

11. Statement and block

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:

public class Sample{ 🡪 this is Main Class

public static void main(String[] args) { 🡪 main method

12. Display text

To display text in java always use System.out.println(“put text here”); or


System.out.print(“put text here”);

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

1. Syntax error is an error in the syntax of a sequence of characters


or tokens that is intended to be written in compile-time. A program
will not compile or run until all syntax errors are corrected.
public class JavaApplication10 {
public static void main(String[] args) {

9
int x = 10;
System.out.println(x) 🡪 this part is syntax error because every
statement must end with semicolon (;).
}
}

2. Logical Error is an error that can be the hardest to track down.


Everything looks like it is working but the results or output of the
program will not work.

public class JavaApplication10 {


public static void main(String[] args) {
int x = 10;
int y = 10;
System.out.println(x-y ) ;//add two number 🡪 this part is logical
error because the program did not add x and y instead it subtract x
and y.
}
}

public class JavaApplication10 {


public static void main(String[] args) {
int x = 10;
int y = 10;
System.out.println(x ) ;//print the value of y
System.out.println(y ) ;//print the value of x 🡪 this part is
logical error because the program did not add x and y instead it
subtract x and y.
}
}

10
11

You might also like