SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Unit 1
Topics:
 Introduction: Programming language Types and Paradigms,
 Computer Programming Hierarchy
 Features of Java Language
 JVM –The heart of Java, Java’s Magic Bytecode.
 The Java Environment: Installing Java, Java Program Development, Java
Source File, Structure, Compilation, Executions.
 Basic Language Elements: Lexical Tokens, Identifiers, Keywords, Literals,
Comments, Primitive Data types, Operators Assignments.
Introduction of Programming Paradigms
Paradigm can also be termed as method to solve some problem or do some task.
Programming paradigm is an approach to solve problem using some programming
language.
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.
Examples of Procedural programming paradigm:
C ,C++ ,Pascal, etc.
B) Object oriented programming –
The program is written as a collection of classes and object which are
meant for communication. The smallest and basic entity is object and
all kind of computation is performed on the objects only. More
emphasis is on data rather procedure. It can handle almost all kind of
real life problems which are today in scenario.
Advantages:
 Data security
 Inheritance
 Code reusability
 Flexible and abstraction is also present
Examples: C++ , JAVA, Python etc.
C) Parallel processing approach –
Parallel processing is the processing of program instructions by
dividing them among multiple processors. A parallel processing
system posses many numbers of processor with the objective of
running a program in less time by dividing them. This approach seems
to be like divide and conquer. Examples are NESL (one of the oldest
one) and C/C++ also supports because of some library function.
2. Declarative programming paradigm: It is divided as Logic, Functional, and
Database. In computer science the declarative programming is a style of building
programs that expresses logic of computation without talking about its control
flow. It often considers programs as theories of some logic. It may simplify writing
parallel programs. The focus is on what needs to be done rather how it should be
done basically emphasizing on what code is actually doing. It just declares the
result we want rather how it has be produced. This is the only difference between
imperative (how to do) and declarative (what to do) programming paradigms.
Getting into deeper we would see logic, functional and database.
1. Logic programming paradigms –
It can be termed as abstract model of computation. It would solve logical
problems like puzzles, series etc. In logic programming we have a knowledge
base which we know before and along with the question and knowledge base
which is given to machine, it produces result. In normal programming
languages, such concept of knowledge base is not available but while using
the concept of artificial intelligence, machine learning we have some models
like Perception model which is using the same mechanism.
In logical programming the main emphasize is on knowledge base and the
problem. The execution of the program is very much like proof of
mathematical statement, e.g., Prolog
2. Functional programming paradigms –
The functional programming paradigms has its roots in mathematics and it is
language independent. The key principal of this paradigm is the execution of
series of mathematical functions. The central model for the abstraction is the
function which is meant for some specific computation and not the data
structure. Data are loosely coupled to functions. The function hides their
implementation. Function can be replaced with their values without changing
the meaning of the program. Some of the languages like Perl, JavaScript
mostly uses this paradigm.
Examples of Functional programming paradigm:
JavaScript, Scala ,Lisp, etc.
The next kind of approach is of Database.
3. Database/Data driven programming approach –
This programming methodology is based on data and its movement. Program
statements are defined by data rather than hard-coding a series of steps. A
database program is the heart of a business information system and provides
file creation, data entry, update, query and reporting functions. There are
several programming languages that are developed mostly for database
application. For example SQL. It is applied to streams of structured data, for
filtering, transforming, aggregating (such as computing statistics), or calling
other programs. So it has its own wide application.
Computer Programming Hierarchy
 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.
