Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Java

The document discusses the goals and characteristics of the Java programming language. It was created to be object-oriented, platform independent, simple, secure, portable, robust, multithreaded, and high performance.

Uploaded by

Shivvu Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Java

The document discusses the goals and characteristics of the Java programming language. It was created to be object-oriented, platform independent, simple, secure, portable, robust, multithreaded, and high performance.

Uploaded by

Shivvu Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Five primary goals to create Java language:

i. to use the object oriented methodology;

ii. to allow same program to be executed on multiple operating

systems (i.e. “Write Once, Run Anywhere”, with free runtimes on

popular platforms);

iii. to contain built-in support for using computer networks, so as to

allow for network access to be limited;

iv. to be designed to execute code from remote sources securely, with

configurable security;

v. to incorporate good features of other object oriented languages.

• Characteristics of Java:

i. purely object oriented;

ii. simple and robust;

iii. safe and secure;

iv. architecture-neutral and portable;

v. interpreted and high performance;

vi. multithreaded;

vii. distributed and dynamic.

Following are the notable features of Java:


Object Oriented
In Java, everything is an Object. Java can be easily extended since it is based on the Object
model.
Platform Independent
Unlike many other programming languages including C and C++, when Java is compiled, it
is not compiled into platform specific machine, rather into platform-independent byte code.
This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on
whichever platform it is being run on.
Simple
Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it
would be easy to master.
Secure
With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.
Architecture-neutral
Java compiler generates an architecture-neutral object file format, which makes the compiled
code executable on many processors, with the presence of Java runtime system.
Portable
Being architecture-neutral and having no implementation dependent aspects of the
specification makes Java portable. The compiler in Java is written in ANSI C with a clean
portability boundary, which is a POSIX subset.
Robust
Java makes an effort to eliminate error-prone situations by emphasizing mainly on compile
time error checking and runtime checking.
Multithreaded
With Java's multithreaded feature it is possible to write programs that can perform many
tasks simultaneously. This design feature allows the developers to construct interactive
applications that can run smoothly.
Interpreted
Java byte code is translated on the fly to native machine instructions and is not stored
anywhere. The development process is more rapid and analytical since the linking is an
incremental and light-weight process.
High Performance
With the use of Just-In-Time compilers, Java enables high performance.
Distributed
Java is designed for the distributed environment of the internet.
Dynamic
Java is considered to be more dynamic than C or C++ since it is designed to adapt to an
evolving environment. Java programs can carry an extensive amount of run-time information
that can be used to verify and resolve accesses to objects at run-time.

• Major editions of Java (for different applications and systems):

1. Java card: for smart cards.

2. Java Micro Edition (JME): for systems with limited resources (like

mobile phones).

3. Java Standard Edition (Java SE): for workstations and desktops.

4. Java Enterprise Edition (JEE): for large distributed Internet

environment.

5. JavaFX: for Rich Internet Applications (RIAs).

[No standard full form of FX]


• Java Applications: standalone software application to run on its

own without needing to be embedded in particular host

environment (such as web browser).

• Standalone applications only needing JVM to execute.

• Two forms ― command-line applications, graphical user interface

(GUI) applications.

• Command-line application: to use command prompt for input/output.

• GUI application: to use graphical components to facilitate input and

output of the program.

• Java Applets: software application typically embedded in HTML

page and run by client web browser.

• Applet to run in sandbox (confined space in memory) guaranteeing

their secured execution.

• Applets to be defined in HTML by <applet> tag:

• In past, applets typically used for graphics and rich interfaces.

• Nowadays, applet support removed by Java SE 11, and replaced by

JavaScript and HTML 5.

• “main()” method: starting of execution of all Java applications by

calling main().

• E.g.: public static void main (String args[]) {...}

• public: access modifier; main() mandatorily be declared as public,

to allow its call from outside its class on start of program execution.

• static: to allow call to main() before creating object of its class;

necessary because JVM to call main() before objects being made.

• void: to allow no return of value on call to main().

• String args[]: parameter in main(), declared as array of String

objects; to receive command-line argument(s) at start of execution.

• Java control block: block of control statements to cause flow of


execution to advance and branch based on changes to state of

program.

• E.g.: selection, iteration, jump etc.

• Selection statements: to allow program to choose different paths of

execution based upon outcome of expression or state of variable,

known only during run time.

• E.g. Java to support two selection statements: if, switch.

Java control block:

• Selection statements: E.g. if, switch.

• Iteration statements: E.g. while, do-while, for, for-each.

• Jump statements: to allow program to execute in nonlinear fashion.

• Jump statements to transfer control to another part of program.

• E.g. break, continue, return.

Array: collection of variables of same type, referred by name.

• Main benefit: data organization for easy manipulation.

• One or more dimensions possible in Java arrays.

• Syntax to declare one-dimensional array:

<datatype> <array-name>[] = new <datatype>[<size>];

• new: operator for dynamic allocation of array.

• size: number of elements in array.

• Two-step process in array creation  (i) to declare array reference

variable, (ii) to allocate memory for array and to assign reference to

that memory to array reference variable.

