PROGRAMMING TECHNIQUES JAVA
PROGRAMMING TECHNIQUES JAVA
Primitive data types: These include int, double, float, char, boolean, byte, short and long.
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
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
Output
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
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
byte b = 120;
short s = 30000;
int i = 100000;
long l = 9000000000L;
float f = 3.14f;
double d = 6.78d;
char c = 'a';
class MyClass {
// Class definition
MARIO MK CHONGO
4
Modulus (%): Returns the remainder when one value is divided by another.
// Addition
result = x + y;
// Subtraction
result = x - y;
MARIO MK CHONGO
5
// Multiplication
result = x * y;
// Division
result2 = a / b;
// Modulus
result = y % x;
// Increment
x++;
// Decrement
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;
MARIO MK CHONGO
7
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