0% found this document useful (0 votes)
6 views9 pages

Assignment 1

assignment for java

Uploaded by

Hetal Mer
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)
6 views9 pages

Assignment 1

assignment for java

Uploaded by

Hetal Mer
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/ 9

Explain java buzzword

features of java
Explain the characteristics/principals of
Object Oriented Programming
Object-oriented programming generally referred to as OOPS is the
backbone of java as java is not a purely object oriented language but
it is object oriented language. Java organizes a program around the
various objects and well-defined interfaces. There are four pillars been
here in OOPS which are listed below. These concepts aim to
implement real-world entities in programs.
Abstraction
Encapsulation
Inheritance
Polymorphism
Abstraction
Abstraction is a process of hiding implementation details and
exposing only the functionality to the user. In abstraction, we deal with
ideas and not events. This means the user will only know “what it
does” rather than “how it does”.
There are two ways to achieve abstraction in Java:
Abstract class (0 to 100%)
Interface (100%)

Encapsulation

Encapsulation is the process of wrapping code and data together


into a single unit.
Real-Life Example:
A capsule which is mixed of several medicines. The medicines are
hidden data to the end user.
In order to achieve encapsulation in java follow certain steps as
proposed below:
Declare the variables as private
Declare the setters and getters to set and get the variable values

Note: There are few advantages of encapsulation in java as follows:


 Control Over Data: We can write the logic in the setter method to

not store the negative values for an Integer. So by this way we can
control the data.
 Data Hiding: The data members are private so other class can’t

access the data members.


 Easy to test: Unit testing is easy for encapsulated classes

Inheritance

Inheritance is the process of one class inheriting properties and


methods from another class in Java. Inheritance is used when we
have is-a relationship between objects. Inheritance in Java is
implemented using extends keyword.
Real-life Example:
The planet Earth and Mars inherits the super class Solar System and
Solar system inherits the Milky Way Galaxy. So Milky Way Galaxy is
the top super class for Class Solar System, Earth and Mars.
Let us do discuss the usage of inheritance in java applications with a
generic example before proposing the code. So consider an example
extending the Exception class to create an application-specific
Exception class that contains more information like error codes. For
example NullPointerException.
There are 5 different types of inheritance in java as follows:
Single Inheritance: Class B inherits Class A using extends keyword

Multilevel Inheritance: Class C inherits class B and B inherits class

A using extends keyword


Hierarchy Inheritance: Class B and C inherits class A in hierarchy

order using extends keyword


Multiple Inheritance: Class C inherits Class A and B. Here A and B

both are superclass and C is only one child class. Java is not
supporting Multiple Inheritance, but we can implement using
Interfaces.
Hybrid Inheritance: Class D inherits class B and class C. Class B

and C inherits A. Here same again Class D inherits two superclass, so


Java is not supporting Hybrid Inheritance as well.

Polymorphism

Polymorphism is the ability to perform many things in many ways.


The word Polymorphism is from two different Greek words- poly and
morphs. “Poly” means many, and “Morphs” means forms. So
polymorphism means many forms. The polymorphism can be present
in the case of inheritance also. The functions behave differently based
on the actual implementation.
Real-life Example:
A delivery person delivers items to the user. If it’s a postman he will
deliver the letters. If it’s a food delivery boy he will deliver the foods to
the user. Like this polymorphism implemented different ways for the
delivery function.
There are two types of polymorphism as listed below:
1.Static or Compile-time Polymorphism
2.Dynamic or Run-time Polymorphism
Static or Compile-time Polymorphism when the compiler is able to
determine the actual function, it’s called compile-
time polymorphism. Compile-time polymorphism can be achieved
by method overloading in java. When different functions in a class
have the same name but different signatures, it’s called method
overloading. A method signature contains the name and method
arguments. So, overloaded methods have different arguments. The
arguments might differ in the numbers or the type of arguments.

What is byte code? How byte code is


generated in java
Java bytecode is the product of the compilation process of your
Java source code. When you write a Java program and compile
it, the Java compiler (javac) doesn't convert your code directly
into machine code. Instead, it translates it into an intermediate
form known as bytecode. This bytecode is a set of instructions
that are not human-readable like Java code but are far less
complex than machine code.

