0% found this document useful (0 votes)
3 views

02 OOP_Lecture#2

Uploaded by

Abdunnaser Diaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

02 OOP_Lecture#2

Uploaded by

Abdunnaser Diaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

10 ‫ من‬1 ‫الصفحة‬

Introduction to Java Programming Language


In Java, all code is grouped into classes.

To implement and illustrate the importance of OOP concepts, an OO


programming language is needed. In this course, Java was chosen to serve
this purpose.
In this lecture, the following topics are covered:
 What is Java?
 What is Java used for?
 Java Program Structure
 Java Naming Conventions
 Java Primitive Data Types

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 ‫الصفحة‬

2. What is Java used for?


Here are some important Java applications:
 It is used for developing desktop applications
 It is used for developing Android Apps
 It is used for developing Enterprise applications
 It is used for Programming of Hardware devices
 It is used for Server-Side Technologies like Apache, JBoss, GlassFish, etc.

3. Java Program Structure


This section provides a brief overview of the Java program structure. Many
of the concepts addressed will then be further explained in subsequent
lessons. Let’s consider the following running example of a Java program
(BMICalculator) calculating BMI (Body Mass Index) to illustrate the
discussion.

10 ‫ من‬2 ‫الصفحة‬
10 ‫ من‬3 ‫الصفحة‬

Below is a discussion of the important components presented in the above


Java program (BMICalculator):

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 ‫الصفحة‬

o In theory, an identifier can have an unlimited length, although


practically it needs to be less than 64k of Unicode characters
and digits,
o It cannot begin with a digit.
o Although technically it is possible to start an identifier with a
dollar sign ($) or punctuation character (such as _), it is highly
discouraged since it will decrease the readability of the code.
o An identifier cannot be equal to a reserved keyword, null literal,
or boolean literal.
 Just like C, Java is case sensitive; weight, Weight and WEIGHT are all
different according to Java.
 When creating identifiers, make sure to use full words instead of
abbreviations as much as possible, unless the abbreviations can be
easily interpreted.

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 ‫الصفحة‬

and will be covered in a subsequent section. Here, this kind of variables


is called instance variables.

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:

Let’s look at a method defined in The BMICalculator class to calculate the


BMI. This method, named calculateBMI, has no parameters and returns the
BMI value which is of a double type. The modifier here is public which
makes the method accessible by other classes without any restriction.

 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.

 Overloading methods can make programs clearer and more readable.


Methods that perform the same function with different types of
parameters should be given the same name.

10 ‫ من‬7 ‫الصفحة‬
10 ‫ من‬8 ‫الصفحة‬

 Overloaded methods must have different parameter lists. You cannot


overload methods based on different modifiers or return types.
 Sometimes there are two or more possible matches for the invocation
of a method, but the compiler cannot determine the best match. This
is referred to as ambiguous invocation. Ambiguous invocation
causes a compile error. Consider the following code:

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

5. Java Data Types


 Java is a strongly typed language. This means that every variable should
first be declared before it can be used.

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 ‫الصفحة‬

You might also like