Unit 1
Unit 1
Unit 1
Topics:
There are lots for programming language that are known but all of them need to
follow some strategy when they are implemented and this methodology/strategy is
paradigms.
Apart from varieties of programming language there are lots of paradigms to fulfil
each and every demand. They are discussed below:
1. Imperative programming paradigm: It is one of the oldest programming
paradigms. It features close relation to machine architecture. It is based on Von
Neumann architecture. It works by changing the program state through assignment
statements. It performs step by step task by changing state. The main focus is on
how to achieve the goal. The paradigm consists of several statements and after
execution of all the result is stored.
Advantage:
1. Very simple to implement
2. It contains loops, variables etc.
Disadvantage:
1. Complex problem cannot be solved
2. Less efficient and less productive
3. Parallel programming is not possible
Imperative programming is divided into three broad categories: Procedural, OOP
and parallel processing. These paradigms are as follows:
A) Procedural programming paradigm –
This paradigm emphasizes on procedure in terms of under lying
machine model. There is no difference in between procedural and
imperative approach. It has the ability to reuse the code and it was
boon at that time when it was in use because of its reusability.
Machine Language:
Machine language is the only language that computer understands and uses to
perform a various operation. Everything from playing video games to
watching your favorite movies is done by computer using Machine language.
Machine Language is a language with a bunch of 0s and 1s. Every word in
computer language is made up of 1s and 0s. This becomes almost impossible
for a programmer to understand Machine language since we hardly remember
our lines of codes. Remembering numbers instead of commands would be
very intricate for the programmer. Not to mention, debugging the program in
Machine language would be a nightmare. So, Computer scientists came up
with an upper layer of programming called Assembly Language that translates
Easy to understand (High-level language) to Machine language.
Assembly Language:
Assembly language is easier than Machine Language and lies on an upper
layer level than Machine language. However, much like computer language,
Assembly languages works on low-level programming and it is unique to a
particular processor. Unlike machine language, assembly language is not
entirely on 1s and 0s, In fact, assembly language allows programmers to use
codes or names to as oppose to a bunch of 1s and 0s. But we know that
computer only understands machine language. So how the assembly language
does is used to communicate with the computer? Well, a utility program is
used so-called "Assembler" that translate this assembly language to machine
language, the only language computer understand.
00011 010 011 ADD 010 011
High-Level Language:
High-Level Language or HLL are the languages that are designed specifically
for the programmer with much easier syntax. High-level language is much
easier to figure out because there are more English-like Languages in written
programs. However, this high-level language must be converted to machine
language, since computer only knows 1s and 0s. This translation is done by a
compiler or interpreter depending on the style. Compiler/Interpreter takes in
the source code as an input, process it by understanding the system and finally
producing executable file meaning the computer readable file. It is also
important to keep in mind that higher level language mostly focuses on
software development aspect and not on the hardware aspect.
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic
Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand.
According to Sun, Java language is a simple programming language because:
o Java syntax is based on C++ (so easier for programmers to learn it after C+
+).
o Java has removed many complicated and rarely-used features, for example,
explicit pointers, operator overloading, etc.
o There is no need to remove unreferenced objects because there is an
Automatic Garbage Collection in Java.
Object-oriented
Object
Any entity that has state and behavior is known as an object. For example: chair,
pen, table, keyboard, bike etc. It can be physical and logical.
Class
Inheritance
When one object acquires all the properties and behaviors of parent object i.e.
known as inheritance. It provides code reusability. It is used to achieve runtime
polymorphism.
Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For
example: to convince the customer differently, to draw something e.g. shape or
rectangle etc.
Abstraction
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as
encapsulation. For example: capsule, it is wrapped with different medicines.
The Java platform differs from most other platforms in the sense that it is a
software-based platform that runs on the top of other hardware-based platforms. It
has two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be run on multiple platforms, for example, Windows, Linux, Sun
Solaris, Mac/OS, etc. Java code is compiled by the compiler and converted into
byte code. This byte code is a platform-independent code because it can be run on
multiple platforms, i.e., Write Once and Run Anywhere (WORA).
Secured
Java is best known for its security. With Java, we can develop virus-free systems.
Java is secured because:
o No explicit pointer
o Java Programs run inside a virtual machine sandbox
o Classloader: Classloader in Java is a part of the Java Runtime Environment
(JRE) which is used to load Java classes into the Java Virtual Machine
dynamically. It adds security by separating the package for the classes of the
local file system from those that are imported from network sources.
o Byte code Verifier: It checks the code fragments for illegal code that can
violate access right to objects.
o Security Manager: It determines what resources a class can access such as
reading and writing to the local disk.
Java language provides these securities by default. Some security can also be
provided by an application developer explicitly through SSL, JAAS,
Cryptography, etc.
Robust
Architecture-neutral
Portable
Java is portable because it facilitates you to carry the Java bytecode to any
platform. It doesn't require any implementation.
High-performance
Distributed
Dynamic
JVM
JVM (Java Virtual Machine) is an abstract machine.
JVMs are available for many hardware and software platforms. JVM, JRE, and
JDK are platform dependent because the configuration of each OS is different from
each other. However, Java is platform independent. The JVM performs the
following main tasks:
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.
JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader, memory
area, execution engine etc.
1) Classloader
1. Bootstrap ClassLoader: This is the first classloader which is the super class
of Extension classloader. It loads the rt.jar file which contains all class files
of Java Standard Edition like java.lang package classes, java.net package
classes, java.util package classes, java.io package classes, java.sql package
classes etc.
2. Extension ClassLoader: This is the child classloader of Bootstrap and
parent classloader of System classloader. It loades the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
3. System/Application ClassLoader: This is the child classloader of
Extension classloader. It loads the classfiles from classpath. By default,
classpath is set to current directory. You can change the classpath using "-
cp" or "-classpath" switch. It is also known as Application classloader.
2) Class(Method) Area
Class(Method) Area stores class structures such as the runtime constant pool, field
and method data, the code for methods.
3) Heap
4) Stack
Java Stack stores frames. It holds local variables and partial results, and plays a
part in method invocation and return.
Each thread has a private JVM stack, created at the same time as thread.
A new frame is created each time a method is invoked. A frame is destroyed when
its method invocation completes.
5) Program Counter Register
PC (program counter) register contains the address of the Java virtual machine
instruction currently being executed.
7) Execution Engine
It contains:
1. A virtual processor
2. Interpreter: Read bytecode stream then execute the instructions.
3. Just-In-Time(JIT) compiler: It is used to improve the performance. JIT
compiles parts of the byte code that have similar functionality at the same
time, and hence reduces the amount of time needed for compilation. Here,
the term "compiler" refers to a translator from the instruction set of a Java
virtual machine (JVM) to the instruction set of a specific CPU.
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE.
The Java Runtime Environment is a set of software tools which are used for
developing Java applications. It is used to provide the runtime environment. It is
the implementation of JVM. It physically exists. It contains a set of libraries +
other files that JVM uses at runtime.
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is
a software development environment which is used to develop Java applications
and applets. It physically exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released
by Oracle Corporation:
The JDK contains a private Java Virtual Machine (JVM) and a few other resources
such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a
documentation generator (Javadoc), etc. to complete the development of a Java
Application.
Java Platforms / Editions
4) JavaFX
Java SE is freely available. Version based on your operating system can be easily
downloaded.
Follow the instructions to download Java and run the .exe to install Java on your
machine. Once you installed Java on your machine, you will need to set
environment variables to point to correct installation directories –
Setting Up the Path for Windows
Assuming you have installed Java in c:\Program Files\java\jdk directory −
Right-click on 'My Computer' and select 'Properties'.
Click the 'Environment variables' button under the 'Advanced' tab.
Now, alter the 'Path' variable so that it also contains the path to the Java
executable. Example, if the path is currently set to 'C:\WINDOWS\
SYSTEM32', then change your path to read 'C:\WINDOWS\
SYSTEM32;c:\Program Files\java\jdk\bin'.
Popular Java Editors
To write your Java programs, you will need a text editor. There are even more
sophisticated IDEs available in the market. But for now, you can consider one of
the following −
Notepad − On Windows machine, you can use any simple text editor like
Notepad
Netbeans − A Java IDE that is open-source and free which can be
downloaded from https://www.netbeans.org
Eclipse − A Java IDE developed by the eclipse open-source community and
can be downloaded from https://www.eclipse.org
Bytecode Verifier
After the bytecode of a class is loaded by the class loader, it has to be
inspected by the bytecode verifier, whose job is to check that the
instructions don’t perform damaging actions. The following are some of
the checks carried out:
Java programs are composed of characters from the Unicode character set.
Java comments
Comments are used by humans to clarify source code. There are three types of
comments in Java.
Comment type Meaning
If we want to add some small comment we can use single-line comments. For
more complicated explanations, we can use multi-line comments. The
documentation comments are used to prepare automatically generated
documentation. This is generated with the javadoc tool.
/*
This is Comments.java
*/
White space in Java is used to separate tokens in the source file. It is also used to
improve readability of the source code.
int i = 0;
White spaces are required in some places. For example between the int keyword
and the variable name. In other places, white spaces are forbidden. They cannot
be present in variable identifiers or language keywords.
int a=1;
int b = 2;
int c = 3;
The amount of space put between tokens is irrelevant for the Java compiler. The
white space should be used consistently in Java source code.
Java identifiers
Identifiers are case sensitive. This means that Name, name, or NAME refer to three
different variables. Identifiers also cannot match language keywords.
There are also conventions related to naming of identifiers. The names should be
descriptive. We should not use cryptic names for our identifiers. If the name
consists of multiple words, each subsequent word is capitalized.
String 23name;
int %col;
short car age;
Java literals(constants)
Here we assign two literals to variables. Number 29 and string "Hungarian" are
literals.
[ ] ( ) { } , ; . "
String language = "Java";
The double quotes are used to mark the beginning and the end of a string. The
semicolon ; character is used to end each Java statement.
System.out.println("Java language");
The square brackets [] are used to denote an array type. They are also used to
access or modify array elements. The curly brackets {} are used to initiate arrays.
The curly brackets are also used enclose the body of a method or a class.
int a, b, c;
class Abc
{
Public:
Int a1,b;
Void out();
}
Abc a
a.a1=4;
a.out();
Java keywords
The package, public, class, static, void, int, for tokens are Java keywords.
Java conventions
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
1. Primitive data types: The primitive data types include Boolean, char, byte,
short, int, long, float and double.
2. Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
byte 2^8
Data Type Default Value Default size
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
The Boolean data type is used to store only two possible values: true and false.
This data type is used for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be
defined precisely.
The byte data type is an example of primitive data type. It is an 8-bit signed two's
complement integer. Its value-range lies between -128 to 127 (inclusive). Its
minimum value is -128 and maximum value is 127. Its default value is 0.
The byte data type is used to save memory in large arrays where the memory
savings is most required. It saves space because a byte is 4 times smaller than an
integer. It can also be used in place of "int" data type.
The short data type can also be used to save memory just like byte data type. A
short data type is 2 times smaller than an integer.
The int data type is a 32-bit signed two's complement integer. Its value-range lies
between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its
minimum value is - 2,147,483,648and maximum value is 2,147,483,647. Its default
value is 0.
The int data type is generally used as a default data type for integral values unless
if there is no problem about memory.
The long data type is a 64-bit two's complement integer. Its value-range lies
between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -
1)(inclusive). Its minimum value is - 9,223,372,036,854,775,808and maximum
value is 9,223,372,036,854,775,807. Its default value is 0. The long data type is
used when you need a range of values more than those provided by int.
The float data type is a single-precision 32-bit IEEE 754 floating point.Its value
range is unlimited. It is recommended to use a float (instead of double) if you need
to save memory in large arrays of floating point numbers. The float data type
should never be used for precise values, such as currency. Its default value is 0.0F.
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value
range is unlimited. The double data type is generally used for decimal values just
like float. The double data type also should never be used for precise values, such
as currency. Its default value is 0.0d.
The char data type is a single 16-bit Unicode character. Its value-range lies
between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to
store characters.
Unicode System
2. The encodings for languages with large character sets have variable length. Some
common characters are encoded as single bytes, other require two or more byte.
Solution
To solve these problems, a new language standard was developed i.e. Unicode System.
In unicode, character holds 2 byte, so java also uses 2 byte for characters.
lowest value:\u0000
highest value:\uFFFF
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
Arithmetic operators are used in mathematical expressions in the same way that
they are used in algebra. The following table lists the arithmetic operators −
If(A<B)
& (bitwise Binary AND Operator copies a bit to (A & B) will give 12 which
and) the result if it exists in both operands. is 0000 1100
^ (bitwise Binary XOR Operator copies the bit if (A ^ B) will give 49 which
XOR) it is set in one operand but not both. is 0011 0001
<< (left shift) Binary Left Shift Operator. The left A=60(0011 1100)
operands value is moved left by the
A << 2 will give 240 which
number of bits specified by the right
is 1111 0000
operand.
01111000(120)
Simple assignment operator. Assigns values from right side operands to C=A+B
left side operand. will assign
=
value of A
+ B into C
Add AND assignment operator. It adds right operand to the left operand C += A is
and assign the result to left operand. equivalent
+=
to C = C +
A
-= Subtract AND assignment operator. It subtracts right operand from the left C -= A is
operand and assign the result to left operand. equivalent
to C = C –
A
Multiply AND assignment operator. It multiplies right operand with the left C *= A is
operand and assign the result to left operand. equivalent
*=
to C = C *
A
/= Divide AND assignment operator. It divides left operand with the right C /= A is
operand and assign the result to left operand. equivalent
to C = C /
A
C <<= 2
is same
<<= Left shift AND assignment operator.
as C = C
<< 2
C >>= 2
is same
>>= Right shift AND assignment operator.
as C = C
>> 2
C &= 2 is
&= Bitwise AND assignment operator. same as
C=C&2
Miscellaneous Operators
There are few other operators supported by Java Language.
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator
consists of three operands and is used to evaluate Boolean expressions. The goal
of the operator is to decide, which value should be assigned to the variable. The
operator is written as −
variable x = (expression) ? value if true : value if false
A=40,B=30
C=(A>B)?3:4;
If(A>B)
{
C=3;
}
Else
{
C=4;
}
Precedence of Java Operators
Operator precedence determines the grouping of terms in an expression. This
affects how an expression is evaluated. Certain operators have higher precedence
than others; for example, the multiplication operator has higher precedence than
the addition operator −
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has
higher precedence than +, so it first gets multiplied with 3 * 2 and then adds into
7.
Here, operators with the highest precedence appear at the top of the table, those
with the lowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first.
Category Operator Associativity