What is the Java Virtual Machine?

The Java Virtual Machine is an abstract computing machine


that forms an integral part of the Java Runtime Environment
(JRE). Unlike a physical machine, which directly executes
machine code, the JVM interprets and executes Java bytecode.
This design enables Java applications to run on any device or
operating system that has a JVM implementation, adhering to
the “write once, run anywhere” (WORA) principle.

Explain java keywords


Reserved Words in Java
Any programming language reserves some words to represent
functionalities defined by that language. These words are called
reserved words. They can be briefly categorized into two
parts: keywords(50) and literals(3). Keywords define functionalities
and literals define value. Identifiers are used by symbol tables in
various analyzing phases(like lexical, syntax, and semantic) of a
compiler architecture.

abstra contin transie


for protected
ct ue nt

Defau
Assert Goto public Try
lt

Boole
Do If Static throws
an

doubl impleme Packa


break strictfp
e nts ge

byte else import super Private


case enum Interface Short switch

Exten instanceo
Catch return void
ds f

synchroniz volatil
Char Final Int
ed e

class finally long throw Date

const float Native This while

Explain java escape sequences


A character with a backslash (\) just before it is an escape sequence or
escape character. We use escape characters to perform some specific
task. The total number of escape sequences or escape characters in
Java is 8. Each escape character is a valid character literal. The list of
Java escape sequences:
public class Test {
public static void main(String[] args)
{
System.out.println("Good Morning\t Geeks! ");
}
}

Explain type conversation in java


Java Type Casting
Type casting is a technique that is used either by the compiler or a
programmer to convert one data type to another in Java. Type casting is also
known as type conversion. For example, converting int to double, double to
int, short to int, etc.
There are two types of type casting allowed in Java programming:
Widening type casting(implicit)
Narrowing type casting(explicit)
Widening Type Casting
Widening type casting is also known as implicit type casting in which a
smaller type is converted into a larger type, it is done by the compiler
automatically.
Hierarchy
Here is the hierarchy of widening type casting in Java:
byte > short > char > int > long > float > double
The compiler plays a role in the type conversion instead of programmers. It
changes the type of the variables at the compile time. Also, type conversion
occurs from the small data type to large data type only.
Example
In this example, we are adding an integer and a double number, storing the
result in a double variable to display the sum, implicitly casting the integer to a
double during the addition:
Open Compiler

package com.tutorialspoint;

public class Tester {


// Main driver method
public static void main(String[] args) {
// Define int variables
int num1 = 5004;
double num2 = 2.5;
double sum = num1 + num2;
// show output
System.out.println("The sum of " + num1 + " and " + num2 + " is " +
sum);
}
}

Narrowing type casting is also known as explicit type casting or explicit


type conversion which is done by the programmer manually. In the narrowing
type casting a larger type can be converted into a smaller type.
When a programmer changes the variable type while writing the code. We can
use the cast operator to change the type of the variable. For example, double
to int or int to double.
Syntax
Below is the syntax for narrowing type casting i.e., to manually type
conversion:
double doubleNum = (double) num;
The above code statement will convert the variable to double type.
Example
In the example below, we define the num variable of integer type and initialize
it with the value. Also, we define the doubleNum variable of double type and
store the num variable's value after converting it to the double.
Next, We created the 'convertedInt' integer type variable and stored the double
value after type casting to int. In the output, we can observe the value of the
double and int variables.
Open Compiler

package com.tutorialspoint;

public class Tester {


// Main driver method
public static void main(String[] args) {
// Define int variable
int num = 5004;
// Type casting int to double
double doubleNum = (double) num;
// show output
System.out.println("The value of " + num + " after converting to the
double is " + doubleNum);
// Type casting double to int
int convertedInt = (int) doubleNum;
// show output
System.out.println("The value of " + doubleNum + " after converting to
the int again is " + convertedInt);
}
}

Explain operator precedence in java


Precedence and associative rules are used when dealing with hybrid
equations involving more than one type of operator. In such cases,
these rules determine which part of the equation to consider first, as
there can be many different valuations for the same equation. The
below table depicts the precedence of operators in decreasing order
as magnitude, with the top representing the highest precedence and
the bottom showing the lowest precedence.

You might also like