1
PROGRAMMING TECHNIQUES (JAVA)
Types of variables in Java
Primitive data types: These include int, double, float, char, boolean, byte, short and long.
Object types: These variables reference an object in memory.
Local variables: These variables are declared within a method or within a block of code.
Instance variables: These variables are declared in a class but outside of any methods or blocks
of code, and are accessible to all methods within the class.
Static variables: These variables are declared with the static keyword and are associated with a
class rather than an instance of a class.
Final variables: These variables are declared with the final keyword and cannot be changed
once they have been assigned a value.
CODE ON VARIABLES
public class Example {
public static void main(String[] args) {
int a = 10; // declaring and initializing an integer variable called 'a' with value 10
double b = 3.14; // declaring and initializing a double variable called 'b' with value 3.14
char c = 'X'; // declaring and initializing a character variable called 'c' with value 'X'
MARIO MK CHONGO
2
boolean d = true; // declaring and initializing a boolean variable called 'd' with value true
System.out.println("The value of variable a is: " + a);
System.out.println("The value of variable b is: " + b);
System.out.println("The value of variable c is: " + c);
System.out.println("The value of variable d is: " + d);
Output
The value of variable a is: 10
The value of variable b is: 3.14
The value of variable c is: X
The value of variable d is: true
Java has two types of data types:
Primitive Data Types:
These data types are predefined, which means they are built into the language. They represent
simple values like integers, floating-point numbers, characters, and boolean values. There are
eight primitive data types:
byte: short: int: long: float: double char: boolean: true or false
Non-Primitive Data Types (also called reference types):
These data types are not predefined. They are created using defined constructors of classes or
interfaces. String, Arrays, and Classes come under this category of data types.
MARIO MK CHONGO
3
public class DataTypesExample {
public static void main(String[] args) {
// Primitive data types
byte b = 120;
short s = 30000;
int i = 100000;
long l = 9000000000L;
float f = 3.14f;
double d = 6.78d;
char c = 'a';
boolean bool = true;
// Non-Primitive Data Type
String str = "Hello, world!";
int[] arr = {1, 2, 3, 4, 5};
MyClass obj = new MyClass();
class MyClass {
// Class definition
MARIO MK CHONGO
4
These basic operations are called arithmetic operators
Addition (+): Adds two values or variables together.
Subtraction (-): Subtracts one value from another.
Multiplication (*): Multiplies two values or variables together.
Division (/): Divides one value by another.
Modulus (%): Returns the remainder when one value is divided by another.
Increment (++): Increases the value of a variable by one.
Decrement (--): Decreases the value of a variable by one.
public class BasicOperationsExample {
public static void main(String[] args) {
int x = 10, y = 20, result;
double a = 30.0, b = 4.0, result2;
// Addition
result = x + y;
System.out.println("x + y = " + result);
// Subtraction
result = x - y;
MARIO MK CHONGO
5
System.out.println("x - y = " + result);
// Multiplication
result = x * y;
System.out.println("x * y = " + result);
// Division
result2 = a / b;
System.out.println("a / b = " + result2);
// Modulus
result = y % x;
System.out.println("y % x = " + result);
// Increment
x++;
System.out.println("x++ = " + x);
// Decrement
y--;
System.out.println("y-- = " + y);
MARIO MK CHONGO
6
Output
x + y = 30
x - y = -10
x * y = 200
a / b = 7.5
y%x=0
x++ = 11
y-- = 19
SCANNER INPUT
In Java, the Scanner class is used to read user input from different sources, such as
the console, files, or network connections.
When you create a Scanner object, you can call its various methods to read different
types of data, such as next(), nextInt(), nextDouble(), etc. The specific method you use
depends on the type of data you are expecting to read.
For example, next() reads the next token of input (a word), nextInt() reads the next
integer value, and nextDouble() reads the next double value.
SOURCE CODE
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MARIO MK CHONGO
7
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + "! You are " + age + " years old.");
scanner.close();
}
}
The above code creates a Scanner object to read input from the console. It prompts the user to
enter their name and age, reads the input using the nextLine() and nextInt() methods of the
Scanner object respectively, and stores them in the name and age variables. Finally, it outputs a
message to the console that includes the user's name and age.
Code that accepts two integers and multiply them and gives the answer.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in)
System.out.print("Enter the first integer: ");
int num1 = input.nextInt();
System.out.print("Enter the second integer: ");
int num2 = input.nextInt();
int result = num1 * num2;
System.out.println("The result of multiplication is: " + result);
}
}
MARIO MK CHONGO
8
In this program, we first import the java.util.Scanner class to allow user input. Then, we
create a new Scanner object called input. We prompt the user to enter two integers, and then
store the user's inputs in the variables num1 and num2. We then multiply num1 and num2
and store the result in a new variable called result. Finally, we print out a message to the user
displaying the result of multiplication.
MARIO MK CHONGO