Overview of Language Hierarchy:
Features of Java
The primary objective of Java programming language creation was to make it
portable, simple and secure programming language. Apart from this, there are also
some excellent features which play an important role in the popularity of this
language.
A list of most important features of Java language is given below.
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
Java is an object-oriented programming language. Everything in Java is an object.
Object-oriented means we organize our software as a combination of different
types of objects that incorporates both data and behavior.
Object-oriented programming (OOPs) is a methodology that simplifies software
development and maintenance by providing some rules.
Basic concepts of OOPs are:
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
Collectionof objects is called class. It is a logical entity.
Inheritance
When one objectacquires all the properties and behaviors of parent object i.e.
known as inheritance. It provides codereusability. 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.
In C++, we use Function overloading and Function overriding to achieve
polymorphism.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For
example: phone call, we don'tknow the internal processing.
In C++, we use abstract class and interface to achieve abstraction.
Encapsulation
Binding (or wrapping) code and data togetherinto a single unit is known as
encapsulation. Forexample: capsule, it is wrapped with different medicines.
Platform Independent: Java is platform independent because it is different from
other languages like C, C++, etc. which are compiled into platform specific
machines while Java is a write once, run anywhere language. A platform is the
hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java
provides a software-based platform.
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
Robust simply means strong. Java is robust because:
o It uses strong memory management.
o There is a lack of pointers that avoids security problems.
o There is automatic garbage collection in java which runs on the Java Virtual
Machine to get rid of objects which are not being used by a Java application
anymore.
o There are exception handling and the type checking mechanism in Java. All
these points make Java robust.
Architecture-neutral
Java is architecture neutral because there are no implementation dependent
features, for example, the size of primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit
architecture and 4 bytes of memory for 64-bit architecture. However, it occupies 4
bytes of memory for both 32 and 64-bit architectures in Java.
Portable
Java is portable because it facilitates you to carry the Java bytecode to any
platform. It doesn't require any implementation.
High-performance
Java is faster than other traditional interpreted programming languages because
Java byte code is "close" to native code. It is still a little bit slower than a compiled
language (e.g., C++). Java is an interpreted language that is why it is slower than
compiled languages, e.g., C, C++, etc.
Distributed
Java is distributed because it facilitates users to create distributed applications in
Java. RMI and EJB are used for creating distributed applications. This feature of
Java makes us able to access files by calling the methods from any machine on the
internet.
Multi-threaded
A thread is like a separate program, executing concurrently. We can write Java
programs that deal with many tasks at once by defining multiple threads. The main
advantage of multi-threading is that it doesn't occupy memory for each thread. It
shares a common memory area. Threads are important for multi-media, Web
applications, etc.
Dynamic
Java is a dynamic language. It supports dynamic loading of classes. It means
classes are loaded on demand. It also supports functions from its native languages,
i.e., C and C++.
Java supports dynamic compilation and automatic memory management (garbage
collection).
JVM
JVM (Java Virtual Machine) is an abstract machine.
It is called a virtual machine because it doesn'tphysically exist. It is a specification
that provides a runtime environment in which Java byte codecan be executed. It
can also run those programs which are written in other languages and compiled to
Java byte code.
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
JVM provides definitions for the:
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
Classloader is a subsystemof JVM which is used to load class files. Whenever we
run the java program, it is loaded first by the classloader. There are three built-in
classloaders in Java.
1. BootstrapClassLoader: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 codefor methods.
3) Heap
It is the runtime data area in which objects are allocated.
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) ProgramCounter Register
PC (program counter) register contains the address of the Java virtual machine
instruction currently being executed.
6) Native Method Stack
It contains all the native methods used in the application.
7) Execution Engine
It contains:
1. A virtual processor
2. Interpreter: Read bytecodestream then execute the instructions.
3. Just-In-Time(JIT) compiler: It is used to improve the performance. JIT
compiles parts of the byte codethat 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.
8) Java Native Interface
Java Native Interface (JNI) is a framework which provides an interface to
communicate with another application written in another language like C, C++,
Assembly etc. Java uses JNI framework to send output to the Consoleor interact
with OS libraries.
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.
The implementation of JVM is also actively released by other companies besides
Sun Micro Systems.
JDK
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:
o Standard Edition Java Platform
o Enterprise Edition Java Platform
o Micro Edition Java Platform
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
There are 4 platforms or editions of Java:
1) Java SE (Java Standard Edition)
It is a Java programming platform. It includes Java programming APIs such as
java.lang, java.io, java.net, java.util, java.sql, java.math etc. It includes coretopics
like OOPs, String, Regex, Exception, Inner classes, Multithreading, I/O Stream,
Networking, AWT, Swing, Reflection, Collection, etc.
2) Java EE (Java Enterprise Edition)
It is an enterprise platform which is mainly used to develop web and enterprise
applications. It is built on the top of the Java SE platform. It includes topics like
Servlet, JSP, Web Services, EJB, JPA, etc.
3) Java ME (Java Micro Edition)
It is a micro platform which is mainly used to develop mobile applications.
4) JavaFX
It is used to develop rich internet applications. It uses a light-weight user interface
API.
Local Environment Setup
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 Filesjavajdk 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:WINDOWSSYSTEM32', then change your path to read
'C:WINDOWSSYSTEM32;c:Program Filesjavajdkbin'.
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
Compilation and Execution of a Java Program
Java, being a platform independent programming language, doesn’twork on one-
step-compilation. Instead, it involves a two-step execution, first through an OS
independent compiler; and second, in a virtual machine (JVM) which is custom-
built for every operating system The two principle stages are explained below:
Compilation javac a.java
First, the source‘a.java’ file is passed through the compiler, which then encodes
the source codeinto a machine independent encoding, known as Bytecode.
The content of each class contained in the source file is stored in a separate ‘.class’
file.
Execution java a
The class files generated by the compiler are independent of the machine or the
OS, which allows them to be run on any system. To run, the main class file (the
class that contains the method main) is passed to the JVM, and then goes through
three main stages before the final machine codeis executed. These stages are:
Class Loader
The main class is loaded into the memory by passing its ‘.class’ file to the
JVM, through invoking the latter. All the other classes referenced in the
program are loaded through the class loader.
A class loader, itself an object, creates a flat name space of class bodies that
are referenced by a string name.
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:
 Variables are initialized before they are used.
 Method calls match the types of object references.
 Rules for accessing private data and methods are not violated.
 Local variable accesses fall within the runtime stack.
 The run time stack does not overflow.
