You are on page 1of 32

Lecture 03

OBJECT ORIENTED PROGRAMMING


CST 124-2
Department of Computer Science & Technology

nisal@uwu.ac.lk
Content
• Elements of a Java Program
• Basic Structure of a Java program
• Java Standard Output
• Escape Sequences in Java
• Tokens in Java
• Primitive Data types of Java
First Program in Java
public class Hello
{
public static void main (String[] args)
{
System.out.print(“Hello World!”);
}
}

• Important : Save the file using the class name. (Hello.java)


Compiling & Executing a Java Program
• To Compile :
javac Hello.java

• To Execute :
java Hello
Class name Passing an array
Elements of a Java Program of String objects into
the main method
Access
public class Hello
modifier
{
public static void main (String[] args) Name of the
String array
{
System.out.print(“Hello World!”);
}
}
Output
function
static keyword return type main method
Access modifiers in Java
• An access modifier restricts the access (scope) of a class,
constructor, data member or method from another class. There
are four types of Java access modifiers;
• Public: The access level of a public modifier can be from anywhere
of the program.
• Private: The scope of private modifier is limited to the class only.
• Default: The scope of this modifier is limited to the package only. If
you do not specify any access level, it will be the default.
• Protected: Only accessible by the classes of the same package and
the subclasses present in any package.
default public private protected
Same class Yes Yes Yes Yes
Same package non-sub class Yes Yes No Yes
Same package sub classes Yes Yes No Yes
Different package non-sub classes No Yes No No
Different package sub classes No Yes No Yes
• Static : Only one instance of that static member is created which
is shared across all instances of the class.

• void : Specify that the main method does not return any type of
data.

• System.out.println(“Hello World!”); : A statement which prints the


argument passed to it. The println() method display results on the
screen.
Naming Convention for a class in Java
• A class name;
• Should start with a capital letter. (Not a rule, a norm)
• Can contain letters, digits, underscore (_), dollar sign ($)
• Should not start with a digit.
• Should not contain any white spaces.
• Class names are case sensitive.
• Use Camel Case for class names : only the first letter of each word
capitalized.
• MyClass, EmployeeDetails, _MinTemp, FinalValue, $GrossSalary
Basic Structure of a Java program
• When writing a Java program, there’s an order of the elements
that we include. They are as follows;
1. Comments/Documentation Recommended
2. Package statement Optional
3. Import statements Optional
4. Interface statements Optional
5. class definitions Essential
6. main-method class Essential
1. Comments/Documentation
• Comments can be used to explain Java code, and to make it more
readable.
Single line comments
// Comments can be included here until the end of the line
Multi-line comments
/* Start of the comments

Comments

*/ End of the comments


2. Package statement
• A java package is a group of similar types of classes, just like a
folder.
• package keyword is used to create a package in java.
• There are two types of packages in Java;
• User-defined packages
• Built-in packages (java, lang, awt, javax, swing, net, io, util,
sql)
3. Import statements
• The import keyword is used to import a package, a class or an
interface into the java program.

import java.time.LocalDateTime;

top level package sub package LocalDateTime class


Sample Program
// A program to display the current date
import java.time.LocalDate; // import the LocalDate class
public class DisplayDate {

public static void main(String[] args) {


LocalDate date = LocalDate.now();
System.out.println("Current Date is : " + date);
}
}
4. Interface statements
• An interface is a reference type in Java. It is similar to class. It is a
collection of static constants and abstract methods.
• Interfaces are used to achieve abstraction in Java.
• The interface keyword is used to define an interface.

interface InterfaceName { // Declaring an interface


variables;
methods;

}
5. Class definitions
• A Java program can contain one or more classes.
• Classes are used to define real-world objects in the program
domain.
• The class keyword is used to define a class.

class Student { // Declaring the Student class


variables;
methods;

}
6. main-method class
• Every Java program essentially requires a main method to start
with.
• The main() method is the entry point into the application for the
JVM.
• After the program finishes the control is passed back to the
Operating system.
• The syntax of the main() method is;
Signature
public static void main(String[] args) of the
main method
Java Standard Output
• System.out.print() is a Java statement that prints the argument
passed, into it.
public class DisplayMessage {

public static void main(String[] args) {


System.out.println("Welcome to : ");
System.out.print("Java ");
System.out.println(“Programming.");
}
}
Escape Sequences in Java
• Escape sequence characters are used to perform some specific
task.
• A backslash (\) is used just before the escape sequence character.
Escape Sequence Description
\t Inserts a tab in the text at this point.
\b Inserts a backspace in the text at this point.
\n Inserts a newline in the text at this point.
\r Inserts a carriage return in the text at this point.
\f Inserts a form feed in the text at this point.
\' Inserts a single quote character in the text at this point.
\" Inserts a double quote character in the text at this point.
\\ Inserts a backslash character in the text at this point.
Sample Program
public class Welcome {

public static void main(String[] args) {


System.out.print("Welcome to \'UWU\'. My University is; \n");
System.out.print("located in \t: \"Badulla\". \n");
System.out.print("started in \t: 2008\b5. \n");
System.out.print("has about \\3000\\ students. \n");

}
}
Tokens in Java
• Smallest individual building block or smallest unit of a Java
program is called as a token.
• There are five different tokens in Java;
• Keywords
• Identifiers
• Literals
• Operators
• Separators
Keywords
• Java keywords are the predefined and reserved words contained
in Java programming language that can be used to develop a Java
program.
• They are used for some internal processes or represent some
predefined actions.
• They have some special meaning for the compiler when they are
interpreted.
• So they can not be used as a variable, class or a method name.
Identifiers

• The names given to classes, variables, methods, objects,


packages, labels and interfaces in a program are called identifiers.

Rules for defining an identifier name


• Each identifier must have at least one character.
• The first character can be : an alphabetic character (a-z, A-Z),
an underscore(_), or a dollar sign($).
• The first character can not be a digit.
$total, first_name, contactDetails, _subject1
• An identifier must be descriptive, easy to understand, meaningful
according to the problem domain and must be short enough for
typing it quickly.

Naming Conventions in Java


• Each variable or method name should start with a leading lowercase
letter.
• If the name is combined with two words, the second word should
start with an uppercase letter. (Camel case)
• Each class or interface name should start with a leading uppercase
letter. HelloWorld, StudentDetails
• Each constant variable names use all uppercase letters and
underscores between words. MAX_TEMP, TOTAL_AREA
Literals

• A literal is a source code representation of a fixed value.


• Literals can be a number, text, or anything that represent a value.
• There are
• Integer 12, 100, 43, 745
• Floating Point 2.76, 36.1453, 567.8
• Character 'A', 'c', 'N', 'd'
• String "Hello", "UWU", "Java Programming"
• Boolean true, false
Operators

• Operators are special symbols (characters) that are used to


perform operations on operands.
• There are different types of operators in Java;

1. Arithmetic 5. Increment/Decrement
2. Relational 6. Conditional
3. Logical 7. Bitwise
4. Assignment 8. Special
Separators

• A separator is a symbol that is used to separate a group of code


from one another.
• They are useful for organizing and arranging the code properly.
Parenthesis ()
braces { }
brackets [ ]
Semicolon ;
Comma ,
Period .
Primitive Data types of Java
Data Type Default Value Default size
• There are 8 different boolean false 1 bit
data types in Java.
char '\u0000' 2 byte
• A data type specifies
the type of data byte 0 1 byte
represented by a short 0 2 byte
variable. int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

You might also like