Search This Blog

Showing posts with label java interview. Show all posts
Showing posts with label java interview. Show all posts

Saturday, July 10, 2021

What are Packages in core java and how to use them ?

  Java package is a technique for organizing Java classes into namespaces similar to the modules of Modula, providing modular programming in JavaJava packages can be stored in compressed files called JAR files, allowing classes to be downloaded faster as groups rather than individually.

Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.

A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and namespace management.

Some of the existing packages in Java are::

·        java. lang - bundles the fundamental classes

·        java.io - classes for input, output functions are bundled in this package

Programmers can define their own packages to bundle groups of classes/interfaces, etc. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, annotations are related.

Since the package creates a new namespace there won't be any name conflicts with names in other packages. Using packages, it is easier to provide access control and it is also easier to locate the related classes.

Creating a package:


When creating a package, you should choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package.

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

If a package statement is not used then the class, interfaces, enumerations, and annotation types will be put into an unnamed package.

 

What are Packages in java and how to use them?

 OOPS CONCEPT

Packages in Java are a mechanism to encapsulate a group of classes, interfaces, and sub-packages. Many implementations of Java use a hierarchical file system to manage source and class files. It is easy to organize class files into packages. All we need to do is put related class files in the same directory, give the directory a name that relates to the purpose of the classes, and add a line to the top of each class file that declares the package name, which is the same as the directory name where they reside.

In java, there are already many predefined packages that we use while programming.

For example: java.langjava.iojava.util etc.


However one of the most useful features of java is that we can define our own packages


Advantages of using a package:


Before discussing how to use them Let see why we should use packages.

§  Reusability:  Reusability of code is one of the most important requirements in the software industry. Reusability saves time, effort and also ensures consistency. A class once developed can be reused by any number of programs wishing to incorporate the class in that particular program.

§  Easy to locate the files.

§  In a real-life situation, there may arise scenarios where we need to define files of the same name. This may lead to “name-space collisions”. Packages are a way of avoiding “name-space collisions”.


Types of packages:


1) User-defined package: The package we create is called a user-defined package.
2) Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.


Defining a Package:


This statement should be used at the beginning of the program to include that program in that particular package.


package  <package name>;

 

Sunday, June 27, 2021

Most common Core Java Interview Questions

1) What is the difference between an Abstract class and an Interface?

1. Abstract classes may have some executable methods and methods left unimplemented. Interfaces contain no implementation code.

2. An class can implement any number of interfaces, but subclass at most one abstract class.

3. An abstract class can have nonabstract methods. All methods of an interface are abstract.

4. An abstract class can have instance variables. An interface cannot.

5. An abstract class can define the constructor. An interface cannot.

6. An abstract class can have any visibility: public, protected, private or none (package). An interface's visibility must be public or none (package).

7. An abstract class inherits from Object and includes methods such as clone() and equals().


2) What are checked and unchecked exceptions?

Java defines two kinds of exceptions :

• Checked exceptions: Exceptions that inherit from the Exception class are checked exceptions. Client code has to handle the checked exceptions thrown by the API, either in a catch clause or by forwarding it outward with the throws clause. Examples - SQLException, IOException.

• Unchecked exceptions: RuntimeException also extends from Exception. However, all of the exceptions that inherit from RuntimeException get special treatment. There is no requirement for the client code to deal with them, and hence they are called unchecked exceptions. Example Unchecked exceptions are NullPointerException, OutOfMemoryError, DivideByZeroException typically,programming errors.


3) What is the difference between C++ & Java?

Well as Bjarne Stroustrup says "..despite the syntactic similarities, C++ and Java are very different languages. In many ways, Java seems closer to Smalltalk than to C++..". 

Here are a few I discovered:

• Java is multithreaded

• Java has no pointers

• Java has automatic memory management (garbage collection)

• Java is platform-independent (Stroustrup may differ by saying "Java is a platform"

• Java has built-in support for comment documentation

• Java has no operator overloading

• Java doesn’t provide multiple inheritance.

• There are no destructors in Java


4) What are the statements in JAVA?

Statements are equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon

• Assignment expressions

• Any use of ++ or --

• Method calls

• Object creation expressions

These kinds of statements are called expression statements. In addition to these kinds of expression statements, there are two other kinds of statements. A declaration statement declares a variable. A control flow statement regulates the order in which statements get executed. The for loop and the if statement are both examples of control flow statements.


5) What is the JAR file?

JavaARchive files are a big glob of Java classes, images, audio, etc., compressed to make one simple, smaller file to ease Applet downloading. Normally when a browser encounters an applet, it goes and downloads all the files, images, audio, used by the Applet separately. This can lead to slower downloads.