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

Java Basics

The document provides an overview of key concepts in Java, including classes, objects, object-oriented programming (OOP), and procedure-oriented programming (POP). It outlines the principles of OOP, such as encapsulation, inheritance, polymorphism, and abstraction, and differentiates between real-world and virtual objects. Additionally, it discusses Java's history, the role of the Java Virtual Machine (JVM), types of Java programs, functions, and various Java packages.

Uploaded by

n85568022
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)
9 views

Java Basics

The document provides an overview of key concepts in Java, including classes, objects, object-oriented programming (OOP), and procedure-oriented programming (POP). It outlines the principles of OOP, such as encapsulation, inheritance, polymorphism, and abstraction, and differentiates between real-world and virtual objects. Additionally, it discusses Java's history, the role of the Java Virtual Machine (JVM), types of Java programs, functions, and various Java packages.

Uploaded by

n85568022
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/ 6

What is a class?

In Java, a class is defined as a blueprint or template that is used to define the structure and behavior of objects. It is
used as a blueprint for crea�ng objects, which are instances of the class. Data members (variables) and methods
(func�ons) that define the characteris�cs and ac�ons of objects belonging to that class are contained within it. It is
considered as a fundamental building block of object-oriented programming in Java, allowing objects with similar
proper�es and func�onali�es to be created and organized.

What is an object?
In Java, objects are instances or individual en��es created from a class. They represent real-world things or concepts
and have both data (atributes) and behaviour (ac�ons) associated with them. Objects are used to interact with each
other and perform specific tasks in a program. They are the basic units of object-oriented programming, allowing
developers to model and manipulate data in a structured and organized manner.

Differen�ate between Real-world objects and Virtual objects.


Real-world objects Virtual objects
Real-world objects refer to physical en��es or Virtual objects are objects that exist only within
concepts that exist in the real world. the context of a computer program or
simula�on.
In Java, real-world objects are represented by In Java, virtual objects are created and
crea�ng instances (objects) of classes that model manipulated using classes just like real-world
these en��es or concepts. objects, but they represent abstract or
conceptual en��es within the program.
Real-world objects have real-world proper�es Virtual objects are used to model and manage
(data) and ac�ons (methods) associated with data and ac�ons in so�ware applica�ons and do
them. not have a physical existence outside the
program's execu�on.
Examples of real-world objects in Java could be a Examples of virtual objects in Java could be a
"Car" object, represen�ng a physical car, or a "User" object represen�ng a user account in a
"Person" object, represen�ng a real person. so�ware applica�on or a "Transac�on" object
represen�ng a financial transac�on within a
banking system.

What is an Object-Oriented Programming?


Object-oriented programming (OOP) is a programming approach that groups data and func�ons together into objects.
It lets you model real-world things and their interac�ons in a modular and reusable way, making so�ware design and
maintenance easier.
Note: It stresses more on data rather than being concerned with objects.

What is a Procedure Oriented Programming?


Procedure-oriented programming (POP) is a programming paradigm that emphasizes the use of procedures or
func�ons to perform tasks. It organizes code into a series of sequen�al steps, and data is o�en separated from the
procedures. In POP, the focus is on solving problems by defining a series of steps to be executed in a specific order,
without necessarily modelling real-world en��es.

What are the principles of OOP? Name them.


The principles of Object-Oriented Programming (OOP) are commonly referred to as the four pillars of OOP. They are:
1. Encapsula�on
2. Inheritance
3. Polymorphism
4. Abstrac�on
What is an abstrac�on in Java?
Abstrac�on is the process of simplifying complex real-world en��es into their essen�al characteris�cs. In Java,
abstrac�on is achieved by crea�ng abstract classes and interfaces. An abstract class defines a common structure for its
subclasses, while an interface defines a set of abstract methods that must be implemented by the classes that
implement the interface. Abstrac�on enables developers to work with high-level concepts and ignore unnecessary
details, making it easier to design and manage large-scale applica�ons.

What is encapsula�on in Java?


Encapsula�on is the principle of bundling data (atributes) and the methods (func�ons) that operate on that data within
a single unit, called a class. This helps hide the internal details of how an object works from the outside world and
exposes only the necessary interfaces to interact with the object. Encapsula�on promotes data security, code
organiza�on, and maintains the integrity of the object's state.

What is inheritance in Java?