If any of the above checks fails, the verifier doesn’tallow the class to be
loaded.
Just-In-Time Compiler
This is the final stage encountered by the java program, and its job is to
convert the loaded bytecode into machine code.
The process canbe well-illustrated by the following diagram:
Due to the two-step execution process described above, a java program is
independent of the target operating system. However, because of the same, the
execution time is way more than a similar program written in a compiled platform-
dependent program.
Java lexical structure
Computer languages, like human languages, have a lexical structure. A source
code of a Java program consists of tokens. Tokens are atomic code elements. In
Java we have comments, identifiers, literals, operators, separators, and
keywords.
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
// comment Single-line comments
/* comment */ Multi-line comments
/** documentation */ Documentation comments
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.
The program uses two types of comments.
// Program starts here
This is an example of a single-line comment.
Comments are ignored by the Java compiler.
/*
This is Comments.java
*/
Comments cannot be nested. The above code does not compile.
Java white space
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 names for variables, methods, classes, or parameters. Identifiers
can have alphanumerical characters, underscores and dollar signs ($). It is an
error to begin a variable name with a number. White space in names is not
permitted.
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 name23; int a, A;
int _col;
short car_age;
These are valid Java identifiers.
String 23name;
int %col;
short car age;
These are invalid Java identifiers.
Java literals(constants)
A literal is a textual representation of a particular value of a type. Literal types
include Boolean, integer, floating point, string, null, or character. Technically, a
literal will be assigned a value at compile time, while a variable will be assigned at
runtime.
int age = 29;
String nationality = "Hungarian";
Here we assign two literals to variables. Number 29 and string "Hungarian" are
literals.
In the above example, we have several literal values. 23 is an integer literal.
"James" is a string literal. The true is a boolean literal. The null is a literal that
represents a missing value. 68.5 is a floating point literal. 'J' is a character literal.
Java separators
A separator is a sequence of one or more characters used to specify the boundary
between separate, independent regions in plain text or other data stream.
[ ] ( ) { } , ; . "
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");
Parentheses (round brackets) always follow a method name. Between the
parentheses we declare the input parameters. The parentheses are present even if
the method does not take any parameters. The System.out.println() method takes
one parameter, a string value. The dot character separates the class name (System)
from the member (out) and the member from the method name (println()).
int[] array = new int[5] { 1, 2, 3, 4, 5 };
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();
The comma character separates variables in a single declaration.
Java keywords
A keyword is a reserved word in Java language. Keywords are used to perform a
specific task in the computer program. For example, to define variables, do
repetitive tasks or perform logical operations.
Java is rich in keywords. Many of them will be explained in this tutorial.
abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static var
class finally long strictfp void
const float native super volatile
while
The package, public, class, static, void, int, for tokens are Java keywords.
Java conventions
Conventions are best practices followed by programmers when writing source
code. Each language can have its own set of conventions. Conventions are not
strict rules; they are merely recommendations for writing good quality code. We
mention a few conventions that are recognized by Java programmers. (And often
by other programmers too).
 Class names begin with an uppercase letter.
 Method names begin with a lowercase letter.
 The public keyword precedes the static keyword when both are used.
 The parameter name of the main() method is called args.
 Constants are written in uppercase.
 Each subsequent word in an identifier name begins with a capital letter.
Data Types in Java
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.
Java Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation.
These are the most basic data types available in Java language.
Java is a statically-typed programming language. It means, all variables must be
declared before its use. That is why we need to declare variable's type and name.
There are 8 types of primitive data types:
o Boolean data type
o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type
short a; 2^16 (65,536) -32,768 to 32,767
byte 2^8
BooleanData Type
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.
Example: Boolean one = false
Byte Data Type
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 spacebecausea byte is 4 times smaller than an
integer. It can also be used in place of "int" data type.
Data Type Default Value Default size
boolean False(0), true(1) 1 bit
char 'u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
Example: byte a = 10, byte b = -20
Short Data Type
The short data type is a 16-bit signed two's complement integer. Its value-range
lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and
maximum value is 32,767. Its default value is 0.
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.
Example: short s = 10000, short r = -5000
Int Data Type
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.
Example: int a = 100000, int b = -200000
Long Data Type
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.
Example: long a = 100000L, long b = -200000L
FloatData Type
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.
Example: float f1 = 234.5f
Double Data Type
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.
Example: double d1 = 12.3
Char Data Type
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.
Example: char letterA = 'A'
Unicode System
Unicode is a universal international standard character encoding that is capable of
representing most of the world's written languages.
Why java uses Unicode System?
Before Unicode, there were many language standards:
o ASCII (American Standard Codefor Information Interchange) for the United
States.
o ISO 8859-1 forWestern European Language.
o KOI-8 for Russian.
o GB18030 andBIG-5 for chinese, and so on.
This causedtwo problems:
1. A particular codevalue corresponds to different letters in the various language
standards.
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.
lowestvalue:u0000
highest value:uFFFF
Java - Basic Operators
Java provides a rich set of operators to manipulate variables. We can divide all the
Java operators into the following groups −
 Arithmetic Operators
 Relational Operators
 Bitwise Operators
 Logical Operators
 Assignment Operators
 Misc Operators
