You are on page 1of 12

ICT 143-2 Data Structures

and Algorithms

Practical 2 – Java Programming(Continue)


main() method
• In Java, every application must contain a main() method, which is the
entry point for the application.

• ▪All other methods are invoked from the main() method.

• ▪The signature of the method is public static void main(String[] args) { }.


It accepts a single argument: an array of elements of type String.
Java Identifiers

How many Identifiers can you see?


Java Modifiers
Java Variables

• Local Variables: Variables defined inside methods, constructors or blocks


are called local variables. The variable will be declared and initialized
within the method and the variable will be destroyed when the method
has completed.
• •Class Variables (Static Variables): Class variables are variables declared
within a class, outside any method, with the static keyword.
• •Instance Variables (Non-static Variables): Instance variables are variables
within a class but outside any method. These variables are initialized when
the class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
Example
Data Types
Primitive Data Types:
◦ byte
◦ short
◦ int
◦ long
◦ float
◦ double
◦ boolean
◦ char
Reference/ Object Data Types:
◦ A reference variable can be used to refer any
object of the declared type or any compatible
type.
Example: Animal animal = new Animal("giraffe");
Object and Class
Method
modifier - It defines the access type of the method, and it is
optional to use.

•returnType - Method may return a value.

•nameOfMethod - This is the method name. The method


signature consists of the method name and the parameter
list.

•Parameter List - The list of parameters, it is the type, order,


and number of parameters of a method. These are optional,
method may contain zero parameters Syntax
.
•method body - The method body defines what the method
does with the statements
Method call
• For using a method, it should be called
• There are two ways in which a method is called
◦ method returns a value
◦ returning nothing (no return value)

You might also like