Any java program can contain any of the following component in their program structure
- Java statements
- Expressions
- Variables
- Data types
- Comments
- Literals
- Operators
Java statements
Statement forms a single Java operation. The following are simple Java statements:
int age= 23;
import java.util.Scanner;
System.out.println(” My name is” + name);
m.engineState = true;
Each statement ends with a semicolon. Forget the semicolon and your Java program won’t compile.
Expressions
Exressions are a combination of operators and variables that returns a value.
They are the simplest form of statement in Java that accomplishes something.
We are going to cover in depth different types of expressions later on but the following are few examples of expressions.
int a +int b = c;
length*width=area; // length, width and area are variables which have to be declared and initialized then can be used as above expression in order to compute it.
Variables
A variable is a named storage area for programs.
It can also be defined as locations in memory in which values can be stored.
A variable is composed of the following a name, a type, and a value.
Before using the variables they must be declared first.After it is declared, you can then assign values to it.
Types of variables
There are three types of variables in java:
- local variable
- instance variable
- static variable
- Local Variable
These are variables which are declared inside the method are known as local variable.
public class Local{
public static void main (String[]args){
int a; // variable declaration (local variable)
int b;// variable declaration (local variable)
a+b; //expression
}
}
2. Instance Variable
These are variables which are declared inside the class but outside the method, are known as instance variable. It is not declared as static.
public class Instance{
int a; // variable declaration (instance variable)
int b;// variable declaration (instance variable)
public static void main (String[]args){
a+b; //expression
}
}
3. Static variable
These are variables that are declared as static are known as static variable. They are declared with the keyword static, They cannot be local.
public class Static{
static int a; // variable declaration (static variable)
static int b;// variable declaration (static variable)
public static void main (String[]args){
a+b; //expression
}
}
Lets now see how to declare and initialize variables
Variable Declaration
In order to use any variable in a Java program, you must first declare it. Variable declarations consist of a type and a variable name:
The following is the syntax of variable declaration
data_type variable_name;
Example
int age;
char name;
string password;
Several variables of the same data type can be declared as a single statement and the names of the variables should be separated by a comma (,)
For example
string name,gender,hobby;
Variable initialization
This is assigning values to the declared variables. Assigning of values can be done by using an assignment operator (=)
Example
age=45;
name = hamisi;
NB
Variable declaration and initialization can also be written as one statement to perform both actions. Below is an example to demonstrate that.
int age = 45;
string name = hamisi;
Practical Example
Write a java program that will aid to calculate area of a circle of radius 10.
public class ComputeArea {
public static void main(String[] args) {
double radius; // Declare radius
double area; // Declare area
// Assign a radius
radius = 10; // radius is initialized to 20
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println(“The area for the circle of radius “ +
radius + ” is “ + area);
}
}
NB
The + sign used in the system.out.println() is called a string concatenation operator in java the + has two functions.
- Addition of values.
- Joining of string.
THEORETICAL EXERCISE
- Explain the following as used in java programs
i. Statements
ii. Expressions
iii. Variables
2. Using examples of a java program State and explain the THREE types of variables used in java.
3. In what cases would you declare a variable and initialize them? use examples to demonstrate that.
PRACTICAL EXETCISE
- Write a java program that will aid in calculating area of a parallelogram with the following expression
A=B*H
where B is 30, and H is 57.89
2. Write a java program that will aid in calculating area of a triangle with the following expression
A=0.5*B*H
The values of B is 34.87 and H is 24.78
