DCIT 22 Computer Programming 1 Lecture 4
DCIT 22 Computer Programming 1 Lecture 4
DCIT 22 –
COMPUTER
PROGRAMMING 1
JAVA BASIC
When we consider a Java program, it can be defined as a collection of objects that
communicate via invoking each other's methods. Let us now briefly look into what do class, object,
methods, and instance variables mean.
• Object − An object is a real-world entity that can be identified distinctly. For example, a desk, a
circle can be considered as objects. An object has a unique behavior, identity, and state. Data
fields with their current values represent the state of an object (also known as its properties or
attributes).
• Class A class is a template or blueprint or prototype that defines data members and methods of
an object. An object is the instance of the class. We can define a class by using the class
keyword.
JAVA BASIC
• Methods − A method is basically a behavior. A class can contain many methods. It is in
methods where the logics are written, data is manipulated and all the actions are executed.
• Instance Variables − Each object has its unique set of instance variables. An object's state is
created by the values assigned to these instance variables.
FIRST JAVA PROGRAM
We will learn how to write the simple program of java.
Creating Hello World Example
• Class Names − For all class names the first letter should be in Upper
Case. If several words are used to form a name of the class, each inner
word's first letter should be in Upper Case.
Example: class MyFirstJavaClass
BASIC SYNTAX CONT…
• Method Names − All method names should start with a Lower Case
letter. If several words are used to form the name of the method, then
each inner word's first letter should be in Upper Case.
When saving the file, you should save it using the class name (Remember Java is case
sensitive) and append '.java' to the end of the name (if the file name and the class name
do not match, your program will not compile).
But please make a note that in case you do not have a public class present in the file
then file name can be different than class name. It is also not mandatory to have a
public class in the file.
Example: Assume 'MyFirstJavaProgram' is the class name. Then the file should be
saved as 'MyFirstJavaProgram.java'
BASIC SYNTAX CONT…
• public static void main(String args[]) − Java program processing
starts from the main() method which is a mandatory part of every Java
program.
JAVA IDENTIFIERS
All Java components require names. Names used for classes,
variables, and methods are called identifiers.
In Java, there are several points to remember about identifiers.
They are as follows −
• Local Variables – This is a variable that declared inside the body of a method.
• Instance Variables – This Java variable is defined without the STATIC keyword, but as
outside of a method declaration. They are object-specific variables, which is why they are
known by this name.
• Static Variables – This variable is initialized only once, just when the program execution
starts. It is the variable that should be initialized first, especially before an instance variable is
initialized.
JAVA VARIABLES EXAMPLE
public class A
{
static int m = 100; //static variable
void method()
{
int n = 90; //local variable
}
public static void main(String args[])
{
int data = 50; //instance variable
}
}//end of class
JAVA KEYWORDS
abstract assert boolean break
-9,223,372,036,854,775,808 to
long 64 bits
• There are four data types 9,223,372,036,854,775,807
you can use to store integers which one
you use depends on the size of the
integers.
VARIABLES CONT…
Another types of number that can be stored is a floating-point number, which has the
type float or double. The float type should be sufficient for most uses because it can handle
number from 1.4E-45 to 3.4E+38. If not, the double type can be used for more precise numbers
ranging from 4.9E-324 to 1.7E+308
The char type is used for individual characters, such as letters, numbers, punctuation,
and other symbols.
The last of the eight primitive data types is boolean. This data type holds either true or
false in Java.
BASIC OPERATORS
1. The Arithmetic Operators
ARITHMETI + (Addition) Adds values on either side of the operator. A + B will give 30
C - (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -10
OPERATORS * (Multiplication) Multiplies values on either side of the operator. A * B will give 200
Checks if the values of two operands are equal or not, if yes then condition
== (equal to) (A == B) is not true.
becomes true.
Checks if the value of left operand is greater than or equal to the value of
>= (greater than or equal to) (A >= B) is not true.
right operand, if yes then condition becomes true.
Checks if the value of left operand is less than or equal to the value of right
<= (less than or equal to) (A <= B) is true
operand, if yes then condition becomes true.
BASIC OPERATORS CONT…
2. The Logical Operators
ASSIGNMEN
= C = A + B will assign value of A + B into C
to left side operand.
T
Add AND assignment operator. It adds right operand to the left
+= C += A is equivalent to C = C + A
operand and assign the result to left operand.
OPERATORS -=
Subtract AND assignment operator. It subtracts right operand from
the left operand and assign the result to left operand.
C -= A is equivalent to C = C – A
CONT…
Multiply AND assignment operator. It multiplies right operand with
*= C *= A is equivalent to C = C * A
the left operand and assign the result to left operand.
You can use these conditions to perform different actions for different decisions.
JAVA CONDITIONS AND IF STATEMENTS
CONT…
Java has the following conditional statements:
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
THE IF STATEMENT CONT…
In the example below, we test two values to find out if 20 is greater than
18. If the condition is true, print some text:
Example:
Example:
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
THE ELSE STATEMENT
Use the else statement to specify a block of code to be executed if the
condition is false.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
THE ELSE STATEMENT CONT…
Example:
In the example above, time (20) is greater than 18, so the condition is
false. Because of this, we move on to the else condition and print to the
screen "Good evening". If the time was less than 18, the program would
print "Good day".
THE ELSE IF STATEMENT
Use the else if statement to specify a new condition if the first condition
is false.
Syntax:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
THE ELSE IF STATEMENT CONT…
Example:
In the example above, time (22) is greater than 10, so the first condition
is false. The next condition, in the else if statement, is also false, so we
move on to the else condition since condition1 and condition2 is both
false - and print to the screen "Good evening".
However, if the time was 14, our program would print "Good day."
THANK YOU!
Fonts used: Roboto Mono and Gugi