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

Java Programming Basics

The document provides an overview of Java programming basics, covering topics such as the differences between primitive types and wrapper classes, the use of annotations, generics, enums, and the distinctions between Array, ArrayList, and Vector. It also explains concepts like autoboxing, unboxing, upcasting, and downcasting in Java. Additionally, it addresses the use of strings in switch statements and the absence of global variables in Java.

Uploaded by

rdipali162
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 views8 pages

Java Programming Basics

The document provides an overview of Java programming basics, covering topics such as the differences between primitive types and wrapper classes, the use of annotations, generics, enums, and the distinctions between Array, ArrayList, and Vector. It also explains concepts like autoboxing, unboxing, upcasting, and downcasting in Java. Additionally, it addresses the use of strings in switch statements and the absence of global variables in Java.

Uploaded by

rdipali162
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/ 8

Java Programming basics

1. What is the difference between Integer and int?

Sr. Factor int Integer


No.

1. Type An int is a primitive An Integer is a wrapper class for the


data type that is int data type that gives us more
capable of storing 32- flexibility in converting, storing, and
bit signed two's manipulating int data.
complement integer.

2. Flexibility An int only allows the An Integer is a wrapper class for int
binary value of an and provides more flexibility in
integer in it, and due comparison to the int.
to which it is provides
less flexibility.

3. Purpose It is used for only a Its main purpose is to convert an int


single purpose, i.e., into an object or an object into an
storing an integer int.
value into memory.

4. Memory It takes 4 bytes to It takes 16 bytes to store an integer


use store an integer value value in it.
in it.

5. Base We cannot convert The Integer class provides several


conversio the integer value of useful methods, such as
n int into another base. toBinaryString(), toOctalString(),
and toHexString(), that allows us to
directly convert the integer value
stored in the Integer.

6. Type We cannot pass the Integer provides several ways to


Casting decimal and string pass a decimal or string value to an
value to an int type object of Integer type. The
variable. Casting is Integer(String) and the
also not supported parseInt(String) are the two ways
for that. through which we can convert a
string to an int value.

7. Operation Operations are not We can perform operations such as


s allowed because of reversing a number, rotating it left,
not using the inbuilt or rotating it right.
functions.
2. Are there any global variables in Java, which can be accessed by other part
of your program?

There are no global variables in Java, but there are global classes with public fields. You can
use static import feature of java 5 to make it look almost like global variables.

3. What is Java Annotations?

Annotations are used to provide supplemental information about a


program.
 Annotations start with ‘@’.
 Annotations do not change the action of a compiled
program.
 Annotations help to associate metadata (information) to
the program elements i.e. instance variables, constructors,
methods, classes, etc.
 Annotations are not pure comments as they can change
the way a program is treated by the compiler. See below
code for example.
 Annotations basically are used to provide additional
information, so could be an alternative to XML and Java
marker interfaces.
Hierarchy of Annotations in Java
4. Can we use String with switch case?
Hence the concept of string in switch statement arises into play in
JDK 7 as we can use a string literal or constant to control a switch
statement, which is not possible in C/C++. Using a string-based
switch is an improvement over using the equivalent sequence of
if/else statements.

Example:
5. What is Autoboxing in Java?
6. What is Unboxing in Java?
Autoboxing refers to the conversion of a primitive value into an
object of the corresponding wrapper class is called autoboxing. For
example, converting int to Integer class. The Java compiler applies
autoboxing when a primitive value is:
 Passed as a parameter to a method that expects an
object of the corresponding wrapper class.
 Assigned to a variable of the corresponding wrapper
class.
Unboxing on the other hand refers to converting an object of a
wrapper type to its corresponding primitive value. For example
conversion of Integer to int. The Java compiler applies to unbox
when an object of a wrapper class is:
 Passed as a parameter to a method that expects a
value of the corresponding primitive type.
 Assigned to a variable of the corresponding primitive
type.
7. What is Generics in Java?
Generics means parameterized types. The idea is to allow type
(Integer, String, … etc., and user-defined types) to be a parameter
to methods, classes, and interfaces. Using Generics, it is possible to
create classes that work with different data types. An entity such as
class, interface, or method that operates on a parameterized type
is a generic entity.

8. What is Enum in Java?

An enum is a special "class" that represents a group


of constants (unchangeable variables, like final variables).

To create an enum, use the enum keyword (instead of class or interface), and
separate the constants with a comma. Note that they should be in uppercase
letters:

Example
enum Level {

LOW,

MEDIUM,

HIGH

You can access enum constants with the dot syntax:

Level myVar = Level.MEDIUM;


9. What is the difference between Array, ArrayList and vector?

S.
No
. ArrayList Vector

ArrayList is not
1. Vector is synchronized.
synchronized.

ArrayList increments 50% Vector increments 100% means


of the current array size if doubles the array size if the total
2.
the number of elements number of elements exceeds its
exceeds ts capacity. capacity.

ArrayList is not a legacy


3. class. It is introduced in Vector is a legacy class.
JDK 1.2.

Vector is slow because it is


synchronized, i.e., in a multithreading
ArrayList is fast because it environment, it holds the other threads
4.
is non-synchronized. in a runnable or non-runnable state
until the current thread releases the
lock of the object.

ArrayList uses the Iterator A Vector can use the Iterator interface
5. interface to traverse the or Enumeration interface to traverse
elements. the elements.

ArrayList performance is
6 Vector performance is low
high

Multiple threads is
7 only one threads are allowed .
allowed
Base Array ArrayList

It can be single-
It can only be single-
Dimensionality dimensional or
dimensional
multidimensional

For and for each


Traversing generally is used for Here iterator is used to
Elements iterating over traverse riverArrayList
arrays

length keyword can size() method is used to


Length give the total size of compute the size of
the array. ArrayList.

It is dynamic and can be


It is static and of
Size increased or decreased in
fixed length
size when required.

It is faster as above It is relatively slower


Speed we see it of fixed because of its dynamic
size nature

Primitive data types are


Primitive data types
Primitive not directly added unlikely
can be stored
Datatype arrays, they are added
directly unlikely
Storage indirectly with help of
objects
autoboxing and unboxing

They can not be They can be added here


Generics added here hence hence makingArrayList
type unsafe type-safe.

Assignment Here a special method is


Adding
operator only serves used known as add()
Elements
the purpose method
10.Explain up casting and down casting with example (Both class level and
primitive level)
Upcasting: Upcasting is the typecasting of a child object to a
parent object. Upcasting can be done implicitly. Upcasting gives
us the flexibility to access the parent class members but it is not
possible to access all the child class members using this feature.
Instead of all the members, we can access some specified
members of the child class. For instance, we can access the
overridden methods.
Downcasting: Similarly, downcasting means the typecasting of
a parent object to a child object. Downcasting cannot be
implicit.
The following image illustrates the concept of upcasting and
downcasting:

Example: Let there be a parent class. There can be many children


of a parent. Let’s take one of the children into consideration. The
child inherits the properties of the parent. Therefore, there is an
“is-a” relationship between the child and parent. Therefore, the
child can be implicitly upcasted to the parent. However, a parent
may or may not inherits the child’s properties. However, we can
forcefully cast a parent to a child which is known as downcasting.
After we define this type of casting explicitly, the compiler checks
in the background if this type of casting is possible or not. If it’s not
possible, the compiler throws a ClassCastException.

You might also like