You are on page 1of 16

‫أساسيات البرمجة‬

Chapter(2):
Primitive Data Types& Operations
Contents: ‫محتويات المحاضرة‬
 Overview of Creating, Compiling, and Executing
a Java Program

 The Rules for naming identifiers

 Declaring Variables in Java program

 Primitive Data Types in Java

 Assignment Statements and Expressions

 Examples
2.1. Creating Java Program
 You can use any text editor (ex: TextPad, NotePad) to
create and edit a Java source code file

 This file must save with the extension .java and must
have the exact same name as the public class name.
ex: Welcome.java - since the public class name is Welcome.

 The main method is the entry point where the


application program starts when it is executed.

 Java source programs are case-sensitive


Create Simple Java Program
1 Class Name 2 start class block
3 main method
4 start main block
Welcome.java
public class Welcome {
public static void main(String[ ] args) {

System.out.print("Welcome to Java ..");


}
}
6 end main block
5 Print Welcome message
7 end class block
2.2. Compiling and Executing a Java Program

 To compile a Java source code file,use javac command


as follow:
javac Welcome.java

If there are no syntax errors, the compiler generates a bytecode


file with “.class” extension.

 To run a Java class, use the java command as follow:

java Welcome
2.3. Identifiers
 Programming language use special symbols called
identifiers to name such programming entities as
variables, constants, methods, classes, and packages.
 Rules for naming identifiers are:
 An identifier is a sequence of characters that consists of
letters, digits, underscores (_), and dollar signs ($).
 must start with a letter, an underscore (_), or a dollar sign
($). It cannot start with a digit.
 An identifier cannot be a reserved word
 An identifier cannot be true, false or null
 An identifier can be with any length
2.3. Identifiers
 Examples of legal identifiers:

$2, ComputeArea, area, x1 , yz2

 Examples of illegal identifiers:

2A ,d+4, null, main, class, false


2.4. Declaring Variables ‫تعريف المتغيرات‬
 Variables are used to store data in program

 Declaring a variable tells the compiler to allocate


appropriate memory space for the variable based on
its data type.

 The syntax for declaring a variable is: ‫الصيغة العامة للتعريف‬

datatype variableName

‫نوع البيانات‬ ‫اسم المتغير‬


Primitive Data Types ‫تحديد نوع البيانات‬
 Java provides eight(8) primitive data types for
numeric values, characters, and boolean values.

 Every data type has a range of values.

 The compiler allocates memory space to store each


variable according to its data type.
Primitive Data Types ‫تحديد نوع البيانات‬
 Numeric Data Types
Name Range Storage size
Byte -27 (-128) to 27 – 1 (127) 8-bit signed
Short -215 (-32768) to 215 – 1 (32767) 16-bit signed

int -231 (-2147483648) to 231 - 1(2147483647) 32-bit signed

long -263 to 263 – 1 64-bit signed

(-9223372036854775808 to 9223372036854775807)
float Negative range: -3.4028235E + 38 to -1.4E-45 32-bit IEEE 754

Positive range: 1.4E-45 to 3.4028235E + 38


double Negative range: -1.7976931348623157E+308 to - 64-bit IEEE 754
4.9E-324

Positive range: 4.9E-324 to


1.7976931348623157E+308
Primitive Data Types ‫تحديد نوع البيانات‬
 example of variable declaration:
int x; // Declare x to be an integer variable;
double radius; // Declare radius to be a double variable;

char a; // Declare a to be a character variable;

‫ فى حالة وجود أكثر من متغير من نفس النوع تكون صيغة التعريف للمتغيرات‬
:‫كاآلتى‬
datatype variable1 , variable2 , ... , variablen;

‫نوع البيانات‬ 1‫اسم المتغير‬ 2‫اسم المتغير‬ n‫اسم المتغير‬

ex: int i , j , k; // Declare i, j, and k as int variables


2.5. Assignment Statement and Expressions
 variable must be declared before it can be assigned
a value.
 In Java, the equal sign (=) is used as the assignment
operator
 The syntax for assignment statements is as follows:
:‫الصيغة العامة‬
variableName = expression;

‫اسم المتغير‬ ‫القيمة التى يتم اسنادها للمتغير‬


ex: int x = 100
2.5. Assignment Statement and Expressions
 Declaring and Initializing Variables in One Step
:‫ لتعريف وتعيين قيم للمتغيرات التى تكون من نفس النوع‬

ex:
int i = 1, k = 50;

int x = y = z = 10;
2.5. Assignment Statement and Expressions
 Example(1):
:‫ماهو ناتج تنفيذ البرنامج اآلتى‬
guess.java
public class guess {
public static void main(String[ ] args) {

int num = 100 ;

System.out.print( num );
}
}

100 :‫ناتج تنفيذ البرنامج‬


2.5. Assignment Statement and Expressions
 Example(2):
:‫ماهو ناتج تنفيذ البرنامج اآلتى‬
guess2.java
public class guess2 {
public static void main(String[ ] args) {
int x = 20 ;
x = x + 10 ;
System.out.print( x );
}
}

30 :‫ناتج تنفيذ البرنامج‬


‫‪Home Work‬‬ ‫الواجب األول‬
‫‪ -1‬أكتب برنامج بطباعة الشكل اآلتى‪:‬‬

‫****‬
‫***‬
‫**‬
‫*‬
‫‪ُ -2‬معطى المتغيرين ‪int x=3 ، y=5‬‬
‫اكتب برنامج يقوم باآلتى‪:‬‬
‫(‪ )1‬تعريف المتغير ‪ Z‬من نوع ‪int‬‬
‫(‪ )2‬تخزين حاصل جمع العددين ‪ y ،x‬بالمتغير ‪Z‬‬
‫(‪ )3‬طباعة المتغير ‪Z‬‬

You might also like