0% found this document useful (0 votes)
8 views90 pages

Introduction - Overview, Tokens, Variables

Introduction to java basic concepts

Uploaded by

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

Introduction - Overview, Tokens, Variables

Introduction to java basic concepts

Uploaded by

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

Object Oriented Principles,

Overview of Language, Tokens,


Datatypes and Variables
Object Oriented Programming (OOP)
• A Programming
paradigm that organizes
code into reusable
objects.
• Objects contains both
data and methods
Definition
• Object-oriented programming is an approach
that provides a way of modularizing
programs by creating partitioned memory
area for both data and functions that can be
used as templates for creating copies of such
modules on demand
OOP-Features
• Emphasis is on data rather than procedure.
• Programs are divided in to what are known as Object.
• Data structures are designed such that they characterize
the objects.
• Methods that operate on the data of an object are tied
together in the data structure.
• Data is hidden and cannot be accessed by external
functions.
• Objects may communicate with each other through
methods.
• New data and methods can be easily added whenever
necessary.
OOPs-Object Oriented Programming
System
Classes and Objects
Class Object
• The class represents a group • An object is an instance of a
of objects having similar class
properties and behavior, or • Real world entities that has
in other words, we can say their own properties and
that a class is a blueprint for behaviours.
objects
Data Abstraction
• Act of representing essential features without
including the background details or
explanations
• In Java, abstraction is achieved
by interfaces and abstract classes.
Encapsulation
• Wrapping up of data
under a single unit
• protective shield that
prevents the data from
being accessed by the
code outside this shield.
• Achieved by declaring all
the variables in a class as
private and writing public
methods in the class to
set and get the values of
the variables.
Inheritance
• One class is allowed to inherit the features
(fields and methods) of another class
• Uses extends keyword
• Supports the concept of "reusability"
Polymorphism
• Ability to take more than one form
• Refers to the ability of object-oriented
programming languages to differentiate
between entities with the same name
efficiently.
• Implemented by
– Method overloading
– Method overiding
Dynamic Binding
• Linking of a procedure call to the code to be
executed in response to the call
• The code associated with a procedure call is
known at run time
Message communication
• Objects communicate
with one another by
sending and receiving
information to each
other
• Message passing
involves specifying the
name of the object, the
name of the function,
and the information to
be sent.
Benefits of OOP
• Simplicity
• Modularity
• Modifiability
• Design Benefits
• Maintainability
• Re-Usability
• Security
Java
Java Milestones
Java Milestones
Java Milestones
Year Development
2006 Java SE 6 was released
2007 Java SE 7 was released
Features
• Simple and Familiar
• Compiled and Interpreted
• Platform Independent and Portable
• Object Oriented
• Robust and Secure
• Architectur-neutral
• Distributed
Features
• Multithreaded
• High Perfomance
• Dynamic
Java Platform
• Java Virtual Machine – JVM

• Java Application Program Interface – Java


API
JVM
• Runs Java applications as a run-time engine
• JVM is a part of JRE (Java Runtime
Environment)
• A virtual machine that executes Java
bytecode, providing a runtime environment
for Java applications to run on different
platforms
JVM
Compiling Java Code:
• When you write Java code and compile it, the Java compiler
generates bytecode, a set of instructions understood by the JVM.
JVM Execution:
• The JVM loads the bytecode, translates it into machine code
(specific to the underlying operating system), and executes the
program.
Memory Management:
• The JVM manages memory allocation, garbage collection (removing
unused memory), and other memory-related tasks.
Platform-Specific Details:
• The JVM adapts the bytecode execution to the specific operating
system it is running on, ensuring that the Java program runs
correctly.
Java Development Kit - JDK
• A collection of tools that are used for
developing and running Java programs.
• JDK is required to build and run Java
applications and applets.
Java Environment
• Java Development Kit – JDK

• Java Runtime Environment - JRE

• Application Programming Interface - API