Java class: comprising of members of class defining shape (i.e.

structured data) and nature (i.e. behaviours).

(purpose: to represent group of objects of similar shape, nature).

where, access-modifier = access control measure to manage


access of members of class by different program statements.

• Access control measures: private, public, protected, default;

to restrict scope of class, member (field/method/constructor) of class.

• Benefit: to hide complexity of implementation inside class.

Private,protected and public difference

• Java class contd.

• Constructor: member of class, used to initialize object(s) created

from that class.

• Constructor syntactically similar to method; no explicit return type.

• Constructor name → same name as its class.

• Syntax of constructor definition:

<access-modifier> <class-name>([<datatype1> <param1>,

<datatype2> <param2>,...])

{ ..

... }

• Recursion: mechanism of defining something in terms of itself.

• In Java, recursion to be attribute allowing method to invoke itself.

• Recursive method: method invoking itself.

• With each recursive invocation, new local variables and parameters

to be allocated storage on stack, and method body to be executed

from start with these variables.

• On recursive call, old local variables and parameters to be removed

from stack, and execution to resume at point of invocation inside

method.

Non access modifier: keywords, for not controlling access level,

but notifying JVM about behavior of class and its members, as


well as synchronizing execution flow and handling display of

similar outcomes irrespective of different platforms for execution.

Static keyword,wrapper class

• Boxing: conversion of primitive types to their corresponding

wrapper classes, for encapsulating values within wrapper objects.

• Syntax of boxing:

<wrapper> <variable-name> = new <wrapper>(...);

• E.g. Double testObj = new Double(5.022);

• Unboxing: conversion of wrapper classes to their corresponding

primitive types, for extracting values from wrapper objects.

• Syntax of unboxing:

<datatype> <var-name> = <wrapper>.<method-name>();

• E.g. double test = testObj.doubleValue();

• Autoboxing: automatic conversion (made by Java compiler)

between primitive types and their corresponding wrapper classes.

• Autoboxing into its equivalent wrapper class, whenever object of that

wrapper type needed, without explicitly constructing that object

• Syntax of autoboxing (for simple case of assignments):

<wrapper> <var-name> = <prim-var-name1>;

<wrapper> <var-name> = <value1>;

[Note: object not explicitly created through keyword new.]

• Auto-unboxing: process by which value of boxed object to be

automatically extracted (i.e. unboxed) from wrapper class, when

its value needed.

• Syntax of auto-unboxing (for simple case of assignments):

<datatype> <prim-var-name1> = <wrapper-object1>;

• Syntax of auto-unboxing (for value returned by method):

...declaration... <wrapper1> <method-name1>(...){


<wrapper1-obj1> = ......;

return <wrapper1-obj1>; }

<datatype1> <var-name1> = <method-name1>(...);

Method overloading: allowing two/more methods within same

class in Java to share same method name, only for their different

parameter declarations.

• Such methods said to be overloaded.

• Important restriction: type and/or parameters count in each

overloaded method MUST differ.

• Overloaded methods may differ in their return types.

• On invoke of overloaded method, version of that method, where

match of parameters and arguments success, to be executed.

Variable-length arguments (termed as varargs) in method: to

allow method to take variable number of arguments during call.

• Such methods referred as variable-arity method, or simply varargs

method.

• Benefit: to support method call, when maximum number of potential

arguments of method larger, or unknowable.

Java StringBuffer and StringBuilder: Differences

Object Oriented Programming

• StringBuilder: no guarantee of synchronization.

StringBuffer: methods synchronized, so that operations on

any particular instance to behave ‘occurring in serial order’ and

consistent with order of method calls made by each individual

thread on that instance.

• StringBuilder: not safe for threaded applications.

StringBuffer: safe to use in multi-threaded applications.


• StringBuilder more efficient (and faster under most

implementations) than StringBuffer.

Dept. of CSE, NITP

11

Dr. Suddhasil De

Java StringBuffer and StringBuilder: Differences (contd.)

Object Oriented Programming

• StringBuilder: introduced in Java version 1.5.

StringBuffer: available since Java version 1.0.

Java inheritance

• Java inheritance: mechanism of creating new class, reusing

properties (i.e. fields and methods) of existing class.

• Superclass (also called base class or parent class): existing class,

whose properties being reused during inheritance.

• Subclass (also called derived class or child class): new class,

created during inheritance.

• Subclass inheriting capabilities of superclass, with possibility to add

own embellishments/refinements, without changing superclass.

Benefits of Java inheritance:

a. code reusability, to 

(i) save time and money,

(ii) increase program’s reliability (by reusing already debugged and

tested code), and

(iii) ease in distributing class libraries;

b. to help in original conceptualization of programming problem;

c. to help in overall design of program.


• Method overriding: mechanism in inheritance to override method

in superclass by another method in subclass with same name

and type signature as method in superclass.

• On invoke of overridden method from within subclass, version of

method defined by subclass to be called, and version of method

defined by superclass to be hidden.

• Access to superclass version of overridden method by using super.

• Method overriding to happen only when identical names and type

signatures of method in superclass and method in subclass.

• If same name but different type signature  method overloading.

You might also like