Chapter-One (Introduction To JAVA)
Chapter-One (Introduction To JAVA)
Undergraduate Program
Course Code: SE132
Course Title: Object Oriented
Programming
Introduction to Java
Contents
1. Overview of Class
2. Variables & Literals
3. Data Types
4. Operators
5. Input & Output
6. Expressions & blocks
7. Conditional Statements
.
8. Looping Statements
9. Array
Overview of Class
class HelloWorld {
public static void main(string[] args) {
System.out.println(“Hello World”);
}
}
The class defines the blueprint from which objects can be created from
It follows camel name convention and must be the same with the file name
Every application in java must contain a main method, since execution starts from
main method
The print statement prints the text to standard output (to the screen)
Variables & Literals
A variable is a reserved memory (storage area) used for storing a value
E.g int speedLimit = 80;
A variable only store a value that matches its data type
Unlike constants a value in a variable can be changed
Variable naming rules
Variables are case sensitive
E.g. int age & int AGE are two different variables
Variables must start with either a letter or an underscore, or a dollar sign
A variable name can not start with numbers
Whitespace can’t be used
Use meaningful name when declaring a variable (recommended)
Keywords can’t be used as variable name
Cont…
Literals are data used for representing fixed values like 1, 2.5. ‘F’
Types of literals
Boolean Literals
Used to initialize Boolean data types (e.g. boolean flag1 = true)
Integer Literals
Is a numeric value without any fractional part
Floating-point Literals
Is a numeric literal that has a fractional part
Character Literals
Is a character enclosed inside single quotes
String literals
Is a sequence of characters enclosed inside double-quotes
Data Types
Data types specify the type of data that can be stored inside a variable
List of primitive data types
Boolean
The default value is false
Uses 1 bit to represent true or false
Byte
Default value is 0
Uses 8 bits to represent the numbers between -128 to 127
Short
Default value is 0
Uses 2 bytes to represent a number
Cont…
Int
Default value is 0
Uses 4 bytes to represent a number
Long
Default value is 0
Uses 8 bytes to represent a number
Double
Default value is 0.0d
Is a double-precision 64-bit floating-point
Float
Default value is 0.0f
Is a single-precision 32-bit floating-point
Cont…
Char
It is a 16-bit Unicode character
The default value is ‘\u0000’
String (not a primitive data type)
It represents array of character and found in java.lang.String
Operators
Arithmetic operators (+, -, *, /, %)
Relational operators (==, !=, >, <, >=, <=)
Logical operators (&&, ||, !)
Instanceof
Ternary operator( expression ? expression1 : expression2
Conditional Statements
If (condition){
statement(s)
} else if (condition) {
statement(s)
} else {
statement(s)
}
Switch(expression) {
Case value1:
code(s)
break;
default:
code(s)
}
Exercise
1. write a program that checks if a number is odd or even
2. Write a program that takes a number between 1 and 7 and displays the name of the
weekday
3. Take three numbers from the user and print the greatest number
4. Write a Java program to find the number of days in a month
5. Write a Java program that takes a single character from a user and prints Vowel or
Consonant, depending on the user input. If the user input is not a letter (between a
and z or A and Z), or is a string of length > 1, print an error message.
6. Write a Java program that takes a year from a user and prints whether that year is a
leap year or not.
Looping Statements
For loop
For (initialExpression; testExpression; updateExpression) {
statement(s)
}
Looping Statements
while loop
while (testExpression) {
statement(s)
}
Do…While
do…while loop
do {
statement(s)
} while(testExpression);
Exercise
1. Write a java program that counts the number of digits in a number
2. print the following shapes (rectangle, triangle and diamond) to
the screen
3. Write a program that calculates factorial of a given number
4. Write a program that takes a number and checks if the number is a
prime number or not
Array
An array is a variable that can store multiple values of same type
Use the following syntax to declare an array
• DataType[] name = new DataType[size];
An array can also be initialized during declaration
• DataType[] name = {val1, …, valn};
To access element of an array use the name of the array with its index
Exercise
1. Create an array of number with a size of 10. Then write a program that
sorts the array in a ascending order. And also write a program that
searches a number from the array.
2. Using the array created in the previous question, write a program that
calculates average of the numbers in the array