Week 3
Week 3
• Platform: Since Java has its own runtime environment (JRE) and API, it is
called platform.
2
What is JDK……?
3
What is 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:
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 output of the compiler is machine independent or platform independent code which is
known as bytecode.
7
Executing the byte code
• Last step is execution.
• 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
11
Download java development kit
• 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 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
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:
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.
25
Primitive Data Types
• There are eight primitive datatypes supported by Java.
26
Primitive Data Types
27
Primitive Data Types
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
• 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
Character in the string ‘S’ ‘u’ ‘n’ ‘n’ ‘y’ ‘‘ ‘D’ ‘a’ ‘y’
32
String and the operator +
• The operator + can be used to concatenate (or join) two strings as well as a
string and a numeric value or a character.
33
Example
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.
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
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
43