Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views5 pages

COS201 Lecture 4 (Updated)

This document provides an overview of Java programming syntax, including definitions of key concepts such as objects, classes, methods, and instance variables. It covers essential Java syntax rules, identifiers, modifiers, variables, keywords, comments, and inheritance, along with a basic example of a Java program. Additionally, it includes an assignment section with questions related to the content discussed.

Uploaded by

wwwtope947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

COS201 Lecture 4 (Updated)

This document provides an overview of Java programming syntax, including definitions of key concepts such as objects, classes, methods, and instance variables. It covers essential Java syntax rules, identifiers, modifiers, variables, keywords, comments, and inheritance, along with a basic example of a Java program. Additionally, it includes an assignment section with questions related to the content discussed.

Uploaded by

wwwtope947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ACHIEVERS UNIVERSITY, OWO

COLLEGE OF NATURAL AND APPLIED SCIENCES


DEPARTMENT OF COMPUTER SCIENCE
COS 201 – COMPUTER PROGRAMMING I – 3 UNITS

LECTURER IN CHARGE - MR. ADEPOJU, S. E.

LECTURE 4

SYNTAX OF JAVA PROGRAMMING


Introduction
When we consider a Java program, it can be defined as a collection of objects that communicate
via invoking each other's methods. Let us now briefly look into what do class, object, methods
and instance variables mean.
● Object - Objects have states and behaviors. Example: A dog has states-color, name,
breed as well as behaviors -wagging, barking, eating. An object is an instance of a class.
● Class - A class can be defined as a template/blue print that describes the behaviors/states
that object of its type support.
● Methods - A method is basically a behavior. A class can contain many methods. It is in
methods where the logics are written, data is manipulated and all the actions are
executed. A method in Java is a set of instructions that can be called for execution using
the method name. A Java method can take in data or parameters and return a value (both
parameters and return values are optional).
● Instance Variables - Each object has its unique set of instance variables. An object's
state is created by the values assigned to these instance variables.

Basic Java Syntax


About Java programs, it is very important to keep in mind the following points.
● Case Sensitivity - Java is case sensitive, which means identifier Hello and hello would
have different meaning in Java.
● Class Names - For all class names, 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.
Example: class MyFirstJavaClass

● Method Names - All method names 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.
Example: public void myMethodName()
● Program File Name - Name of the program file should exactly match the class name.
When saving the file, you should save it using the class name (Remember Java is case
sensitive) and append '.java' to the end of the name (if the file name and the class name
do not match your program will not compile).
Example : Assume ‘MyFirstJavaProgram’ is the class name, then the file should be saved as
‘MyFirstJavaProgram.java’

● public static void main(String args[]) - Java program processing starts from the main()
method, which is a mandatory part of every Java program.

Java Identifiers
All Java components require names. Names used for classes, variables and methods are called
identifiers. In Java, there are several points to remember about identifiers. They are as follows:
● All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an
underscore (_).
● After the first character, identifiers can have any combination of characters.
● A keyword cannot be used as an identifier.
● Most importantly identifiers are case sensitive.
● Examples of legal identifiers: age, $salary, _value, _1_value
● Examples of illegal identifiers: 123abc, -salary

Java Modifiers
Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There
are two categories of modifiers:
● Access Modifiers: default, public, protected, private
● Non-access Modifiers: final, abstract, strictfp

Java Variables
We would see following forms of variables in Java:
● Local Variables
● Class Variables (Static Variables)
● Instance Variables (Non-static variables)

Java Keywords
Table 1 shows the reserved words in Java. These reserved words may not be used as constant or
variable or any other identifier names.
Table 1: Java Keywords
abstract assert boolean break
byte case catch char
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

Comments in Java
Java supports single-line and multi-line comments very similar to c and c++. All characters
available inside any comment are ignored by the Java compiler. Comments are parts of the
source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow
the programmer to insert notes or descriptions embedded within the source code.
● Single-line comment // single-line comment
● Multi-line comment /* block comment */
The single-line comment discards everything from where the pair of slash signs (//) is found up
to the end of that same line. The multi-line or block comment discards everything between the /*
characters and the first appearance of the */ characters, with the possibility of including more
than one line.
Comments should be used to enhance (not to hinder) the readability of a program. The following
two points, in particular, should be noted:
A comment should be easier to read and understand than the code which it tries to explain. A
confusing or unnecessarily-complex comment is worse than no comment at all. Over-use of
comments can lead to even less readability. A program which contains so much comment that
you can hardly see the code can by no means be considered readable.

Using Blank Lines


A line containing only whitespace, possibly with a comment, is known as a blank line, and Java
totally ignores it.

Inheritance
Java classes can be derived from classes. Basically, if you need to create a new class and here is
already a class that has some of the code you require, then it is possible to derive your new class
from the already existing code. This concept allows you to reuse the fields and methods of the
existing class without having to rewrite the code in a new class. In this scenario, the existing
class is called the superclass and the derived class is called the subclass.

Basic Example
In Java, every application begins with a class name, and that class must match the filename. Let's
create our first Java file, called MyFirstJavaProgram.java, which can be done in any text
editor.
The file should contain a "Hello, COS201 Class" message, which is written with the following
code:

MyFirstJavaProgram.java
1 public class MyFirstJavaProgram {
2 public static void main(String[] args) {
3 System.out.println("Hello, COS201 Class");
4 }
5 }

// Output: Hello, COS201 Class

Every line of code that runs in Java must be inside a class. The class name should always start
with an uppercase first letter. In our example, we named the class ‘MyFirstJavaProgram’.
The name of the java file must match the class name. When saving the file, save it using the class
name and add ".java" to the end of the filename. To run the example above on your computer,
make sure that Java is properly installed.

public class MyFirstJavaProgram {


The ‘public’ is a keyword used to make classes, methods and variables accessible from other
classes. The ‘public’ access modifier indicates that the ‘MyFirstJavaProgram’ method can be
accessed from anywhere within the program, even from outside the class where it is defined.
This is particularly useful when you need to expose certain functionalities or data to other parts
of your application.
The ‘public class MyFirstJavaProgram’ often serves as the backbone of Java applications, a
starting point from where the JVM begins execution.

public static void main(String[] args) {


The main() method is required and you will see it in every Java program. Any code inside the
main() method will be executed. Don't worry about the keywords before and after it. You will get
to know them bit by bit. For now, just remember that every Java program has a class name which
must match the filename, and that every program must contain the main() method.
System.out.println("Hello, COS201 Class");
Inside the main() method, we can use the println() method follow by a semicolon ‘;’to print a
line of text to the screen. You can add as many println() methods as you want but note that it
will add a new line for each method.
There is also a print() method, which is similar to println(). The only difference is that it does
not insert a new line at the end of the output. Text must be wrapped inside double quotations
marks "", if you forget the double quotes, an error occurs.

Note
● The curly braces {} marks the beginning and the end of a block of code.
● System is a built-in Java class that contains useful members, such as out, which is short
for "output". The println() method, short for "print line", is used to print a value to the
screen (or a file).
● Don't worry too much about how System, out and println() works. Just know that you
need them together to print something to the screen.
● You should also note that each code statement must end with a semicolon ( ; ).

ASSIGNMENT
1) In a Java program, can a method have the same name as the class name? Either Yes or No,
give a reason for your answer.
2) Differentiate between Local variable and Class variable.
3) What would happen if one uses an illegal identifier in a Java program?

You might also like