The Arithmetic 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 −
Assume integer variable A holds 10 and variable B holds 20, then −
Operator Description Example
+ (Addition)
Adds values on either side of the operator. A + B
will give
30
- (Subtraction)
Subtracts right-hand operand from left-hand operand. A - B will
give -10
* (Multiplication)
Multiplies values on either side of the operator. A * B will
give 200
/ (Division)
Divides left-hand operand by right-hand operand. B / A will
give 2
% (Modulus)
Divides left-hand operand by right-hand operand and
returns remainder 4%5= 4, 1%5=1, 5%5=0
B % A
will give
0
++ (Increment)
(unary)
Increases the value of operand by 1. ++B;, B++
C=++B+B++;
C=22+20=42
B++
gives 21
--(Decrement)
(unary)
Decreases the value of operand by 1. –B, B--
B-- gives
19
The RelationalOperators
There are following relational operators supported by Java language.
Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example
== (equal to)
Checks if the values of two operands are equal or
not, if yes then condition becomes true.
(A == B) is
not true.
!= (not equal to) Checks if the values of two operands are equal or
(A != B) is
not, if values are not equal then condition becomes
true.
true.
> (greater than)
Checks if the value of left operand is greater than
the value of right operand, if yes then condition
becomes true.
(A > B) is
not true.
< (less than)
Checks if the value of left operand is less than the
value of right operand, if yes then condition
becomes true.
A=2, B=4
C=A>B(0)
If(A<B)
(A < B) is
true.
>= (greater than
or equal to)
Checks if the value of left operand is greater than or
equal to the value of right operand, if yes then
condition becomes true.
(A >= B) is
not true.
<= (less than or
equal to)
Checks if the value of left operand is less than or
equal to the value of right operand, if yes then
condition becomes true.
(A <= B) is
true.
The Bitwise Operators
Java defines several bitwise operators, which can be applied to the integer types,
long, int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a =
60 and b = 13; now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100 AND
a|b = 0011 1101 OR |
a^b = 0011 0001 XOR^
~a = 1100 0011 NOT
The following table lists the bitwise operators −
Assume integer variable A holds 60 and variable B holds 13 then −
Operator Description Example
& (bitwise
and)
Binary AND Operator copies a bit to
the result if it exists in both operands.
(A & B) will give 12 which
is 0000 1100
| (bitwise or)
Binary OR Operator copies a bit if it
exists in either operand.
(A | B) will give 61 which is
0011 1101
^ (bitwise
XOR)
Binary XOR Operator copies the bit if
it is set in one operand but not both.
(A ^ B) will give 49 which
is 0011 0001
~ (bitwise
compliment)
Binary Ones Complement Operator is
unary and has the effect of 'flipping'
bits.
(~A ) will give -61 which is
1100 0011 in 2's
complement form due to a
signed binary number.
<< (left shift)
Binary Left Shift Operator. The left A=60(0011 1100)
operands value is moved left by the
number of bits specified by the right
operand.
A << 2 will give 240 which
is 1111 0000
01111000(120)
>> (right
shift)
Binary Right Shift Operator. The left
operands value is moved right by the
number of bits specified by the right
operand.
A=60(0011 1100)
A >> 2 will give 15 which is
1111
>>> (zero fill
right shift)
Shift right zero fill operator. The left
operands value is moved right by the
number of bits specified by the right
operand and shifted values are filled
up with zeros.
A=60(0011 1100)
A >>>2 will give 15 which
is 0000 1111
The Assignment Operators
Following are the assignment operators supported by Java language − a=1
Operator Description Example
=
Simple assignment operator. Assigns values from right side operands to
left side operand.
C = A + B
will assign
value of A
+ B into C
+=
Add AND assignment operator. It adds right operand to the left operand
and assign the result to left operand.
C += A is
equivalent
to C = C +
A
-=
Subtract AND assignment operator. It subtracts right operand from the left
operand and assign the result to left operand.
C -= A is
equivalent
to C = C –
A
*=
Multiply AND assignment operator. It multiplies right operand with the left
operand and assign the result to left operand.
C *= A is
equivalent
to C = C *
A
/= Divide AND assignment operator. It divides left operand with the right
operand and assign the result to left operand.
C /= A is
equivalent
to C = C /
A
%=
Modulus AND assignment operator. It takes modulus using two operands
and assign the result to left operand.
C %= A is
equivalent
to C = C
% A
<<= Left shift AND assignment operator.
C <<= 2
is same
as C = C
<< 2
>>= Right shift AND assignment operator.
C >>= 2
is same
as C = C
>> 2
&= Bitwise AND assignment operator.
C &= 2 is
same as
C = C & 2
^= bitwise exclusive OR and assignment operator. C ^= 2 is
same as
C = C ^ 2
|=
bitwise inclusive OR and assignment operator. C |= 2 is
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;
}
PrecedenceofJava 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
Postfix expression++ expression-- Left to right
Unary ++expression –-expression +expression –expression ~
!
Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> >>> Left to right
Relational < > <= >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= ^= |= <<= >>= >>>= Right to left

Weitere ähnliche Inhalte

Was ist angesagt?

Chapter 5-programming
Chapter 5-programmingChapter 5-programming
Chapter 5-programmingAten Kecik
 
SD & D Types of programming language
SD & D Types of programming languageSD & D Types of programming language
SD & D Types of programming languageForrester High School
 
Cmp2412 programming principles
Cmp2412 programming principlesCmp2412 programming principles
Cmp2412 programming principlesNIKANOR THOMAS
 
Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Krishna Sai
 
Ch1 language design issue
Ch1 language design issueCh1 language design issue
Ch1 language design issueJigisha Pandya
 
Coding vs programming
Coding vs programmingCoding vs programming
Coding vs programmingAman Kumar
 
Programming paradigm and web programming
Programming paradigm and web programmingProgramming paradigm and web programming
Programming paradigm and web programmingMohammad Kamrul Hasan
 
La5 ict-topic-5-programming
La5 ict-topic-5-programmingLa5 ict-topic-5-programming
La5 ict-topic-5-programmingKak Yong
 
Programing paradigm &amp; implementation
Programing paradigm &amp; implementationPrograming paradigm &amp; implementation
Programing paradigm &amp; implementationBilal Maqbool ツ
 
Language translator
Language translatorLanguage translator
Language translatorSumitSumit26
 
Basic programming concepts
Basic programming conceptsBasic programming concepts
Basic programming conceptssalmankhan570
 

Was ist angesagt? (17)

Chapter 5-programming
Chapter 5-programmingChapter 5-programming
Chapter 5-programming
 
SD & D Types of programming language
SD & D Types of programming languageSD & D Types of programming language
SD & D Types of programming language
 
Cmp2412 programming principles
Cmp2412 programming principlesCmp2412 programming principles
Cmp2412 programming principles
 
Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-Principles of-programming-languages-lecture-notes-
Principles of-programming-languages-lecture-notes-
 
