02 OOP_Lecture#2
02 OOP_Lecture#2
1. What is Java?
Java is a programming language originally developed by James Gosling at
Sun Microsystems and released in 1995.
Java applications are typically compiled to bytecode (not the machine
language) that can run on any Java Virtual Machine (JVM) regardless of
the computer architecture.
Java is a general-purpose, concurrent, class-based, object-oriented
language.
It is intended to let application developers "write once, run anywhere".
Classes are the building blocks of Java programs.
10 من1 الصفحة
10 من2 الصفحة
10 من2 الصفحة
10 من3 الصفحة
Java classes
In Java, all code is grouped into classes. A class is thus a code container.
The definition of the class starts with an access modifier (public in this
case), which specifies which classes have access to it (you will learn about
this later more extensively). This is followed by the keyword class and the
name of the class (BMICalculator).
Every class definition is enclosed within brackets { }.
It has both variables (weight and height) and methods (BMICalculator,
calculateBMI and main).
The BMICalculator method is of a special type, known as a constructor,
which is invoked to create a new object. A constructor can perform any
action, but constructors are designed to perform initializing actions, such
as initializing the data fields of objects.
The main method is a special method since it is the entry point of
program execution. In other words, when the class BMICalculator is run,
it will start by executing the main method. Note that not every Java class
should have a main method.
Identifiers
An identifier is a name of a language element. This can be a class,
variable, or method.
In the BMI example, the following are identifiers: BMICalculator,
weight, height, main, and calculateBMI.
Use these naming conventions when defining identifiers:
10 من3 الصفحة
10 من4 الصفحة
Variables
As stated earlier, every class definition consists of both variables and
methods.
A variable is a name for a memory location that stores a specific value.
This value may change during program execution, which is why it’s called
a “variable.” The BMICalculator class starts by defining the following
variables:
The weight and height variables are defined using the data type double,
which represents a floating point number. Other data types exist in Java
10 من4 الصفحة
10 من5 الصفحة
Java methods
In many languages (like C and C++), the term function is used to describe
a named subroutine.
The term that is more commonly used in Java is method, as in “a way to
do something.”
Methods can be used to define reusable code and organize and simplify
coding.
They are used to define the behavior aspect of Java classes.
Methods in Java determine the messages an object can receive.
A method definition consists of its method name, parameters, return
value type, and body. The syntax for defining a method is as follows:
The method header specifies the modifiers, return value type, method
name, and parameters of the method.
A method may return a value. The returnValueType is the data type of
the value the method returns.
10 من5 الصفحة
10 من6 الصفحة
The return keyword is used to return a value whose type is the same as
The returnValueType.
Some methods perform desired operations without returning a value. In
this case, the returnValueType is the keyword void. For example, the
returnValueType is void in the main method.
When the return type is void, then the return keyword is used only to exit
the method.
The variables defined in the method header are known as formal
parameters or simply parameters.
A parameter is like a placeholder: when a method is invoked, you pass a
value to the parameter. This value is referred to as an actual parameter or
argument.
The method name and the parameter list together constitute the
method signature.
Parameters are optional; that is, a method may contain no parameters.
For example, the calculateBMI method in the BMICalculator class has
no parameters.
The method body contains a collection of statements that implement the
method.
The method body of the calculateBMI method contains one return
statement which returns a value produced by evaluating the expression:
return weight / (height*height);
Methods Overloading
Method overloading enables you to define the methods with the same
name as long as their signatures are different.
10 من6 الصفحة
10 من7 الصفحة
The following is a Java program that creates three methods. The first
finds the maximum integer, the second finds the maximum double,
and the third finds the maximum among three double values. All
three methods are named max.
10 من7 الصفحة
10 من8 الصفحة
Both max (int, double) and max (double, int) are possible candidates to
match max (1, 2). Because neither is better than the other, the invocation is
ambiguous, resulting in a compile error.
Comments
Comments are needed to improve code readability and facilitate future
maintenance operations.
They are not executed when the Java program runs.
One way of including comments is as follows:
// This is our main method.
Using //, you create a line comment that runs until the end of the line.
10 من8 الصفحة
10 من9 الصفحة
Block comments span multiple lines and can be defined using the
delimiters /* ... */, as follows:
/* Here, we call the method calculateBMI
which will calculate the BMI */
4. Java Naming Conventions
Various Java communities have introduced naming conventions for
identifiers.
These are not strictly enforced, so your code will compile successfully
even if you don’t comply. However, you will improve the readability and
future maintenance of your Java programs if you follow these
conventions.
A very popular naming convention originally suggested by Sun
Microsystems is explained in the following table:
Table1. Java Naming Convention
10 من9 الصفحة
10 من10 الصفحة
The data type of a variable specifies the kind of values it can be assigned.
For example, the weight, and height are declared as double variables, so
they can only be assigned floating point numbers from a specific range.
Primitive Data Types
Java supports eight built-in primitive data types.
Table 2 defines each of these and specifies the range and default value.
If the user has not initialized a variable, the compiler will automatically
assign the default value.
Note that the ranges and default values are uniform and do not depend
on the underlying machine architecture on which the Java program runs.
Table2. Primitive Data Types
10 من10 الصفحة