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

Week 3

Uploaded by

Ara K.b
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)
23 views

Week 3

Uploaded by

Ara K.b
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/ 43

Programming Concept and Algorithms

Basic Elements of Java (Part 1)

Dr. Karzan Hussein sharif


[email protected]
What is JAVA?
• Java is a high level, secured and object- oriented programming language.

• Platform: Since Java has its own runtime environment (JRE) and API, it is
called platform.

• Portability: Java does run on all the platforms.

• Huge library and developer community support available on Internet.

2
What is JDK……?

• JDK stand for java development kit


• It is used to build and develop the java program
• It internally contains JRE, compiler and debugger, libraries and files to build
and compile the java program.
• Without JDK we can’t build any java program

3
What is JRE…..?

• JRE stands for Java Runtime environment


• JRE is the responsible unit to run the java program
• Without JRE we can’t run java program (JDK is used to build java program,
where as JRE is used to run java program).
• JRE contains JVM
• JRE contains all the inbuilt packages and library files (lang , io , util etc all
packages are present in JRE)

4
JVM
• The Java Virtual Machine (JVM)
is a runtime environment that
enables Java bytecode (compiled
Java programs) to be executed on
any machine, regardless of its
underlying hardware or operating
system. It is a core component of
the Java Runtime Environment
(JRE).
5
Life cycle of a java program
• Three main stages in the life cycle of a java program. They are:

1. Editing the program

2. Compiling the source code

3. Executing the byte code

6
Editing the program
• Start with typing the program in a text-editor (ex: Dr.java, notepad++, wordpad, textedit
etc).

• Save the file. While saving the file you should remember that the file must be saved
with .java extension.

• The name of the Java compiler is javac.

• The output of the compiler is machine independent or platform independent code which is
known as bytecode.

• The file which is generated after compilation is .class file.

7
Executing the byte code
• Last step is execution.

• The bytecode generated by the compiler will be executed by Java Virtual


Machine (JVM).

• Input to the JVM is bytecode and output is machine code (0’s and 1’s)
which will be executed by the CPU of the local machine.

8
Life cycle of a java program

9
Platform independent

10
Tools we will need

• For this course we will need the following software’s:


• An operating system like Windows
• Java jdk
• Dr. Java as text editor.

11
Download java development kit

• Download Java SE Development Kit According to your Operating System


architecture (32bit or 64bit).

• https://www.oracle.com/technetwork/java/javase/downloads/index.html

• We can also download JDK from any site with latest version.

12
Now Download Dr. Java

• For Download Dr. Java Go this Site


• https://drjava.sourceforge.net/.

• For how to install and setup dr. java visit this link:
• https://www.youtube.com/watch?v=Kt9io-ctgLs

13
First Java program

main() method : This is the main method which is executed, hence all the logic must be inside the
main() method. If a java class is not having a main() method, it causes compilation error.
14
Comments in java

• Commenting in Java. Comments are an integral part of any program. They


help the person reading the code (often you) better understand the intent and
functionality of the program.

Type of comments:
1. Single line comment.
2. Multi Line Comment.

15
Comments in java
1. Single line comment.
// symbol is use to write single line comment in java.
Syntax:
// comments code
2. Multi Line Comment.
/* */ symbol is use to write multi line comment in java.
Syntax:
/* comment code line 1
comment code line 2 */

16
Variable in Java
Variable: is name of reserved area allocated in memory.
variable names start with a lowercase letter

17
Variable in Java
• Each variable in Java has a specific type, which determines:
✓ The size of the variable's memory.
✓The set of operations that can be applied to the variable.

• We must declare all variables before they can be used. Following is the basic
form of a variable declaration:

data type variable = value ; variable = value ... ;

18
19
Identifiers in Java
• All Java components require names.
• Name used for classes, methods, interfaces and variables are called Identifier.
• Identifier must follow some rules. Here are the rules:
1. All identifiers must start with either a letter( a to z or A to Z ) or currency
character($) or an underscore ( _ ).
2. After the first character, an identifier can have any combination of characters.
3. A Java keyword cannot be used as an identifier.
4. Identifiers in Java are case sensitive, foo and Foo are two different identifiers.
5. Spaces are not permitted inside identifiers

20
Java Keywords

21
Example
int score; //Then assign a value score=98
int score=98;
int myScore=92; //(Correct: camel Case)
int 1myScore=92; //(Wrong)
int my Score=92; //(Wrong)
Int @myScore=92; //(Wrong)

22
Example

23
Data types in java
• The variables are the way to store data in the programming languages.

• Depending on the data types the operating system allocates memory and
decides what type of data to be stored e.g. a number, a character etc.

• There are two kinds of Java data types:


1. Primitive data types
2. Non-Primitive data types (Object data types also known as the Reference
data types )
24
Data types in java

25
Primitive Data Types
• There are eight primitive datatypes supported by Java.

✓ Byte: 8 bit, and number.


