KEMBAR78
Programming Languages Lec 3 | PDF | Java (Programming Language) | Java Virtual Machine
0% found this document useful (0 votes)
6 views30 pages

Programming Languages Lec 3

Uploaded by

omarahmady754
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views30 pages

Programming Languages Lec 3

Uploaded by

omarahmady754
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Spring Semester

2024-2025

CS112
Programming Languages Lecture 3
Mahmoud Bassiouni, PhD
mbassiouni@eelu.edu.eg
Table of Contents
• History of Java Programming Language
• Java Compilation Process
• Integrated Development Environment (IDEs)
• Java Program Components
• Escape Sequences
• Primitive Datatypes
• Variables Declaration and Initialization
• Rules for naming Variables
• Inputs from User
• Errors and Comments
2
History of Java Programming Language
• It was a project in 1991 by Sun Microsystems and it was named “Oak”.
• In, 1995 it was renamed “Java”, and in 1996, the first version of “java 1.0” was
released.
• Java became open source in 2006.
• Sun was bought by Oracle in 2010 which made Java programming language belong
to Oracle now.
• In, 2025 the last version of java released was “Java 23.0”.

3
Java Compilation Process
• Before starting learning any programming language, you should know how the
source code of this language is converted to machine language and how it is
executed.
• Each file in the computer has an extension, the notepad file has an extension (.txt),
the word and the power point files have an extension (.doc or docx) (.ppt or .pptx)
respectively. Are the programming languages written in file? What are their
extensions?
• Yes, they are written in files. These files have extensions. In other words, a
programing language like C is written in a file with extension (.c), a programming
language like C++ is written in a file with extension (.cpp), but what for java?
• Java is written in files with an extension (.java). This file is called the source program.

4
Java Compilation Process
• The source code of the java program is written in
a file with extension (.Java) let’s consider it
(MYClass.java).
• The (MYClass.Java) is compiled using a compiler
called (Javac) or (Java development kit (JKD)).
• This complier performs two main steps. The first
step is to check the syntax of the java source
code. The second step to convert the source
code into a byte code.
• The byte code is written in a file called
(MYClass.class). Is the byte code understandable
by the computer ? NO

5
Java Compilation Process
• The bytecode is in the form of hexadecimal
format and it consists of some opcodes and
operands.
• The bytecode file (MYClass.class) is input to an
interpreter called java virtual machine (JVM) or
java real time environment (JRE).
• This interpreter performs two main steps. The
first step is to convert from the byte code to
machine language. The second step executes
the machine language to produce a program
that can stored on the memory and executed by
the processor.

6
Java Program Life Cycle

7
Portability
• Write Once, Run Anywhere (WORA)
• Byte code runs on any computer that has a
Java Virtual Machine

8
Java Development Kit (JDK)
• The software that you use to create Java programs is referred to as the JDK (Java
Development Kit)
http://www.oracle.com/technetwork/java/javase/downloads/index.html

9
Integrated Development Environment (IDEs)
• IntelliJ
• Visual Studio Code
• Jdeveloper
• BlueJ
• DrJava
• Eclipse
• JCreator
• Net Beans www.netbeans.org

10
Java Program Components
• Program
–Package(s)
• Class(es)
–Method(s)
»Statements ;

11
Hello Word Program
package helloworld;

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

}
Java
}
12
Package
package helloworld;

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

}
}
13
Class
package helloworld;

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

}
}
14
Method
package helloworld;

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

}
}
15
Statement
package helloworld;

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");

}
}
16
Escape Sequences
• Some symbols are used to format the printed output in java

\n newline Advances the cursor to the next line for subsequent printing

\t tab Causes the cursor to skip over to the next tab stop

\b backspace Causes the cursor to back up, or move left, one position

Point the screen cursor at the beginning of the current line any
\r Carriage return characters after the carriage return overwrite the characters previously
output on that line

\\ backslash Causes a backslash to be printed

\” double quote Causes a double quotation mark to be printed

