Saturday, 22 June 2013

What is rt.jar file in Java

rt.jar is java runtime library. It contains all classes required by JRE for execution of java programm.

In jar file, we have bunch of packages clubbed together.

In each package we have related classes stored together.

In Below image you can see how classes are organised in Java.



What are packages in JAVA

Packages are nothing but folders containing similar java classes, interfaces, enumerations, annotations.
So if you have n java classes that are focused on one functionality, we generally store those classes in one folder. This is called as a package.

Advantages of creating packages in JAVA:-

  • We can easily search the classes, interfaces etc.
  • It helps in naming convention.
  • It prevents name conflicts.
If you have a package transport wich contains Bike class.
You can access class using below methods
  • Class name -> transport.Bike.java
  • Path name -> transport/Bike.java
Generally source/class files are stored in reverse company domain name.

for example - 

com\amazon\transport\Abc.java
com.amazon.transport\Abc.class

//ABC.java - source file

package com.amazon.transport; 
public class Abc{ }
class Xyz{ }

When you compile the Abc.java
$javac -d . Abc.java

This will create 2 class files.

com\amazon\transport\Abc.class
com\amazon\transport\Xyz.class

To import all classes defined in package transport, you can use below statment

import com.amazon.transport.*


To tell JVM where it should find the classes, we can set the classpath variable giving the path your class files are stored.









What is boxing and Unboxing in JAVA

Well - to understand boxing concept in JAVA, you must know the classes and objects concept.

In java everything is object.

For example -

int a=10;

here a is a variable of primitive data type but you can convert this a into object of Integer Type.

Integer a=10;

here a is a object of class Integer.

So conversion of int to Integer is called boxing and reverse way is unboxing

How to come out of for loop in JAVA

We can exit any loop in java like for, while using break keyword.

What are different modifiers in JAVA

There are 2 types of modifiers in JAVA.

1. Access Modifiers in JAVA

  • private     - visible to class
  • public      - visible to world
  • protected - visible to package and subclasses
  • default     - visible to package 
Classes can be either default or Public.

Default class can be accessed by other classes in same package.
Public class can be accessed by classes from any other classes (in same or different package)

Class members can have any of the modifier.

Class members like variables and methods can be private, public, protected or default.
Private members can be accessed only within the class.
Public members can be accessed from outside the class.
Default members and protected members are same except one difference that default members have only package visibility. While Protected members have package visibility as well as in other sub classes.


2. Non-Access Modifiers in JAVA
  • static
  • final
  • abstract
  • synchronized and volatile - used in threads

Classes can be - final and abstract

Methods can be static final abstract

Variables can be static, final, synchronized and volatile.










What are different variables types in Java

There are 3 types of variables in JAVA.


  1. Local Variables - This is used inside method for temporary purpose.
  2. Instance Class Variables - copy of this variable is created separately for each object.
  3. Static Class Variables - there is single copy of this variable for all objects.


What are the different data types in JAVA

JAVA has below primitive data types -


  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. boolean
  8. char
Java has also got the class data types 

For Example -  Test test = new Test();

here Test is a class data type .