0% found this document useful (0 votes)
1 views18 pages

Java Lecture Two

This document outlines a lecture on Java syntax as part of an OOP course, covering basic concepts such as classes, objects, methods, and instance variables. It includes a simple Java program example, guidelines for naming conventions, and an introduction to Java modifiers and variables. Additionally, it provides information on comments and a sample program for adding two numbers.

Uploaded by

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

Java Lecture Two

This document outlines a lecture on Java syntax as part of an OOP course, covering basic concepts such as classes, objects, methods, and instance variables. It includes a simple Java program example, guidelines for naming conventions, and an introduction to Java modifiers and variables. Additionally, it provides information on comments and a sample program for adding two numbers.

Uploaded by

manunerd1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

COMP 223/INTE 222/COSF 311

OOP WITH JAVA

LECTURE TWO
JAVA SYNTAX

OMBUYA D.O.
Devotional Meditation

Proverbs 18:15
An intelligent heart acquires knowledge, and the ear of
the wise seeks knowledge.
Lecture Outline

1. Review of the previous lecture


2. Item 1.... Basic JAVA syntax
3. Item 2 … First JAVA program
4. Item 3 … Modifiers
5. Item 4 … Addition of two numbers program.
PREVIEW OF THE PREVIOUS CLASS

1. Introduction to OOP.
2. Overview of OOP concepts
3. Introduction to Java
4. History of Java
5. Features of Java
JAVA BASIC SYNTAX
A Java program can be defined as a collection of objects that communicate
via invoking each other's methods.

what do class, object, methods and instance variables mean.

Object - Objects have states and behaviors. Example: A dog has states - colour,
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
behaviours/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.
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.
First Java Program:
Let us look at a simple code that would print the words Hello World.

public class MyFirstJavaProgram {


//This is my first java program .
// This will print 'Hello World' as the output

public static void main(String []args) {


System.out.println("Hello World"); // prints Hello World
}
}
Basic 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
RememberJavaiscasesensitive and append '.java' to the end of the name
ifthefilenameandtheclassnamedonotmatchyourprogramwillnotcompile
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 AtoZ or atoz, currency character $ or
an underscore _ .
After the first character identifiers can have any combination of characters.
A key word 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
We will be looking into more details about modifiers in the next
section.
Java Variables:
We would see following type of variables in Java:
Local Variables
Class Variables StaticVariables
Instance Variables Non − staticvariables

Java Keywords:

The following list shows the reserved words in Java. These reserved
words may not be used as constant or variable or any other identifier
names.
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 Java
compiler.

public class MyFirstJavaProgram {


/* This is m y first java program .
* This will print 'Hello World' as the output
* This is an exam ple of m ulti-line com m ents.
*/
public static void m ain(String []args){
// This is an exam ple of single line com m ent
/* This is also an exam ple of single line com m ent. * /
System .out.println("Hello World");
Using Blank Lines:
A line containing only white space, possibly with a
comment, is known as a blank line, and Java totally ignores
it.
ADDITION OF TWO NUMBERS
import java.util.Scanner;

class AddNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter two integers to calculate their sum ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = "+z);
}
WELCOME

PLEASE PROCEED TO MOODLE. THE QUIZ


STARTS AT 4:40. IT RUNS TILL SEVEN.
ONCE YOU OPEN IT, YOU DO IT FOR
30MINUTES MAXIMUM! ITS A TIMED QUIZ
END

You might also like