Professional Documents
Culture Documents
Lecture 03
Lecture 03
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!”);
}
}
• 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.
Comments
import java.time.LocalDateTime;
}
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.
}
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 {
}
}
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
1. Arithmetic 5. Increment/Decrement
2. Relational 6. Conditional
3. Logical 7. Bitwise
4. Assignment 8. Special
Separators