0% found this document useful (0 votes)
494 views

Java Theory

The document discusses features of the Java programming language including that it is object-oriented, platform independent, secure, robust, and architecture neutral. Java code is compiled to bytecode that can run on multiple platforms.

Uploaded by

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

Java Theory

The document discusses features of the Java programming language including that it is object-oriented, platform independent, secure, robust, and architecture neutral. Java code is compiled to bytecode that can run on multiple platforms.

Uploaded by

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

Java is an 

object-oriented, class-based, concurrent, secured and general-purpose computer-programming language. It is a


widely used robust technology.
OBJECT
Object is a basic run time entity of object oriented programming is an abstrct datatype created by developer. It can include
multiple properties and methods and even contain other objects. Objects are defined by classes. Object provide structural
approach to programming.

History of Java

James Gosling initiated Java language project in June 1991 for use in one of his many set-top box projects. The language,
initially called ‘Oak’ after an oak tree that stood outside Gosling's office, also went by the name ‘Green’ and ended up later
being renamed as Java, from a list of random words.
Sun released the first public implementation as Java 1.0 in 1995. It promised  Write Once, Run Anywhere (WORA), providing
no-cost run-times on popular platforms.
On 13 November, 2006, Sun released much of Java as free and open source software under the terms of the GNU General
Public License (GPL).
On 8 May, 2007, Sun finished the process, making all of Java's core code free and open-source, aside from a small portion of
code to which Sun did not hold the copyright.

Definition of OOP Concepts in Java

OOP concepts in Java are the main ideas behind Java’s Object Oriented Programming. They are
an abstraction, encapsulation, inheritance, and polymorphism. Grasping them is key to understanding how Java works.
Basically, Java OOP concepts let us create working methods and variables, then re-use all or part of them without
compromising security.

List of OOP Concepts in Java


Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code
reusability. It is used to achieve runtime polymorphism.

Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to
draw something, for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal
processing.
In Java, we use abstract class and interface to achieve abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is
wrapped with different medicines. A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here.
Abstract Classes and Methods

Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).

The abstract keyword is a non-access modifier, used for classes and methods:

 Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from
another class).
 Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the
subclass (inherited from).

Interfaces

Another way to achieve abstraction in Java, is with interfaces.

An interface is a completely "abstract class" that is used to group related methods with empty bodies:

To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with
the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class:

Java Polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another
class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different
ways.

For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be
Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows,
etc.):
13/4/2021

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. The features of Java are also known as java buzzwords.

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:
 Java syntax is based on C++ (so easier for programmers to learn it after C++).
 Java has removed many complicated and rarely-used features, for example, explicit pointers, operator
overloading, etc.
 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:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
 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 bytecode. This bytecode is a platform-independent code because it can
be run on multiple platforms, i.e., Write Once and Run Anywhere(WORA).

 Bytecode

Bytecode is program code that has been compiled from source code into low-level code designed for a software
interpreter. It may be executed by a virtual machine (such as a JVM) or further compiled into machine code, which is
recognized by the processor.

Different types of bytecode use different syntax, which can be read and executed by the corresponding virtual
machine. A popular example is Java bytecode, which is compiled from Java source code and can be run on a Java
Virtual Machine (JVM). Below are examples of Java bytecode instructions.

 new (create new object)


 aload_0 (load reference)
 istore (store integer value)
 ladd (add long value)
 swap (swap two values)
 areturn (return value from a function)

While it is possible to write bytecode directly, it is much more difficult than writing code in a high-level language, like
Java. Therefore, bytecode files, such as Java .CLASS files, are most often generated from source code using a
compiler, like javac.

 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 Bytecode 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.
o 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 bytecode 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).

JAVA AND INTERNET

 Java is strongly associated with the internet because of the first application program is written in Java was hot Java.

 Web browsers to run applets on the internet.

 Internet users can use Java to create applet programs & run then locally using a Java-enabled browser such as hot
Java.

 Java applets have made the internet a true extension of the storage system of the local computer.
JAVA CLASS LIBRARY

The Java Class Library (JCL) is a set of dynamically loadable libraries


that Java Virtual Machine (JVM) languages can call at run time.
Because the Java Platform is not dependent on a specific operating
system, applications cannot rely on any of the platform- native
libraries. Instead, the Java Platform provides a
comprehensive set of standard class libraries, containing the
functions common to modern operating systems.