Basic JDK Tools
• javac –
– Compiler for the Java programming language
– Used to compile .java file.
• java –
– The Java interpreter, which runs java programs
by reading & interpreting bytecode files.
– Once the class file has been created, the java
command can be used to run the Java
program.
• appletviewer
– run and debug applets without a web
browser
• jdb
– Java debugger
– Helps to find errors in the program
• Javah
– Produce C header files
• javadoc
– Creates HTML format documentation from
Java source code.
• Javap
– Disassembler
– Displays information about the fields,
constructrs, methods, parameters etc. in a
class
• jar
–Java archive
–A package file format that contains
class, text, images and sound files for a
Java application or applet gathered
into a single compressed file.
Java Runtime Environment - JRE
• Set of software tools for the development of
Java applications
• It Requires
– JVM: The heart of the JRE. It's the platform that
runs Java programs
– Runtime libraries: These are libraries that contain
pre-written code that Java programs can use, like
those for input/output, string manipulation, and
networking
JRE - Components
• Java Web Start and Java Plug-in.
• User Interface Toolkit, which includes Abstract Window
Toolkit (AWT), Swing, Image Input / Output, Accessibility,
drag, and drop, etc.
• Other different base libraries, including Input/Output,
extension mechanisms, beans, JMX, JNI, networking,
override mechanisms, etc.
• Lang and util base libraries which include lang and util,
management, versioning, collections, etc.
• Integration libraries, which includes Interface Definition
Language (IDL), Java Database Connectivity (JDBC), Java
Naming and Directory Interface (JNDI), Remote Method
Invocation (RMI).
Java API
• Set of predefined classes, interfaces, methods,
and packages that developers can use to build
applications in Java
– Language Support Package
– Utilities Package
– Input/Output Package
– Networking Package
– AWT Package
• Applet Package
• Swing Package
JVM
Types of Java Program
1. Stand-alone applications
– Refers to a Java program that can run
independently on a computer
– Steps involved
• Compile source code into byte code using Java
compiler
• Execute byte code using Java interpreter
2. Applets
– Java applications that run within a
web browser
– Mainly used for internet
programming
3. Servlets
– Runs with java servers
General Structure of Java Program
// a single line comment is declared like this
/* a multi-line comment is declared like this
and can have multiple lines as a comment */
/** a documentation comment starts with a delimiter and ends with */

• Documentation Session
– It is used to improve the readability of the
program.
– It consists of comments in Java
• Package Statement
– Used to group related classes and interfaces into a
single namespace
• Import Statement
– Used to bring classes and interfaces from other
packages into the current file
• import java.util.*
• Interface section
– Collection of abstract methods
• Class Definition
– It defines the information about the user-
defined classes in a program.
• Main Method Class
– The main method is from where the
execution actually starts
Example – Structure of Java Program
Main Method in Java
• 1. public: The public modifier makes it accessible from
anywhere in the application.
• 2. static: The static modifier makes it a class method so
that it can be called using the class name without
creating an object of the class.
• 3. void: The return type of the main method is void,
which means that it does not return a value to its caller.
We must specify void when you declare the main
method.
• 4. main: It is the name of a method where execution will
start. In Java, the main method is called by JVM.
• 5. String[ ] args: The main method accepts one argument of type
String array (String[ ]). The square brackets [ ] represent the array of
strings that are passed as an argument to this method. “args” is the
name of its parameter. We can use any parameter name as you
wish.
• 6. ({): This is an opening brace that marks the beginning of the main
method body. We must pair it with a closing brace.
• 7. Body of main Method: The region between the opening brace
and closing brace is called main method body that contains a set of
program statements. This region is a static region.
• 8. (}): This is a closing brace that marks the closing of the main
method body. It must be paired with an opening brace.
Java Character Set
• Characters used to write Java Tokens
• Defined by Unicode character set
16 bit character coding system
Supports more than 34,000 defined
charactres from 24 languages
Java Tokens
• The tokens are the small building blocks
of a Java program that are meaningful to
the Java compiler
• The Java compiler breaks the line of code
into text (words) is called Java tokens.
• These are the smallest element of the
Java program.
Java Tokens - Types
1. Reserved Keywords
2. Identifiers
3. Literals
4. Operators
5. Separators
1. Keywords
• Pre-defined
reserved words
• Each keyword has
a special meaning.
• It is always written
in lower case
Identifiers
• Used to name a variable, constant, function,
class, and array
• Usually defined by the user.
• Rules
 All identifiers should begin with a letter (A-Z or a-z),
currency character($), or an underscore(_)
 A keyword cannot used as identifier
 They are case sensitive
 Can be of any length
