OOP - Chapter 2
OOP - Chapter 2
Input/output in java
Static: allows main ( ) to be called without having to instantiate a particular instance of the class.
void simply tells the compiler that main ( ) does not return a value
main ( ) is the method called when a Java application begins. Keep in mind that Java is case-
sensitive. Thus, Main is different from main.
String args[ ] declares a parameter named args, which is an array of instances of the class String.
Objects of type String store character strings. In this case, args receives any command-line arguments
present when the program is executed.
System.out.println This line outputs the string “Hellow worled.” followed by a new line on the screen.
Output is actually accomplished by the built-in println( ) method. In this case, println( ) displays the
string which is passed to it.
We can use System.out.print(“Hellow worled”);
They are software applications that provide a comprehensive set of tools for developers to
write, edit, debug, test, and deploy code more efficiently.
Here are the most widely used IDE’s for java programming development
Strings. Strings are used for storing text. A String variable contains a collection
of characters surrounded by double quotes:
Boolean: This group includes boolean, which is a special type for representing
true/false values.
Java programs are a collection of identifiers, comments, literals, operators, and keywords etc.
1. Identifiers: they are used for class names, method names, and variable names.
They can be any descriptive sequence of uppercase and lowercase letters, numbers, or the
underscore and dollar-sign characters.
Value and value in java are different , some types of valid identifiers are :- Sum, X1, Y_Z,
here are some invalid identifiers 2x, high-temp, Not/Ok
2. Literals: A constant value in Java is created by using a literal representation of it. 100, 3.96f,
”A+”,”yes” .
3. Comments: They are never executed during program execution, They can be single line,
multiline comments , they are used to offer certain information the reader of the program. //,
/* here are multiple lines */
4. Java Keywords: 49 reserved keywords currently defined in the Java language. These
keywords, combined with the syntax of the operators and separators, form the definition of the
Java language. These keywords cannot be used as names for a variable, class, or method.
The main thing which differentiate java variable declaration from c++ variable
declaration is the addition of access modifier before data type.
Syntax : Access_Modifier data_type Variable Name
Example
public class Addition {
public int a=34; //here I am declaring variable a and assigning
value
public int b=45;// declaring variable b and assigning value
public static void main(String[] args) {
// we can declare here but no need of adding access modifier
here
int x=67;
int y=89;
}}
3/5/2025
OOP Chapter 2 [by Haftu M. & Bekalu M.]
11
Example with arithmetic operators 3/5/2025
5. Conditional Operator: Java includes a special ternary (three-way) operator that can
replace certain types of if-then-else statements.
Import: it is a keyword that helps the compiler to locate a class that can be used in a
program.
Java has rich set of predefined class that we can reuse than “reinventing the wheel”
These classes are grouped to package , which is a collection of related classes, collectively
named java class libraries or java APIs(application programming interfaces).
All import declaration must appear before any class declaration.
Example import java.util.Scanner;
We are importing the class named “Scanner” from the package java.util. , It contains the
collections framework, legacy collection classes, event model, date and time facilities,
internationalization, and miscellaneous utility classes (a string tokenizer, a random-number
generator, and a bit array).
Example
A programming language uses control statements to cause the flow of execution to advance
and branch based on changes to the state of a program.
Java’s program control statements can be classified into the following categories:
selection, iteration, and jump.
Selection statements allows program to choose different paths of execution based upon the
outcome of an expression or the state of a variable.
Iteration statements enable program execution to repeat one or more statements (that is,
iteration statements form loops) until the condition is true.
Jump statements allows program to execute in a nonlinear fashion. All of Java’s control
statements are examined here.
Statement2;}
if(condition1)
Statement 1;
else if(condition 2)
Statement2;
else if(condtion3)
Statement3 ;
The if statements are executed from the top to down
a loop repeatedly executes the same set of instructions until a termination condition is met.
continue statement: A continue statement takes control to the beginning of the loop.
The continue statement breaks one iteration (in the loop), if a specified condition occurs,
Arrays offer a more streamlined way to store data than using individual data items for
each variable.
Arrays also allow us to work with their data more efficiently than with data stored in
individual variables.
Let’s take we do have 10 different values to be stored for further processing if we don’t plan
In an array, data are arranged in a linear or sequence which can be detected when the
program is compiled.
Like objects, they are instantiated with the new operator and they have instance variables
When arrays are used as parameters, a reference to the array is passed rather than a copy
The primary difference between arrays and full-fledged objects is that arrays aren’t
defined in terms of an Array class. Thus, arrays don’t fit into Java’s Object hierarchy. They
don’t inherit any properties from Object and they cannot be subclassed.
We can consider array as a container that contains a number of variables that can be
Each of the components of the array has the same type, which is called the array’s
A one-dimensional array has components that are called the array’s elements. Their type is
An array’s elements may be of any type, including primitive and reference types. This
means you can have arrays of int, char, boolean, String, Object,Image, TextField
object requires that we create both a name for the array and then the array
itself. Example
public class ArrayDeclaration {
public static void main(String[] args) {
int arr[];// am declare a name for array
arr=new int[4];// am creating the array itself
}}
3/5/2025 OOP Chapter2 [by Haftu M. & Bekalu M.] 33
Declaring and Creating Arrays
Array can be declared as follows
int arr1 [ ];//the brackets may follow the array's name
int [] array1;//the bracket may proceed the array's name
Sample array Example
public class ArrayDeclaration {
public static void main(String[] args) {
String Starray[]=new String[5];//String array declaration
for(int i=0;i<Starray.length;i++) {
Starray[i]=new String("Hellow"+i);//assigning array value
System.out.println("Array of:"+i+" "+Starray[i]);
}}}
Boolean elements are initialized to false, and integer and real types are initialized to 0.
Arrays can also be assigned initial values when they are created, although this is feasible only
for relatively small arrays. An array initializer is written as a list of expressions separated by
Example
3/5/2025 35
OOP Chapter2 [by Haftu M. & Bekalu M.]
Assigning and Using Array Values
Array elements can be used in the same way as other variables. The only difference, of course, is
For example, the following assignment statements assign values to the elements of two arrays,
3/5/2025 36
OOP Chapter2 [by Haftu M. & Bekalu M.]
Sample code snippet
package ethioTechnotube;
public class ArrayDeclaration {
public static void main(String[] args) {
int Evens[]= {0,2,4,6,8};
String Names[]= {"Abel","Bethelhem","Rahel"};//Assigning String values
System.out.println("first even:"+Evens[0]);
String Starray[]=new String[5];//String array declaration
for(int i=0;i<Starray.length;i++) {
Starray[i]=new String("Hellow"+i);//assigning array value
System.out.println("Array of:"+i+" "+Starray[i]);
}}}
Output
first even:0
Array of:0 Hellow0
Array of:1 Hellow1
Array of:2 Hellow2
Array of:3 Hellow3
Array of:4 Hellow4
3/5/2025 37
OOP Chapter2 [by Haftu M. & Bekalu M.]
Group Discussion
3/5/2025 38
OOP Chapter2 [by Haftu M. & Bekalu M.]
End of Chapter Two ,
Thank You for Your Attention!!
Do you have any question?????
Chapter 3: Classes objects and methods
[Coming Soon…]
End!! 39