100% found this document useful (1 vote)
35 views

JAVA

Uploaded by

durgaprasada323
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
35 views

JAVA

Uploaded by

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

JAVA

1.What is Java?
Java is a high-level, general-purpose, object-oriented, and secure programming language developed
by James Gosling at Sun Microsystems, Inc. in 1991. It is formally known as OAK. In 1995, Sun
Microsystem changed the name to Java. In 2009, Sun Microsystem takeover by Oracle Corporation.

2.Feature of Java:

o Simple: Java is a simple language because its syntax is simple, clean, and easy to understand.
Complex and ambiguous concepts of C++ are either eliminated or re-implemented in Java. For
example, pointer and operator overloading are not used in Java.
o Object-Oriented: In Java, everything is in the form of the object. It means it has some data
and behavior. A program must have at least one class and object.
o Robust: Java makes an effort to check error at run time and compile time. It uses a strong
memory management system called garbage collector. Exception handling and garbage
collection features make it strong.
o Secure: Java is a secure programming language because it has no explicit pointer and
programs runs in the virtual machine. Java contains a security manager that defines the access
of Java classes.
o Platform-Independent: Java provides a guarantee that code writes once and run anywhere.
This byte code is platform-independent and can be run on any machine.

o Portable: Java Byte code can be carried to any platform. No implementation-dependent


features. Everything related to storage is predefined, for example, the size of primitive data
types.
o High Performance: Java is an interpreted language. Java enables high performance with the
use of the Just-In-Time compiler.
o Distributed: Java also has networking facilities. It is designed for the distributed environment
of the internet because it supports TCP/IP protocol. It can run over the internet. EJB and RMI
are used to create a distributed system.
o Multi-threaded: Java also supports multi-threading. It means to handle more than one job a
time.

3.Difference between C++ and Java?

C++ Java
C++ is platform-dependent. Java is platform-independent.

C++ is mainly used for system programming Java is mainly used for application programming. It is
widely used in Windows-based, web-based, enterprise,
and mobile applications.

C++ was designed for systems and applications Java was designed and created as an interpreter for
programming. It was an extension of the C printing systems but later extended as a support
programming languages. network computing. It was designed to be easy to use
and accessible to a broader audience.

C++ uses compiler only. C++ is compiled and run Java uses both compiler and interpreter. Java source
using the compiler which converts source code code is converted into bytecode at compilation time.
into machine code so, C++ is platform dependent. The interpreter executes this bytecode at runtime and
produces output. Java is interpreted that is why it is
platform-independent.

C++ doesn't support documentation comments. Java supports documentation comment (/** ... */) to
create documentation for java source code.

4.Why java is platform independent?


Java platform-independent is that the Java compiled code(byte code) can run on all operating
systems. A program is written in a language that is a human-readable language. It may contain
words, phrases, etc which the machine does not understand. For the source code to be understood
by the machine, it needs to be in a language understood by machines, typically a machine-level
language. So, here comes the role of a compiler. The compiler converts the high-level language
(human language) into a format understood by the machines.
Therefore, a compiler is a program that translates the source code for another program from a
programming language into executable code. This executable code may be a sequence of machine
instructions that can be executed by the CPU directly, or it may be an intermediate representation
that is interpreted by a virtual machine. This intermediate representation in Java is the Java Byte
Code.

5.What is Variable ?
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, it is a name of the memory location. It is a combination of


"vary + able" which means its value can be changed. There are three types of variables in java: local,
instance and static.

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 an instance variable. It is not declared as static.It is called an instance variable because its value
is instance-specific and is not shared among instances.

3) Static variable: A variable that is declared as static is called a static variable. It cannot be local.
You can create a single copy of the static variable and share it among all the instances of the class.
Memory allocation for static variables happens only once when the class is loaded in the memory.

6.What is Method?
A method is a block of code which only runs when it is called. Methods are used to perform certain
actions, and they are also known as functions.
Why use methods?
To reuse code: define the code once, and use it many times.
A method must be declared within a class. It is defined with the name of the method, followed by
parentheses (). Java provides some pre-defined methods, such as System.out.println().
Advantage of Method

o Code Reusability
o Code Optimization

7.What is Data Type?

Data types specify the different sizes and values that can be stored in the variable. There are two types
of data types in Java:

Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and
double.

Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

8.What is OOP?
Alan Kay coined the term “object oriented programming” in 1966 .

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that perform operations on the data,
while object-oriented programming is about creating objects that contain both data and methods.

Object-oriented programming has several advantages over procedural programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to
maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code and shorter
development time

9.What is Class and Object?


A class is a group of objects which have common properties. It is a template or blueprint from which
objects are created.

An entity that has state and behavior is known as an object

Example of Class And Object:

Class: Laptop

In your example, Laptop is a class. It serves as a blueprint for creating laptop objects. It defines the
structure and behavior that all laptops should have.

Attributes: Attributes, also known as fields or properties, are characteristics or data associated with a
class. In the context of a Laptop class, attributes could include:

Brand: This attribute represents the brand of the laptop, such as HP, Lenovo, or Dell.

Model: It specifies the model or series of the laptop, like Pavilion, ThinkPad, or XPS.

Price: This attribute represents the price of the laptop.

Objects: HP, Lenovo, Dell

In your example, HP, Lenovo, and Dell are objects created from the Laptop class. These objects are
instances of the Laptop class and have their own unique values for the attributes.