JAVA DATA TYPES

A data type in java is a term that specifies memory size and type of
values that can be stored into the memory location. In other
words, data types define different values that a variable can take.

Java language provides several data types as shown in the below


figure. All data types are divided into two categorized that are as
follows:

 Primitive data types (also called intrinsic or built- in types)

Primitive data types in Java are those data types whose


variables can store only one value at a time. We cannot store multiple values of the same type. These data types are
predefined in Java. They are named by a Keyword.Primitive data types are not user-defined data-types. i.e.
Programmers cannot develop primitive data types.

Types of Primitive data types in Java

Java defines eight primitive data types: boolean, char, byte, short, int, long, char, float, and double. These can be
further categorized into four groups. They are as follows:

o Conditional category data type: boolean

o Character category data type: char

o Integer category data types: byte, short, int, and long.

o Float category data types: float and double

 Non-primitive data types (also called derived or reference data type)

These data types are not actually defined by the programming language but are created by the programmer. They
are also called “reference variables” or “object references” since they
reference a memory location which stores the data.

Types
o String
o Arrrays
o Classes
o Interfaces

Structure of Java Program

Java is an object-oriented programming, platform-independent, and secure


programming language that makes it popular. Using the Java programming language,
we can develop a wide variety of applications. So, before diving in depth, it is necessary
to understand the basic structure of Java program in detail. In this section, we have discussed the basic structure of a Java
program. At the end of this section, you will able to develop the Hello world Java program, easily.

A typical structure of a Java program contains the following elements:

 Documentation Section

 Package Declaration

 Import Statements

 Interface Section

 Class Definition

 Class Variables and Variables

 Main Method Class

 Methods and Behaviors