Inheritance is the principle that allows a class (subclass or derived class) to inherit proper�es and behaviours from
another class (superclass or base class). The subclass can reuse and extend the func�onali�es of the superclass, leading
to code reusability and hierarchy. This enables a natural way to model rela�onships between objects, as subclasses can
specialize or add specific features while inheri�ng common characteris�cs from the superclass.

What is polymorphism in Java?


Polymorphism means the ability of a class or method to take on mul�ple forms. In Java, polymorphism can be achieved
through method overloading (having mul�ple methods with the same name but different parameters) and method
overriding (redefining a method in a subclass that already exists in its superclass). Polymorphism allows objects of
different classes to be treated as objects of a common superclass, providing flexibility and dynamic behaviour in the
code.

Differen�ate between OOP and POP.


OOP POP
Object oriented. Structure oriented.
Program is divided into objects. Program is divided into func�ons.
Botom-up approach. Top-down approach.
Inheritance property is used. Inheritance is not allowed.
It uses access specifier. It doesn’t use access specifier.
Encapsula�on is used to hide the data. No data hiding.
Concept of virtual func�on. No virtual func�on.
Object func�ons are linked through message Parts of program are linked through parameter
passing. passing.
Adding new data and func�ons is easy Expanding new data and func�ons is not easy.
The exis�ng code can be reused. No code reusability.
use for solving big problems. Not suitable for solving big problems.
C++, Java. C, Pascal.

What is JVM?
JVM stands for "Java Virtual Machine." It is a crucial part of the Java programming language and allows Java programs
to run on different computer systems without modifica�on.

Imagine having a magic translator for Java. This translator can understand Java code and convert it into a language that
your computer can understand and execute. That's what JVM does! It takes your Java program and translates it into
instruc�ons that your computer's processor can follow. This way, Java programs can run on any computer, whether it's
a Windows PC, a Mac, or a Linux machine, because the JVM handles the transla�on for you.

So, JVM acts as a bridge between your Java code and the computer's hardware, making Java a "write once, run
anywhere" language.
Explain history of Java.
Java was developed by a team of engineers at Sun Microsystems, led by James Gosling. The team, known as the "Green
Team," was formed in 1991 with the goal of crea�ng so�ware for interac�ve television and other consumer electronics.
The primary individuals involved in the development of Java were:
James Gosling: O�en referred to as the "father of Java," James Gosling was the chief architect of the Java programming
language. He played a crucial role in the design and development of Java from its incep�on.
Mike Sheridan: Mike Sheridan was one of the key members of the Green Team and contributed to the early
development of Java.
Patrick Naughton: Patrick Naughton was another important member of the Green Team and made significant
contribu�ons to the crea�on of Java.
The original name of the programming language was "Oak." It was named so because the team developed it for the
Green Project, which was aimed at crea�ng so�ware for interac�ve television and home appliances.
However, as the project shi�ed its focus from interac�ve television to the World Wide Web and other networked
devices, the name "Oak" no longer seemed appropriate. Addi�onally, there were trademark conflicts with the name
"Oak" as it was already in use by other companies.
To address these issues and find a more suitable name, the team held a brainstorming session. They eventually setled
on the name "Java," inspired by the Java coffee they o�en drank in their office cafeteria. The name "Java" was finalized
in 1994 and officially announced to the public in May 1995.

Differen�ate between a normal compiler and a Java compiler.


• Java uses javac (compiler) to convert the java code to byte code (.class file).
• When we run this code using JVM, it internally converts the byte code to system understandable code using
an interpreter.
• Instead of execu�ng a piece of code, again and again, JVM iden�fies them as “hot spots” and compiles them
using Just in �me compiler and, later reuses the same when required.
• A compiler compiles (translates) the given program to executable code (whole code at a �me).
• A JIT compiler performs a similar task but it is used by JVM internally, to translate the hotspots in the byte
code.
• A compiler compiles (translates) the given program to executable code (whole code at a �me).
• A JIT compiler performs a similar task but it is used by JVM internally, to translate the hotspots in the byte
code.

How many types of Java programs are there?


Java programs are of 2 types:
a) Applets
b) Applica�ons
What is a func�on in Java?
In Java, a func�on is a small, self-contained block of code that performs a specific task. It's like a mini-program within
a larger program. Func�ons are designed to take inputs (if needed), process them, and then give an output (if required).
They make it easier to organize and reuse code in a program, making it more manageable and understandable.
Think of a func�on like a recipe. Just as a recipe has a set of instruc�ons to prepare a dish, a func�on has a set
of instruc�ons to perform a par�cular job in a program. You can use the same func�on mul�ple �mes in your program,
just like you might use the same recipe to make the same dish several �mes.
In Java, func�ons are also called "methods," and they are declared inside a class. They have a name, a list of
inputs (called parameters), and an op�onal output (called the return value). When you call a func�on, you're telling
Java to execute the code inside that func�on, and it can return a result if needed.
For example, imagine you have a func�on called calculateSum that takes two numbers as input and returns their
sum. You can use this func�on to add different pairs of numbers, and it will give you the correct sum each �me.