Ch1 language design issue
Ch1 language design issueCh1 language design issue
Ch1 language design issue
 
Coding vs programming
Coding vs programmingCoding vs programming
Coding vs programming
 
Programming paradigm and web programming
Programming paradigm and web programmingProgramming paradigm and web programming
Programming paradigm and web programming
 
La5 ict-topic-5-programming
La5 ict-topic-5-programmingLa5 ict-topic-5-programming
La5 ict-topic-5-programming
 
Introduction to programming languages part 1
Introduction to programming languages   part 1Introduction to programming languages   part 1
Introduction to programming languages part 1
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Programing paradigm &amp; implementation
Programing paradigm &amp; implementationPrograming paradigm &amp; implementation
Programing paradigm &amp; implementation
 
Ict topic 5
Ict topic 5Ict topic 5
Ict topic 5
 
Notacd07
Notacd07Notacd07
Notacd07
 
Language translator
Language translatorLanguage translator
Language translator
 
Notacd071
Notacd071Notacd071
Notacd071
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Basic programming concepts
Basic programming conceptsBasic programming concepts
Basic programming concepts
 

Ähnlich wie Unit 1

ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.docICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.docAmanGunner
 
Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - IntroductionMadishetty Prathibha
 
Ppt about programming in methodology
Ppt about programming in methodology Ppt about programming in methodology
Ppt about programming in methodology Vaishnavirakshe2
 
Computer Science Is The Study Of Principals And How The...
Computer Science Is The Study Of Principals And How The...Computer Science Is The Study Of Principals And How The...
Computer Science Is The Study Of Principals And How The...Laura Martin
 
Specification Of The Programming Language Of Java
Specification Of The Programming Language Of JavaSpecification Of The Programming Language Of Java
Specification Of The Programming Language Of JavaKim Moore
 
PCCF UNIT 2.pptx
PCCF UNIT 2.pptxPCCF UNIT 2.pptx
PCCF UNIT 2.pptxDivyaKS12
 
Top Programming Languages of 2020
Top Programming Languages of 2020Top Programming Languages of 2020
Top Programming Languages of 2020Ikbal Ahmed
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxprakashvs7
 
Procedural Programming Of Programming Languages
Procedural Programming Of Programming LanguagesProcedural Programming Of Programming Languages
Procedural Programming Of Programming LanguagesTammy Moncrief
 
Grade 8: Introduction To Java
Grade 8: Introduction To JavaGrade 8: Introduction To Java
Grade 8: Introduction To Javanandanrocker
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
PCCF UNIT 2 CLASS.pptx
PCCF UNIT 2 CLASS.pptxPCCF UNIT 2 CLASS.pptx
PCCF UNIT 2 CLASS.pptxvishnupriyapm4
 
Unit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptxUnit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptxshashiden1
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to javaKalai Selvi
 

Ähnlich wie Unit 1 (20)

ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.docICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
 
Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - Introduction
 
OOP Java
OOP JavaOOP Java
OOP Java
 
Ppt about programming in methodology
Ppt about programming in methodology Ppt about programming in methodology
Ppt about programming in methodology
 
Computer Science Is The Study Of Principals And How The...
Computer Science Is The Study Of Principals And How The...Computer Science Is The Study Of Principals And How The...
Computer Science Is The Study Of Principals And How The...
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Specification Of The Programming Language Of Java
Specification Of The Programming Language Of JavaSpecification Of The Programming Language Of Java
Specification Of The Programming Language Of Java
 
PCCF UNIT 2.pptx
PCCF UNIT 2.pptxPCCF UNIT 2.pptx
PCCF UNIT 2.pptx
 
Top Programming Languages of 2020
Top Programming Languages of 2020Top Programming Languages of 2020
Top Programming Languages of 2020
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docx
 
Procedural Programming Of Programming Languages
Procedural Programming Of Programming LanguagesProcedural Programming Of Programming Languages
Procedural Programming Of Programming Languages
 
Grade 8: Introduction To Java
Grade 8: Introduction To JavaGrade 8: Introduction To Java
Grade 8: Introduction To Java
 
Java Programming Basics
Java Programming BasicsJava Programming Basics
Java Programming Basics
 
Ic lecture8
Ic lecture8 Ic lecture8
Ic lecture8
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
PCCF UNIT 2 CLASS.pptx
PCCF UNIT 2 CLASS.pptxPCCF UNIT 2 CLASS.pptx
PCCF UNIT 2 CLASS.pptx
 
Java
JavaJava
Java
 
Unit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptxUnit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptx
 
Introduction to java
Introduction to  javaIntroduction to  java
Introduction to java
 

Kürzlich hochgeladen

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 

Kürzlich hochgeladen (20)

Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 

