You are on page 1of 7

Java basics

Java Basics:

1. Java is a High level language


Java is called high level Language as it is of a human
readable/understandable format
Below, is a list of various level of programming languages.
a. Binary language
This is a machine language which comprise of instructions in the
format of 1s and 0s
b. Pneumonics
Assembly level language. Comprise instruction in the format of
pseudocodes.
c. HighLevel language
This language comprise of high level instruction, which is more
understandable by humans. Various components called compilers,
and interpreters are used to convert the high level language into
binary language
2. Java is Platform independent.
WORA Write Once Run Anywhere.

Compilation process:
Compilation verifies the syntax of the programming languages. Converts
the instruction into machine understandable format.

1
Java basics

3. Robust language:
Two main problems that cause program failures are memory management
mistakes and mishandled runtime errors. Java handles both of them
efficiently.
1) Memory management mistakes can be overcome by garbage collection.
Garbage collection is automatic de-allocation of objects which are no
longer needed.

Both stack and heap are set of memory locations.


Stack: Last In First Out
Heap: Random memory locations

2) Mishandled runtime errors are resolved by Exception Handling


procedures.
Achieved through Try-Catch-Finally mechanism.

4. Object oriented programming language


Object oriented programming is a way of organizing programs as
collection of objects, each of which represents an instance of a class.
Object oriented programming is a way of organizing programs as
collection of objects, each of which represents an instance of a class.

4 main concepts of Object Oriented programming are:

1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction

2
Java basics

Encapsulation:

Encapsulation is the process of compartmentalizing the elements of an


abstraction that constitute its structure and behavior ; encapsulation serves
to separate the contractual interface of an abstraction and its
implementation.

a) Hides the implementation details of a class.


b) Forces the user to use an interface to access data
c) Makes the code more maintainable.

Inheritance:
Inheritance is the process by which one object acquires the properties of
another object.
Inheritance allows a class to use the properties and methods of another
class. In other words, the derived class inherits the states and behaviors
from the base class. The derived class is also called subclass and the base
class is also known as super-class. The derived class can add its own
additional variables and methods. These additional variable and methods
differentiates the derived class from the base class.

Polymorphism:

Polymorphism is the capability of a method to do different things based on


the object that it is acting upon. In other words, polymorphism allows you
define one interface and have multiple implementations. I know it sounds
confusing. Dont worry we will discuss this in detail.

1) It is a feature that allows one interface to be used for a general


class of actions.
2) An operation may exhibit different behavior in different instances.
3) The behavior depends on the types of data used in the operation.

Following concepts demonstrate different types of polymorphism in java.


1) Method Overloading
2) Method Overriding

3
Java basics

Abstraction:

Note: 1) Abstract class can also have regular (or concrete) methods along
with abstract methods.
2) Abstract methods do not have body, they just have prototype (method
signature).
3) Abstract methods must be implemented in the child class (if the class is
not abstract) otherwise program will throw compilation error

Variable:

The Java programming language defines the following kinds of variables:

Instance Variables (Non-Static Fields) Technically speaking, objects store


their individual states in "non-static fields", that is, fields declared without
the static keyword. Non-static fields are also known as instance
variables because their values are unique to each instance of a class (to each
object, in other words); the current Speed of one bicycle is independent from
the current Speed of another.

Argument variables These are the variables that are defined in the header
oaf constructor or a method. The scope of these variables is the method or
constructor in which they are defined. The lifetime is limited to the time for which
the method keeps executing. Once the method finishes execution, these
variables are destroyed.

Local Variables Similar to how an object stores its state in fields, a method
will often store its temporary state in local variables. The syntax for declaring a
local variable is similar to declaring a field (for example, int count = 0;). There is
no special keyword designating a variable as local; that determination comes
entirely from the location in which the variable is declared which is between
the opening and closing braces of a method. As such, local variables are only
visible to the methods in which they are declared; they are not accessible from
the rest of the class.

4
Java basics

Data Types:

Summary of Data Types


Primitive Type Size Minimum Value Maximum Value Wrapper Type

Char 16-bit Unicode 0 Unicode 216-1 Character

Byte 8-bit -128 +127 Byte

Short 16-bit -215 +215-1 Short


(-32,768) (32,767)

Int 32-bit -231 +231-1 Integer


(-2,147,483,648) (2,147,483,647)

Long 64-bit -263 +263-1 Long


(9,223,372,036,854,775,807)
(-9,223,372,036,854,775,808)

Float 32-bit Approx range 1.4e-045 to 3.4e+038 Float

5
Java basics

Double 64-bit Approx range 4.9e-324 to 1.8e+308 Double

Boolean 1-bit true or false Boolean

Non primitive data types in java

Access Specifiers:

Access specifiers are used to control the visibility of members like classes, variables and
methods. There are three access specifiers: public, private and protected. Protected
access specifier comes into picture when inheritance is implemented.

Following table shows what access specifiers may be assigned to different elements.
Note that all four levels may be applied to all elements except classes. Classes may be
declared with only public and private access specifiers

public private protected < unspecified >


class allowed not allowed not allowed allowed

constructor allowed allowed allowed allowed

variable allowed allowed allowed allowed

method allowed allowed allowed allowed

6
Java basics

The following table summarises the above said points regarding the accessibility of
entities.

class subclass package outside


private allowed not allowed not allowed not allowed

protected allowed allowed allowed not allowed

public allowed allowed allowed allowed

< unspecified > allowed not allowed allowed not allowed