17
Escape Sequences (Examples)
• Some symbols are used to format the printed output in java
Example Code Output
System.out.print("HelloWorld\n");
System.out.print(“\tHelloWorld");
System.out.println("\bHelloWorld\b\b");
System.out.print("HelloWorld\rJava");
System.out.println("D:\\Folder\\File");

System.out.println("\"Congratulations!\"");

System.out.println("\'Stop Here\'");
18
Escape Sequences (Examples)
• He said that “I am From Egypt” but lives in “KSA”
System.out.print(“He Said That\”Iam From Egypt\” but lives in \”KSA\”)
• The Files located in D:\javaprog\tryoutput
System.out.print(“The Files located in D:\\javaprorg\\tryout “)
• Sunday Monday Tuesday Wednesday Thursday
System.out.print(“Sunday\tMonday\tTuesday\tWednesday\tThursday”);

19
Primitive and Non-Primitive Data Types
byte 1 byte Integers in the range
-128 to +127
short 2 bytes Integers in the range of
-32,768 to +32,767
int 4 bytes Integers in the range of
-2,147,483,648 to +2,147,483,647
long 8 bytes Integers in the range of
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float 4 bytes Floating-point numbers in the range of
±1.4E-45 to 3.4E38, with 7-8 digits of accuracy
double 8 bytes Floating-point numbers in the range of
±4.9E-324 to 1.7E308, with 15-16 digits of accuracy
boolean 1 bit true or false
Char 2 bytes A single character ‘a’, ‘A’, ‘-’, ‘!’…

String - Words

20
Variables and Literals

• A variable is a named storage


This line is called The following line is known
location in the computer’s a variable declaration. as variable initialization
memory. int value; value = 5;
– int value;
0x000
The value 5
0x001 5
• A literal is a value that is written is stored in
0x002 Memory of size
into the code of a program.
0x003 4 bytes
– value=5;

• Declaration and Initialization can This storage holds integer values only
be done in one line.
– int value=5;

21
Variables and Literals
• Variables should start with letters, and these letters are alphabet letters (A-Z) or (a-z).
• Variable should not start with numbers or digits, but it can contain digits in between the
variable.
• Some signs are valid such as underscore (_) and dollar signs ($) and you can start with
them the variable.
• Variables should not contain operators or symbols.
• Variable should not be from the reserved words (keywords already existing and used in
Java).
• If a variable contains more than one word, it should not be separated by space or
whitespace.

22
Variables and Literals
• Variable names are preferred to be self-declarative (has meaning for the reader).
• It is a tradition to use camel case for naming variable with multiple words.
totalAmount (low camelCase) TotalAmount (upper camelCase)
• Variables are case sensitives in java (“myVar” is different from “myvar”). Both are
different variables.
abstract else interface switch
assert enum long synchronized
boolean extends native this
break false new throw
byte final null throws
case finally package transient
Reserved Words catch float private true
char for protected try
class goto public void
const if return volatile
continue implements short while
default import static
do instanceof strictfp
double int super 23
Examples on Variables Declaration & Initialization
Declaration
• int Number;
• String Name;
• float Price;
Initialization
• int a, b, c;
• int a = 10, b = 10;
• Byte B = 22;
• double pi = 3.14159;
• char a = ‘a’;

23
Variables (Examples)
grade Valid
quizGrade Valid
NetworkConnection Valid
frame2 Valid
3rdTestScore Invalid – cannot begin with a digit
MAXIMUM Valid
MIN_CAPACITY Valid
student# Invalid – cannot contain the '#' character
Shelves1&2 Invalid – cannot contain the '&' character

24
Variables (Examples)
X Valid
4Times Invalid – cannot begin with a digit
A$$$ Valid
myAddress Valid
_test Valid
$1 Valid
myNewDevice Valid
First One Invalid – cannot contain whitespace between two words
Last1 Valid

25
Input from User in Java
• In order to take an input from user, we need to perform three main steps.
• The first step is to import the class that hold the input methods. This step is written
between the package and the class.
import java.util.Scanner;
• The second step is to create a reference that refers to the input methods in the Scanner
class. This step is written in the main method.
Scanner input = new Scanner (System.in);
• The third step is to call the method that gets the input required based on the datatype:
To read an int value: int x = input.nextInt();
To read a float value: float y = input.nextFloat();
To read a double value: float d = input.nextDouble();
To read a String value: float s = input.next();
To read a char value: char c = input.next().charAt(0); 26
Errors
• Compilation Errors (Detected by the Compiler)
Variable not declared
Missing operand after operator
Missing quote

• Logical Errors (Produce Incorrect Results)


+ instead of *

• Runtime errors (Causes the Program to abort)


3/0

27
Comments
•Parts of the program that is to be skipped by the compiler.
•You can use them to write notes.

•Single line comment:


//

•Multi-line comment:
/*

*/

28

You might also like