Unit 1

  • 1. Unit 1 Topics:  Introduction: Programming language Types and Paradigms,  Computer Programming Hierarchy  Features of Java Language  JVM –The heart of Java, Java’s Magic Bytecode.  The Java Environment: Installing Java, Java Program Development, Java Source File, Structure, Compilation, Executions.  Basic Language Elements: Lexical Tokens, Identifiers, Keywords, Literals, Comments, Primitive Data types, Operators Assignments. Introduction of Programming Paradigms Paradigm can also be termed as method to solve some problem or do some task. Programming paradigm is an approach to solve problem using some programming language. 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:
  • 2. 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. Examples of Procedural programming paradigm: C ,C++ ,Pascal, etc. B) Object oriented programming – The program is written as a collection of classes and object which are meant for communication. The smallest and basic entity is object and all kind of computation is performed on the objects only. More emphasis is on data rather procedure. It can handle almost all kind of real life problems which are today in scenario. Advantages:  Data security  Inheritance  Code reusability  Flexible and abstraction is also present Examples: C++ , JAVA, Python etc. C) Parallel processing approach – Parallel processing is the processing of program instructions by
  • 3. dividing them among multiple processors. A parallel processing system posses many numbers of processor with the objective of running a program in less time by dividing them. This approach seems to be like divide and conquer. Examples are NESL (one of the oldest one) and C/C++ also supports because of some library function. 2. Declarative programming paradigm: It is divided as Logic, Functional, and Database. In computer science the declarative programming is a style of building programs that expresses logic of computation without talking about its control flow. It often considers programs as theories of some logic. It may simplify writing parallel programs. The focus is on what needs to be done rather how it should be done basically emphasizing on what code is actually doing. It just declares the result we want rather how it has be produced. This is the only difference between imperative (how to do) and declarative (what to do) programming paradigms. Getting into deeper we would see logic, functional and database. 1. Logic programming paradigms – It can be termed as abstract model of computation. It would solve logical problems like puzzles, series etc. In logic programming we have a knowledge base which we know before and along with the question and knowledge base which is given to machine, it produces result. In normal programming languages, such concept of knowledge base is not available but while using the concept of artificial intelligence, machine learning we have some models like Perception model which is using the same mechanism. In logical programming the main emphasize is on knowledge base and the problem. The execution of the program is very much like proof of mathematical statement, e.g., Prolog 2. Functional programming paradigms – The functional programming paradigms has its roots in mathematics and it is language independent. The key principal of this paradigm is the execution of series of mathematical functions. The central model for the abstraction is the function which is meant for some specific computation and not the data structure. Data are loosely coupled to functions. The function hides their implementation. Function can be replaced with their values without changing the meaning of the program. Some of the languages like Perl, JavaScript mostly uses this paradigm. Examples of Functional programming paradigm: JavaScript, Scala ,Lisp, etc.
  • 4. The next kind of approach is of Database. 3. Database/Data driven programming approach – This programming methodology is based on data and its movement. Program statements are defined by data rather than hard-coding a series of steps. A database program is the heart of a business information system and provides file creation, data entry, update, query and reporting functions. There are several programming languages that are developed mostly for database application. For example SQL. It is applied to streams of structured data, for filtering, transforming, aggregating (such as computing statistics), or calling other programs. So it has its own wide application. Computer Programming Hierarchy  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
  • 5.  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. Overview of Language Hierarchy: Features of Java The primary objective of Java programming language creation was to make it portable, simple and secure programming language. Apart from this, there are also
  • 6. some excellent features which play an important role in the popularity of this language. A list of most important features of Java language is given below. 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 Java is an object-oriented programming language. Everything in Java is an object. Object-oriented means we organize our software as a combination of different types of objects that incorporates both data and behavior. Object-oriented programming (OOPs) is a methodology that simplifies software development and maintenance by providing some rules. Basic concepts of OOPs are:
  • 7. 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 Collectionof objects is called class. It is a logical entity. Inheritance When one objectacquires all the properties and behaviors of parent object i.e. known as inheritance. It provides codereusability. 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. In C++, we use Function overloading and Function overriding to achieve polymorphism. Abstraction Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don'tknow the internal processing. In C++, we use abstract class and interface to achieve abstraction. Encapsulation Binding (or wrapping) code and data togetherinto a single unit is known as encapsulation. Forexample: capsule, it is wrapped with different medicines. Platform Independent: Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. A platform is the hardware or software environment in which a program runs.
  • 8. There are two types of platforms software-based and hardware-based. Java provides a software-based platform. 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.
  • 9. Robust Robust simply means strong. Java is robust because: o It uses strong memory management. o There is a lack of pointers that avoids security problems. o There is automatic garbage collection in java which runs on the Java Virtual Machine to get rid of objects which are not being used by a Java application anymore. o There are exception handling and the type checking mechanism in Java. All these points make Java robust. Architecture-neutral Java is architecture neutral because there are no implementation dependent features, for example, the size of primitive types is fixed. In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for 64-bit architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.
  • 10. Portable Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't require any implementation. High-performance Java is faster than other traditional interpreted programming languages because Java byte code is "close" to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc. Distributed Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the internet. Multi-threaded A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc. Dynamic Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++. Java supports dynamic compilation and automatic memory management (garbage collection). JVM JVM (Java Virtual Machine) is an abstract machine.
  • 11. It is called a virtual machine because it doesn'tphysically exist. It is a specification that provides a runtime environment in which Java byte codecan be executed. It can also run those programs which are written in other languages and compiled to Java byte code. 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 JVM provides definitions for the: 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.
  • 12. 1) Classloader Classloader is a subsystemof JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java. 1. BootstrapClassLoader: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
  • 13. 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 codefor methods. 3) Heap It is the runtime data area in which objects are allocated. 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) ProgramCounter Register PC (program counter) register contains the address of the Java virtual machine instruction currently being executed. 6) Native Method Stack It contains all the native methods used in the application. 7) Execution Engine It contains:
  • 14. 1. A virtual processor 2. Interpreter: Read bytecodestream then execute the instructions. 3. Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte codethat 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. 8) Java Native Interface Java Native Interface (JNI) is a framework which provides an interface to communicate with another application written in another language like C, C++, Assembly etc. Java uses JNI framework to send output to the Consoleor interact with OS libraries. 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. The implementation of JVM is also actively released by other companies besides Sun Micro Systems.
  • 15. JDK 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: o Standard Edition Java Platform o Enterprise Edition Java Platform o Micro Edition Java Platform 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.
  • 16. Java Platforms / Editions There are 4 platforms or editions of Java: 1) Java SE (Java Standard Edition) It is a Java programming platform. It includes Java programming APIs such as java.lang, java.io, java.net, java.util, java.sql, java.math etc. It includes coretopics like OOPs, String, Regex, Exception, Inner classes, Multithreading, I/O Stream, Networking, AWT, Swing, Reflection, Collection, etc. 2) Java EE (Java Enterprise Edition) It is an enterprise platform which is mainly used to develop web and enterprise applications. It is built on the top of the Java SE platform. It includes topics like Servlet, JSP, Web Services, EJB, JPA, etc. 3) Java ME (Java Micro Edition) It is a micro platform which is mainly used to develop mobile applications. 4) JavaFX It is used to develop rich internet applications. It uses a light-weight user interface API.
  • 17. Local Environment Setup 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 Filesjavajdk 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:WINDOWSSYSTEM32', then change your path to read 'C:WINDOWSSYSTEM32;c:Program Filesjavajdkbin'. 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 Compilation and Execution of a Java Program Java, being a platform independent programming language, doesn’twork on one- step-compilation. Instead, it involves a two-step execution, first through an OS independent compiler; and second, in a virtual machine (JVM) which is custom- built for every operating system The two principle stages are explained below:
  • 18. Compilation javac a.java First, the source‘a.java’ file is passed through the compiler, which then encodes the source codeinto a machine independent encoding, known as Bytecode. The content of each class contained in the source file is stored in a separate ‘.class’ file. Execution java a The class files generated by the compiler are independent of the machine or the OS, which allows them to be run on any system. To run, the main class file (the class that contains the method main) is passed to the JVM, and then goes through three main stages before the final machine codeis executed. These stages are: Class Loader The main class is loaded into the memory by passing its ‘.class’ file to the JVM, through invoking the latter. All the other classes referenced in the program are loaded through the class loader. A class loader, itself an object, creates a flat name space of class bodies that are referenced by a string name. 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:  Variables are initialized before they are used.  Method calls match the types of object references.  Rules for accessing private data and methods are not violated.  Local variable accesses fall within the runtime stack.  The run time stack does not overflow. If any of the above checks fails, the verifier doesn’tallow the class to be loaded. Just-In-Time Compiler This is the final stage encountered by the java program, and its job is to convert the loaded bytecode into machine code. The process canbe well-illustrated by the following diagram:
  • 19. Due to the two-step execution process described above, a java program is independent of the target operating system. However, because of the same, the execution time is way more than a similar program written in a compiled platform- dependent program. Java lexical structure Computer languages, like human languages, have a lexical structure. A source code of a Java program consists of tokens. Tokens are atomic code elements. In Java we have comments, identifiers, literals, operators, separators, and keywords. Java programs are composed of characters from the Unicode character set.
  • 20. Java comments Comments are used by humans to clarify source code. There are three types of comments in Java. Comment type Meaning // comment Single-line comments /* comment */ Multi-line comments /** documentation */ Documentation comments 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. The program uses two types of comments. // Program starts here This is an example of a single-line comment. Comments are ignored by the Java compiler. /* This is Comments.java */ Comments cannot be nested. The above code does not compile. Java white space 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;
  • 21. 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 names for variables, methods, classes, or parameters. Identifiers can have alphanumerical characters, underscores and dollar signs ($). It is an error to begin a variable name with a number. White space in names is not permitted. 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 name23; int a, A; int _col; short car_age; These are valid Java identifiers. String 23name; int %col; short car age; These are invalid Java identifiers. Java literals(constants) A literal is a textual representation of a particular value of a type. Literal types include Boolean, integer, floating point, string, null, or character. Technically, a literal will be assigned a value at compile time, while a variable will be assigned at runtime. int age = 29; String nationality = "Hungarian";
  • 22. Here we assign two literals to variables. Number 29 and string "Hungarian" are literals. In the above example, we have several literal values. 23 is an integer literal. "James" is a string literal. The true is a boolean literal. The null is a literal that represents a missing value. 68.5 is a floating point literal. 'J' is a character literal. Java separators A separator is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data stream. [ ] ( ) { } , ; . " 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"); Parentheses (round brackets) always follow a method name. Between the parentheses we declare the input parameters. The parentheses are present even if the method does not take any parameters. The System.out.println() method takes one parameter, a string value. The dot character separates the class name (System) from the member (out) and the member from the method name (println()). int[] array = new int[5] { 1, 2, 3, 4, 5 }; 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(); The comma character separates variables in a single declaration.
  • 23. Java keywords A keyword is a reserved word in Java language. Keywords are used to perform a specific task in the computer program. For example, to define variables, do repetitive tasks or perform logical operations. Java is rich in keywords. Many of them will be explained in this tutorial. abstract continue for new switch assert default goto package synchronized boolean do if private this break double implements protected throw byte else import public throws case enum instanceof return transient catch extends int short try char final interface static var class finally long strictfp void const float native super volatile while The package, public, class, static, void, int, for tokens are Java keywords. Java conventions Conventions are best practices followed by programmers when writing source code. Each language can have its own set of conventions. Conventions are not strict rules; they are merely recommendations for writing good quality code. We mention a few conventions that are recognized by Java programmers. (And often by other programmers too).  Class names begin with an uppercase letter.  Method names begin with a lowercase letter.  The public keyword precedes the static keyword when both are used.  The parameter name of the main() method is called args.  Constants are written in uppercase.  Each subsequent word in an identifier name begins with a capital letter. Data Types in Java Data types specify the different sizes and values that can be stored in the variable. There are two types of data types in Java:
  • 24. 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. Java Primitive Data Types In Java language, primitive data types are the building blocks of data manipulation. These are the most basic data types available in Java language. Java is a statically-typed programming language. It means, all variables must be declared before its use. That is why we need to declare variable's type and name. There are 8 types of primitive data types: o Boolean data type o byte data type o char data type o short data type o int data type o long data type o float data type o double data type
  • 25. short a; 2^16 (65,536) -32,768 to 32,767 byte 2^8 BooleanData Type 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. Example: Boolean one = false Byte Data Type 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 spacebecausea byte is 4 times smaller than an integer. It can also be used in place of "int" data type. Data Type Default Value Default size boolean False(0), true(1) 1 bit char 'u0000' 2 byte byte 0 1 byte short 0 2 byte int 0 4 byte long 0L 8 byte float 0.0f 4 byte double 0.0d 8 byte
  • 26. Example: byte a = 10, byte b = -20 Short Data Type The short data type is a 16-bit signed two's complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0. 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. Example: short s = 10000, short r = -5000 Int Data Type 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. Example: int a = 100000, int b = -200000 Long Data Type 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. Example: long a = 100000L, long b = -200000L FloatData Type 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.
  • 27. Example: float f1 = 234.5f Double Data Type 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. Example: double d1 = 12.3 Char Data Type 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. Example: char letterA = 'A' Unicode System Unicode is a universal international standard character encoding that is capable of representing most of the world's written languages. Why java uses Unicode System? Before Unicode, there were many language standards: o ASCII (American Standard Codefor Information Interchange) for the United States. o ISO 8859-1 forWestern European Language. o KOI-8 for Russian. o GB18030 andBIG-5 for chinese, and so on. This causedtwo problems: 1. A particular codevalue corresponds to different letters in the various language standards. 2. The encodings for languages with large character sets have variable length. Some
  • 28. 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. lowestvalue:u0000 highest value:uFFFF Java - Basic Operators Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups −  Arithmetic Operators  Relational Operators  Bitwise Operators  Logical Operators  Assignment Operators  Misc Operators The Arithmetic 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 − Assume integer variable A holds 10 and variable B holds 20, then − Operator Description Example + (Addition) Adds values on either side of the operator. A + B will give 30
  • 29. - (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -10 * (Multiplication) Multiplies values on either side of the operator. A * B will give 200 / (Division) Divides left-hand operand by right-hand operand. B / A will give 2 % (Modulus) Divides left-hand operand by right-hand operand and returns remainder 4%5= 4, 1%5=1, 5%5=0 B % A will give 0 ++ (Increment) (unary) Increases the value of operand by 1. ++B;, B++ C=++B+B++; C=22+20=42 B++ gives 21 --(Decrement) (unary) Decreases the value of operand by 1. –B, B-- B-- gives 19 The RelationalOperators There are following relational operators supported by Java language. Assume variable A holds 10 and variable B holds 20, then − Operator Description Example == (equal to) Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != (not equal to) Checks if the values of two operands are equal or (A != B) is
  • 30. not, if values are not equal then condition becomes true. true. > (greater than) Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < (less than) Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. A=2, B=4 C=A>B(0) If(A<B) (A < B) is true. >= (greater than or equal to) Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= (less than or equal to) Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true. The Bitwise Operators Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte. Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60 and b = 13; now in binary format they will be as follows − a = 0011 1100
  • 31. b = 0000 1101 ----------------- a&b = 0000 1100 AND a|b = 0011 1101 OR | a^b = 0011 0001 XOR^ ~a = 1100 0011 NOT The following table lists the bitwise operators − Assume integer variable A holds 60 and variable B holds 13 then − Operator Description Example & (bitwise and) Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 | (bitwise or) Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101 ^ (bitwise XOR) Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ~ (bitwise compliment) Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. << (left shift) Binary Left Shift Operator. The left A=60(0011 1100)
  • 32. operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000 01111000(120) >> (right shift) Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A=60(0011 1100) A >> 2 will give 15 which is 1111 >>> (zero fill right shift) Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros. A=60(0011 1100) A >>>2 will give 15 which is 0000 1111 The Assignment Operators Following are the assignment operators supported by Java language − a=1 Operator Description Example = Simple assignment operator. Assigns values from right side operands to left side operand. C = A + B will assign value of A + B into C += Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A
  • 33. -= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C – A *= Multiply AND assignment operator. It multiplies right operand with the left operand and assign the result to left operand. C *= A is equivalent to C = C * A /= Divide AND assignment operator. It divides left operand with the right operand and assign the result to left operand. C /= A is equivalent to C = C / A %= Modulus AND assignment operator. It takes modulus using two operands and assign the result to left operand. C %= A is equivalent to C = C % A <<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator. C &= 2 is same as C = C & 2 ^= bitwise exclusive OR and assignment operator. C ^= 2 is same as
  • 34. C = C ^ 2 |= bitwise inclusive OR and assignment operator. C |= 2 is 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; } PrecedenceofJava 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.
  • 35. 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 Postfix expression++ expression-- Left to right Unary ++expression –-expression +expression –expression ~ ! Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> >>> Left to right Relational < > <= >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right
  • 36. Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %= ^= |= <<= >>= >>>= Right to left