Here's a simple example of a Java func�on:

class MathOpera�ons
{
int calculateSum(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
}

How many types of func�ons are present in Java language?


There are two func�ons in Java:
a) Pure Func�on (Returnable Func�on)
b) Impure Func�on (Non-Returnable Func�on)

What is a pure func�on?


A pure func�on is a type of func�on in programming that always produces the same output for the same input and
has no side effects. This means that a pure func�on does not modify any external state or variables and does not have
any observable interac�ons outside of the func�on itself. The concept of pure func�ons is a fundamental principle in
func�onal programming.
Syntax of an pure func�on in Java:
class PureFunc�onExample
{
sta�c int add(int num1, int num2)
{
return num1 + num2;
}
public sta�c void main(String[] args)
{
int result = add(5, 3);
System.out.println("The result is: " + result);
}
}
The add() here is the pure func�on since it has a data type as its return type and it uses a return keyword in its
defini�on.
Syntax:
Data_type func�on_name()
What is an impure func�on?
An impure func�on is a type of func�on in programming that does not adhere to the principles of a pure func�on.
Unlike pure func�ons, impure func�ons may have side effects or rely on external state, making their behaviour less
predictable and poten�ally causing changes in the program's state beyond the func�on's return value.
Syntax of an impure func�on in Java:
class ImpureFunc�onExample
{
sta�c int sum = 0; // External state
sta�c int addToSum(int num)
{
sum += num; // Modifying external state (side effect)
return sum;
}
sta�c void main(String[] args)
{
int result1 = addToSum(5);
System.out.println("Result 1: " + result1); // Output: Result 1: 5

int result2 = addToSum(3);


System.out.println("Result 2: " + result2); // Output: Result 2: 8
}
}
In this example, the addToSum func�on takes an integer as input and modifies the external variable sum. It has a side
effect of altering the program's state, and the result of subsequent calls depends on the value of sum, which is beyond
the func�on's input parameters. Therefore, addToSum is considered an impure func�on.

Explain ‘new’ keyword in Java.


The ‘new’ keyword in Java is used to create new instances (objects) of classes.

Name some Java packages.


Some java packages are as follows:
• java.lang: This package contains fundamental classes and is automa�cally imported into all Java programs. It
includes classes like String, Integer, Boolean, and others.
• java.u�l: This package provides u�lity classes that are commonly used in Java programs. It includes classes for
working with collec�ons, dates, and other u�lity func�ons.
• java.io: This package contains classes for handling input and output opera�ons, such as reading and wri�ng
data to files.
• java.awt: This package contains classes for building GUI applica�ons. It provides basic components like butons,
text fields, and windows.
• java.net: This package contains classes for networking in Java. It allows you to work with URLs, sockets, and
network connec�ons.
• java.sql: This package contains classes for database connec�vity and working with SQL databases.
• java.�me: This package provides classes for working with dates, �mes, and �me zones in Java.
• javax.swing: This package extends the java.awt package and provides more advanced GUI components for
building desktop applica�ons.
• javax.servlet: This package contains classes for building web applica�ons using Java Servlets.
What is ternary Operator in Java? Give syntax.
The ternary operator in Java is a shorthand way to write a simple condi�onal expression. It is the only operator that
takes three operands: a condi�on, a value to be returned if the condi�on is true, and a value to be returned if the
condi�on is false. The ternary operator allows you to write concise if-else statements in a single line of code.
Syntax of the ternary operator:
result = (condi�on) ? value_if_true : value_if_false;

Explana�on:
condi�on: The expression that evaluates to either true or false.
value_if_true: The value to be assigned to result if the condi�on is true.
value_if_false: The value to be assigned to result if the condi�on is false.

Example:
int num = 10;
String result = (num > 5) ? "Greater than 5”: "Less than or equal to 5";
System.out.println(result);

In this example, if the value of num is greater than 5, the ternary operator will return "Greater than 5," and if num is
less than or equal to 5, it will return "Less than or equal to 5." The result will be printed accordingly.

You might also like