Documentation Section
The documentation section is an important section but optional for a Java program. It includes basic information about a
Java program. The information includes the author's name, date of creation, version, program name, company name, and
description of the program. It improves the readability of the program. Whatever we write in the documentation section, the
Java compiler ignores the statements during the execution of the program. To write the statements in the documentation
section, we use comments. The comments may be single-line, multi-line, and documentation comments.
 Single-line Comment: It starts with a pair of forwarding slash (//). For example:
//First Java Program
 Multi-line Comment: It starts with a /* and ends with */. We write between these two symbols. For example:
/*It is an example of
multiline comment*/
 Documentation Comment: It starts with the delimiter (/**) and ends with */. For example:
/**It is an example of documentation comment*/
Package Declaration
The package declaration is optional. It is placed just after the documentation section. In this section, we declare the package
name in which the class is placed. Note that there can be only one package statement in a Java program. It must be defined
before any class and interface declaration. It is necessary because a Java class can be placed in different packages and
directories based on the module they are used. For all these classes package belongs to a single parent directory. We use
Import Statements
The package contains the many predefined classes and interfaces. If we want to use any class of a particular package, we
need to import that class. The import statement represents the class stored in the other package. We use the import
keyword to import the class. It is written before the class declaration and after the package statement. We use the import
statement in two ways, either import a specific class or import all classes of a particular package. In a Java program, we can
use multiple import statements. For example:
Interface Section
It is an optional section. We can create an interface in this section if required. We use the interface keyword to create an
interface. An interface is a slightly different from the class. It contains only constants and method declarations. Another
difference is that it cannot be instantiated. We can use interface in classes by using the implements keyword. An interface
can also be used with other interfaces by using the extends keyword. For example:
Class Definition
In this section, we define the class. It is vital part of a Java program. Without the class, we cannot create any Java program. A
Java program may conation more than one class definition. We use the class keyword to define the class. The class is a
blueprint of a Java program. It contains information about user-defined methods, variables, and constants. Every Java
program has at least one class that contains the main() method. For example:
Class Variables and Constants
In this section, we define variables and constants that are to be used later in the program. In a Java program, the variables
and constants are defined just after the class definition. The variables and constants store values of the parameters. It is used
during the execution of the program. We can also decide and define the scope of variables by using the modifiers. It defines
the life of the variables. For example:
Main Method Class
In this section, we define the main() method. It is essential for all Java programs. Because the execution of all Java programs
starts from the main() method. In other words, it is an entry point of the class. It must be inside the class. Inside the main
method, we create objects and call the methods. We use the following statement to define the main() method:
Methods and behavior
In this section, we define the functionality of the program by using the methods. The methods are the set of instructions that
we want to perform. These instructions execute at runtime and perform the specified task. For example:

Understanding public static void main(String[] args) in Java

In Java programs, the point from where the program starts its execution or simply the entry point of Java programs is
the main() method. Hence, it is one of the most important methods of Java and having proper understanding of it is very
important.
Most common syntax of main() method:
public class MyFirstJavaProgram {
public static void main(String []args)
{
System.out.println("Hello World");
}
}
Explanation:
Every word in the public static void main statement has got a meaning to the JVM.
Public: It is an Access modifier, which specifies from where and who can access the method. Making the main() method
public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in
the current class.
Static: It is a keyword  which is when associated with a method, makes it a class related method. The main() method is
static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which
would have been used by the object declared only for calling the main() method by the JVM.
Void: It is a keyword and used to specify that a method doesn’t return anything. As main() method doesn’t return
anything, its return type is void. As soon as the main() method terminates, the java program terminates too. Hence, it
doesn’t make any sense to return from main() method as JVM can’t do anything with the return value of it.
main: It is the name of Java main method. It is the identifier that the JVM looks for as the starting point of the java
program. It’s not a keyword.
String[] args: It stores Java command line arguments and is an array of type java.lang.String class. Here, the name of the
String array is args  but it is not fixed and user can use any name in place of it.
Apart from the above mentioned signature of main, you could use public static void main(String args[]) or public static
void main(String… args) to call the main function in java. The main method is called if it’s formal parameter matches that
of an array of Strings.
Java Variables
A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.
Variable is a name of memory location. There are three types of variables in java: local, instance and static.
There are two types of data types in Java: primitive and non-primitive.
________________________________________
Variable
Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination
of "vary + able" that means its value can be changed.
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this variable only within that method
and the other methods in the class aren't even aware that the variable exists.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called instance variable. It is not declared as static.
It is called instance variable because its value is instance specific and is not shared among instances.
3) Static variable
A variable which is declared as static is called static variable. It cannot be local. You can create a single copy of static variable
and share among all the instances of the class. Memory allocation for static variable happens only once when the class is
loaded in the memory.
Example to understand the types of variables in java
class A{
int data=50;//instance variable
static int m=100;//static variable
void method(){
int n=90;//local variable
}
}//end of class

Operators in JAVA

Operation Type Operator Examples


Arithmetic Operator +,-,*,/,%,++,-- A+B
Relational Operator ==,!=,>,<,>=,<= A==B
Logical Operator &&,||,! (A==B)&&(A<B)
Assignment =,+=,-=,*=,/=,%= B=10
Operator
Ternary Operator (Condition)?value if true: value if false X=(A<B?)10:20
Bitwise Operator &,|,`,^ A&B,A|B
COMMAND LINE ARGUMENT
class test{
public static void main(String X[])
{
int count=0;
count=X.length;
System.out.println("No of argument:"+count);
for(int j=0;j<X.length;j++)
{
System.out.println("STUDENT"+j+":"+X[j]);
}
}
}
Java Operator Precedence Table
Precedenc Operator Type Associativity
e
15 () Parentheses Left to Right
[] Array subscript
· Member selection
14 ++ Unary post-increment Right to left
-- Unary post-decrement
13 ++ Unary pre-increment Right to left
-- Unary pre-decrement
+ Unary plus
- Unary minus
! Unary logical negation
~ Unary bitwise complement
( type ) Unary type cast
12 * Multiplication Left to right
/ Division
% Modulus
11 + Addition Left to right
- Subtraction
10 << Bitwise left shift Left to right
>> Bitwise right shift with sign extension
>>> Bitwise right shift with zero extension
9 < Relational less than Left to right
<= Relational less than or equal
> Relational greater than
>= Relational greater than or equal
instanceof Type comparison (objects only)
8 == Relational is equal to Left to right
!= Relational is not equal to
7 & Bitwise AND Left to right
6 ^ Bitwise exclusive OR Left to right
5 | Bitwise inclusive OR Left to right
4 && Logical AND Left to right
3 || Logical OR Left to right
2 ?: Ternary conditional Right to left
1 = Assignment Right to left
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment

Larger number means higher precedence.

You might also like