• Eg:- sum, $salary
Identifiers – Naming Conventions
• Name of all public methods and instance
variables start with a leading lowercase letter
eg: sum
• When more than one words are used in a name,
the second and subsequent word are marked
with a leading uppercase letter
eg: totalMarks
• All private and local variables use only lowercase
letters combined with underscores
eg:length, batch_strength
Identifiers – Naming Conventions
• All classes and interface start with
leading uppercase letter
eg: Student, HelloJava
• Variables that represent constant values
use all uppercase letter and underscore
between words
eg: TOTAL, PRINCIPAL_AMOUNT
Literals (Constants)
• Represents a fixed value (constant) in the source code
• Integer Literal
– Any integral literal
1. Decimal literal - Consists of digits 0-9
Eg: 125, -5
2. Octal Literal - Consists of digits 0-7
Eg: 342, 146
3. Hexadecimal Literal - Consists of digits 0-9 and
alphabets a-f
Eg: Ox345 [sequence of digits preceded by Ox or ox]
Literals
• Floating-Point Literals
– Represents decimal values with fractional
parts
• Standard notation :- 3.1456
• Scientific notation :- 2.1565e2
• Character Literal
– Contain single character enclosed in a pair
of single quotation mark
– Eg: ‘a’, ‘5’
Literals
• String Literal
– Sequence of charactes enclosed within double
quotes
– Eg: “welcome”
• Boolean Literal
– Only two values are allowed for Boolean
literals, i.e., true and false
• Null Literal – write as null
– A special value that represents the absence of
a value or a reference to an object
Literal
• Backslash
character
Constants
– consists of a
backslash
character (\)
followed by one
ore more other
characters
Operators
• Special symbol that tells the compiler to
perform a special operation.
• Types
->Arithmetic ->Relational
->Logical ->Assignment
->Conditional - Ternary ->Bitwise
->Increment/Decrement - Unary
->Special
Operators
• Based on number of operands,
operators in Java can be divided into
three types:
– Unary operator (works on single operand)
– Binary operator (works on two operands)
– Ternary operator (works on three
operands)
Arithmetic Operators – Construct
mathematical expressions
Relational Operator- check for
relations between two operands
Logical Operators
• Used to combine two or more
conditions/constraints or to complement the
evaluation of the original condition under
particular consideration
• AND Operator (&&): If( a && b ) [if true execute
else don't]
• OR Operator (||): If( a || b) [if one of them is
true to execute; else don't]
• NOT Operator (!): !(a<b) [returns false if a is
smaller than b]
Assignment Operator
• Used to assign values to
a variable
– a=10
• Short-hand operator
(+=)
– v op=exp
Increment/Decrement Operator
Conditional Operator
Bitwise Operator - Perform a bit
manipulation on numbers
Spercial Operators
• Instanceof Operator
– used to test whether an object is an
instance of a specific class or a subclass
– Syntax
• object instanceof ClassName
• Eg: person instanceof Student
is true if the object belongs to the class Student
otherwise false
Special Operators
• Dot Operator
– used to access the members of a class, such
as methods, variables, and nested classes
–Eg: object.age;
Operator Precedence & Associativity
• Operator Precedence
– A set of rules that defines the priority for all the
operators present in a programming language
• Associativity defines whether you should go
from left to right or you should go to right to
left
Separators
Data Types
Data Types
Variables
Variables
• A name given to a memory location.
• It is the basic unit of storage in a program.
• The value stored in a variable can be
changed during program execution.
• In Java, all the variables must be declared
before use.
• Syntax
– datatype variable = value;
Variables – Rules & Scope
• Rules
– Variables must not begin with a digit
– Case sensitive
– Should not be a keyword
– Whitespace is not allowed
– Can be of any length
• Scope
– Confined within the block in which it is
declared
Casting of Variables
Automatic Conversion - Widening
Explict Casting - Widening
Symbolic Constants
• A named constant value defined once and used
throughout a program
• Symbolic constants are declared using the final
keyword.
– Which indicates that the value cannot be changed once it
is initialized.
– The naming convention for symbolic constants is to use all
capital letters with underscores separating words.
• Syntax
– final data_type symbolic_name = value;
• Eg:
– final float PI = 3.14159
Types of Variables
Local Variables
• Declared inside a method, constructor, or
block.
• Accessible only within the method,
constructor, or block where they are declared.
• Their lifetime is limited to the time the
method or block is running.
• Must be initialized before use, as they do not
have a default value.
Local Variable
Instance Variables
• Declared inside a class but outside any
method.
• Associated with an instance of the class
(object).
• Each object of the class has its own copy of
the instance variable.
• Can be accessed using object references.
Instance Variable
Static Variables
• Associated with the class itself, not individual
objects.
• All instances of the class share the same static
variable.
• Can be accessed directly using the class name.
• Declared using static keyword
Static Variable
Comparison

You might also like