✓ Short: is number type and takes two bytes.
✓ int: is a numeric data type and takes four bytes.
✓ Long: is numeric and takes eight bytes.
✓ float: is a single precision and takes four bytes.
✓ double: is a double precision and takes eight bytes.
✓ char: can store any character and takes two bytes.
✓ boolean: It takes one byte and can store either of two values : True or False.

26
Primitive Data Types

27
Primitive Data Types

• Primitive data types allows us to store only one value


• This is a data type whose variable can hold maximum one value at a time.
Example These are
int a; // valid comment

a=10; // valid Variable


a=10, 20, 30; // invalid Name

Data Type

28
Primitive Data Types Example

29
Default value

• Variables that are declared but not initialized will be set to a reasonable
default by the compiler

• default will be zero or null, depending on the data type

• When variable is declared, then initialization of that variable is optional.

• Means you may or may not initialize to its value. If you are not, then Java
runtime assigns default value to it.
30
What about data values such as a person’s name?
String

• A person’s name contains more than one character. Such values are called
strings.
• String: Is a sequence of zero or more characters.
• Strings in Java are enclosed in double quotation marks “ string”.
• A string that contains no characters is called a null or empty string.
• Example: “Ara“ , and “ “
• Every character in a string has a specific position in the string.
• The position of the first character is 0, the position of the second character is
1, and so on.
• The length of a string is the number of characters in it.
31
Example

String “Sunny Day”

Character in the string ‘S’ ‘u’ ‘n’ ‘n’ ‘y’ ‘‘ ‘D’ ‘a’ ‘y’

Position of the character in the string 0 1 2 3 4 5 6 7 8

The length of the string “Sunny Day” is 9

32
String and the operator +

• One of the most common operations performed on strings is the


concatenation operation, which allows a string to be appended at the end of
another string.

• The operator + can be used to concatenate (or join) two strings as well as a
string and a numeric value or a character.

• "Sunny" + " Day“ This expression evaluates to "Sunny Day"

33
Example

1. "The sum = " + 12 + 26


This expression evaluates to
"The sum = 1226“

2. "The sum = " + 12 + 26


is evaluated as follows:
"The sum = " + 12 + 26 = ("The sum = " + 12) + 26
= "The sum = 12" + 26
= "The sum = 1226"
34
Text Codes

• Computer programmers realized that they need a commonly agreed


standard code in which numbers stood for alphabets, special
characters and digits.

• ASCII and Unicode are two most popular coding systems.

35
ASCII
• Every letter, number, or special symbol (such as * or {) on your keyboard is
encoded as a sequence of bits, each having a unique representation.

• The characters from 0 to 31 are control characters.


• 32 to 64 special characters
• 65 to 96 uppercase letters and few symbols
• 97 to 127 lowercase letters and other symbols
• And 128 to 255 are other symbols.

36
UNICODE
• An developing standard is Unicode Worldwide Character Standard.
• It has two bytes 16 bits to represent each symbol.
• It can represent 65536 symbols, which include international symbols, and
alphabets from different languages in the world.

• The ASCII character set is a subset of Unicode; the first 128 characters of Unicode are
the same as the characters in ASCII.
• The advantage of the Unicode character set is that symbols from languages other than
English can be handled easily.

37
Java naming conventions

Major naming convention in java:


• package in java should be in small letters as java.util.
• Each word of interfaces should start with capital letter as Serializable
• Each word of classes should start with capital letter as String
• Method name should be in small letters as println().
• Constants should be in all capital letters like MAX_VALUE
• Variable name should be start with small letter and change as per word
change.

38
Arithmetic operators
• Arithmetic operators are
used in mathematical like
addition, subtraction etc.
• Arithmetic Expression is
constructed by using
arithmetic operators and
numbers.
• The numbers and
alphabetical symbols in the
expression are called
operand.

39
Character Arithmetic
• Since the char data type is also an integral data type, java allows you to preform
arithmetic operations on char data.
• There is difference between the character ‘8’ and the integer 8.
• the integer value 8 is 8.
• The integer value of ‘8’ is 56, which is the Unicode collating sequence of the
character ‘8’.

1. 8 + 7 = 15
2. ‘8’ * ‘7’ = 56 * 55 = 3080.

40
Escape Character
Escape Sequence Description
\n Newline Cursor moves to the beginning of the next
line
\t Tab Cursor moves to the next tab stop
\b Backspace Cursor moves one space to the left
\r Return Cursor moves to the beginning of the
current line (not the next line)
\\ Backslash Backslash is printed
\’ Single quotation Single quotation mark is printed
\” Double quotation double quotation mark is printed

41
Example
1. System.out.print("Hello there. ");
System.out.print("My name is James.");
If these statements are executed in sequence, the output is:
Hello there. My name is James.

2. System.out.print("Hello there.\n");
System.out.print("My name is James.");
The output of these Java statements is:
Hello there.
My name is James.
3. System.out.print("Hello \nthere. \nMy name is James.");
Hello
there.
My name is James.

42
Lab Activity

1. Write a program that prints the following banner:

2. Write a Java program to performs the following arithmetic operations:


addition, subtraction, multiplication, division, and modulus. Display the
results for each operation.

43

You might also like