10.What is Polymorphism?
Polymorphism allows us to perform a single action in different ways. In other words, polymorphism
allows you to define one interface and have multiple implementations. The word “poly” means
many and “morphs” means forms, So it means many forms.Polymorphism promotes code reusability
and flexibility because you can write code that can work with a wide variety of objects that share a
common superclass.
Example: Animals
Consider an animal hierarchy with a common superclass Animal and subclasses like Dog, Cat, and
Bird. The Animal class may have a method called makeSound(). Polymorphism enables you to
interact with animals in a uniform way by calling makeSound() on any animal object. The specific
sound produced (barking, meowing, or chirping) depends on the actual type of animal.
Method Overloading:
When there are multiple functions with the same name but different parameters then these functions
are said to be overloaded. Functions can be overloaded by changes in the number of arguments
or/and a change in the type of arguments. Method overriding is used for Compile-time polymorphism

Example: Calculator
The Calculator class has two add methods with different parameter lists—one for integers and one for
double numbers. When you call the add method, the compiler determines which version of the method
to invoke based on the argument types you provide.

class Calculator {
// Method to add two integers
public int add(int num1, int num2) {
return num1 + num2;
}
// Method to add two double numbers
public double add(double num1, double num2) {
return num1 + num2;
}
}

Method OverRiding:
A subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java. Method overriding is used for runtime polymorphism

Example: Bank Accounts

For a BankAccount hierarchy, a superclass may define an interestRate method. Subclasses like
SavingsAccount and FixedDeposit can override it with their own interest rate calculations.

class BankAccount {
protected double balance;
BankAccount(double balance) {
this.balance = balance;
}
double calculateInterest() {
return 0.0; // Default interest for a generic bank account
}
}
class SavingsAccount extends BankAccount {
SavingsAccount(double balance) {
super(balance);
}
@Override
double calculateInterest() {
// Specific interest calculation formula for a savings account
double interestRate = 0.03; // 3% annual interest rate for savings
return balance * interestRate;
}
}
class FixedDeposit extends BankAccount {
FixedDeposit(double balance) {
super(balance);
}
@Override
double calculateInterest() {
// Specific interest calculation formula for a fixed deposit
double interestRate = 0.05; // 5% annual interest rate for fixed deposit
return balance * interestRate;
}
}
public class Main {
public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount(1000.0);
FixedDeposit fixedDeposit = new FixedDeposit(2000.0);
Double savingsInterest=savingsAccount.calculateInterest();
Double fixedDepositInterest = fixedDeposit.calculateInterest();
System.out.println("Savings Account Interest: " + savingsInterest);
System.out.println("Fixed Deposit Interest: " + fixedDepositInterest);
}
}

Disadvantages of Polymorphism
 Can make it more difficult to understand the behavior of an object, especially if the code is
complex.
 This may lead to performance issues, as polymorphic behavior may require additional
computations at runtime.
Advantages of Polymorphism in Java
 Increases code reusability by allowing objects of different classes to be treated as objects of a
common class.
 Improves readability and maintainability of code by reducing the amount of code that needs
to be written and maintaince.

11.What is Inheritance?
Getting the properties from base class to derived class. The new class that is created is known
as subclass (child or derived class) and the existing class from where the child class is derived is
known as superclass (parent or base class).
 The extends keyword is used to perform inheritance
 The super keyword is used to call the method of the parent class from the method of the child
class.
The most important use of inheritance in Java is code reusability. The code that is present in the parent
class can be directly used by the child class.

Types of inheritance:There are five types of inheritance.

1. Single Inheritance
In single inheritance, a single subclass extends from a single superclass. For example,
2. Multilevel Inheritance

In multilevel inheritance, a subclass extends from a superclass and then

the same subclass acts as a superclass for another class. For

example,

3.Hierarchical Inheritance

In hierarchical inheritance, multiple subclasses extend from a single

superclass. For example,

4.Multiple Inheritance

Java does not support Multiple Inheritance directly. We can

achieve this by using “Interface” keyword. In multiple inheritance, a single subclass

extends from multiple super classes. For example,

5.Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of

inheritance. For example,

12.What is Encapsulation?
Encapsulation in Java is a process of wrapping code and data together into a single unit, for
example, a capsule which is mixed of several medicines.
Example: SmartPhone
Imagine you have a smartphone, such as an iPhone or an Android device. A smartphone is a complex
piece of technology with various hardware components and software functionalities. It's a great
example of encapsulation because it encapsulates its internal workings, data, and functionalities from
the user.

13.What is Abstraction?
Abstraction in Java is a process of hiding the implementation details from the user and showing only
the functionality to the user. It can be achieved by using abstract classes, methods, and interfaces.

Example: Television Remote Control


Abstraction in a television remote control means simplifying the process of controlling a TV. Users
interact with a straightforward set of buttons to perform actions like changing channels and adjusting
the volume. Behind the scenes, the remote control abstracts away the complex inner workings of the
TV, such as electronics and communication protocols. This abstraction offers a standardized and user-
friendly interface, making it easy for anyone to operate the TV without needing to understand the
technical details of how it functions.

14.What is JRE?

The Java Runtime Environment (JRE) is software that Java programs require to run correctly.
JRE is the part of the Java Development Kit (JDK). It is a freely available software distribution which
has Java Class Library, specific tools, and a stand-alone JVM. It is the most common environment
available on devices to run java programs. The source Java code gets compiled and converted to Java
bytecode.

15.What is JVM?

The JVM is software that runs the Java program line by line. A Java Virtual Machine (JVM) is a program
that interprets Java bytecode to run as a program by providing a runtime environment that executes
this process.

16.What is 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.

You might also like