Dec 20, 2013

Java Interview Questions (Part 1)

Java interview questions are provided here for the convenience of interviews. Fresher don't have any idea about the java question asked by interviewers in a java interview. Questions asked in java interview can be from any where. They can be from java development to java evolution. So here i am providing some important java interview questions for the java interviews. It will help them a lot in cracking a java interview. A list of java interview questions is given below-

Question: Why Was Java Developed?

Ans: Java was developed to provide the concept of platform independence by James Gosling and his team.

Question: What is java?

Ans: Java is the trademark of SUN MICRO SYSTEM.

Note: There is no full form of java.

Java is combination of 3 things:

(1)Oops

(2)Platform

(3)Technology

Question: What is program?

Ans: Program is set of instructions to perform the specific task.

Question: What is programming language?

Ans: Programming Language is a mode of human communication with the computer.

Question: What is a high-level-language(hll)?

Ans: A high level language is a language which is-

(1)Pure english language

(2)Human understandable language

(3)Source code also in high level language.

Question: What is middle level language?

Ans: (1)It is intermediate language code.

(2)It contains byte code.

(3)MLL code like mnemonics.

(4)Format of mnemonics looks like a(microprocessor commands)ADD,move,MUL,SUB(low level).

(5)Humane cannot understand ,that code.

(6)But Assembly and micro processor commands in Low Level .

Question: What is Low Label Language?

Ans: 1.Machine(OS) understandable code.

2.Pure binary(0/1) data.

3.But Assembly and micro processor commands(ADD,MOVE,MUL,SUB) in Low Level



.

Nov 7, 2013

Internals of Classes

Internals of Classes: Explaining internal mechanism of classes in java

Internals of classes: Classes in java are user defined data types.We can understand internal of classes with an example which is discussed below in this post.A java class is the container which stores members. Class members means methods,variables,blocks,constructor. Class binds multiple methods into a single unit. A class can have only four types of members i.e. methods,variables,blocks,constructor. 

As in real world, Human is a class,Vehicle is a class, Animal is a class same here in java there are classes having their objects.

More specifically class is a concept. for ex- A class room which has its boundary, all things like fan,table,chair,computer are its member and the area in which these members exists is known as class.

Program for a class in java-

//defining class name in java programming

class Java_prg

{

// main() method

public static void main(String args[])

{

// message to display

System.out.println("this is a class");

}

}

Java References:-



Oct 6, 2013

static method void return type reference parameter

static method with void return type and reference parameter

Java: static method void return type reference parameter in java-programming. Explaining working of a static method with void return type and reference parameter. Complete description of each term in static method void return type and reference parameter.

static methods are called without use of object i.e. methods which are declared as static can be called with corresponding class name. Methods can have either zero parameter or multiple parameters. Static methods can have arguments of both type i.e. primitive and reference. Like non-static methods, a static method's return type can be void or primitive or reference. When arguments passed with in the method are reference it means argument will be of class type or interface type or enum type or array type. A static method method whose return type is void then it means, this method will not return any value. Here is a program to explain the concept of Static method void return type reference parameter.


Example of static method void return type reference parameter:-

class SM
{
static void abc(SM y)
 {
   System.out.println("inside static method of abc");
 }
public static void main(String args[])
 {
  SM d=new SM();
 abc(d);
 }
}

In above program, method abc() is called without object because it is a static method and all static methods in java can be called with or without object if they are called in same class otherwise with corresponding class name.

Oct 5, 2013

Internal working of JVM(Java Virtual Machine)

Internal working of Java Virtual Machine(JVM) in java Programming

Java:Internal Working of JVM(Java Virtual Machine). Let us describe what is the internal working of Java Virtual Machine(JVM) in java-programming. I am describing here what happens internally in the JVM when a java-program is executed. Working of Java Virtual Machine is described below-

Working of Java Virtual Machine(JVM):-
  1. To load .class file from hard disk to RAM by the Class Loader Subsystem and stored into method memory area only once.
  2. To allocate the memory for all the static member of the specified class.
  3. If in specified class there is a static variable without initialized then JVM sets the default value of corresponding data type.
  4. If a class contains static blocks, then they executes in top to bottom order.
  5. Now control moves to the main method.
Above five steps defines the internal working of JVM(Java Virtual Machine) when a java-program is executed. 
.



Oct 2, 2013

static method void return type primitive parameter

static method with void return type with primitive parameter: Lets describe what is the mean of static method whose return type is primitive and it has primitive parameter. void return type i.e. it does not returns any type of value and primitive parameter means it has a parameter like int, char, float etc.... static method means it can be called with class name i.e. there is no need to create the object of the class for calling this method. A program for this tutorial is given below-

class Sm
{
 static void abc(int a,boolean b)
  {
    System.out.println("inside static method of abc of class Sm");
  }
public static void main(String args[])
  {
   int a=20;
   boolean c=true;
   Sm.abc(a,c);
  }
}

Explanation:
In this program we can also call abc() method directly without class name because within the same class we can call static method with or without class name.
For Example:
class Sm1
{
 static void abc1(int a,boolean b)
  {
    System.out.println("inside static method of abc of class Sm");
  }
public static void main(String args[])
  {
   int a=20;
   boolean c=true;
   abc1(a,c);
  }
}

Related posts:

Oct 1, 2013

static method void return type with zero parameter

Static method with Void Return type with zero parameter in java : This tutorial is provided to understand the mean of void return type with zero parameter.In java, there are three types of methods namely- 1) Void return type. 2) Primitive return type 3)reference return type. Void return type means a method which returns no value and zero parameter means no parameter is passed within the method. Let's demonstrate void return type method with zero parameter in java. Program for this tutorial is given below-


class Sm1
{
static void abc()
{
       System.out.println("inside static method of abc");
 }
 }
class  Test
{
public static void main(String[] args) 
{
Sm1.abc();
}
}

Explanation:
abc() is a staic method in this program i.e. it can be called without creating object of this class. So we called it with class name i.e. Sm1. abc() is not returning any type of value so its return type is declared as void and we have not used return statement in the method body area.

Sep 19, 2013

Job of new operator in java

What does new operator do?

new operator in java is used for creating the object of the class. new operator instantiates the class and allocates memory to the object. As an object is instantiated all non-static member of the class are activated and ready to be used. It is the well known working of new operator, I want to tell you what happens in the memory when an object is created in the memory i.e. job of new operator or responsibilities of new operator in java programming.
We will discuss step by step jobs of new operator. First i want to tell you how we create an object?

class_name <reference variable> = new class_name();

Here, <reference variable> is the variable of specified class and it stores the hashcode(address of object where it has been created) returned by the new operator. hashcode is in the form of hexadecimal code.
Now we will discuss job of new operator.
  1. If .class file is not loaded  then to load the .class file from hard disk to RAM and store into method memory area(mma).
  2. To allocate the memory for all non-static context and allocate the memory for all static member.
  3. Execute all static blocks.
  4. Create the context or segment for an object of class into heap memory area and generate a unique id that is known as hashcode. Non technically  we call it reference id or unique id.
  5. Allocate memory for all non-static member of a class inside an object.
  6. If any non-static variable of a class which is not initialized, is initialized by its default value.
  7. Execute all non-static blocks into top to bottom order.
  8. Execute constructor.
  9. Write the hashcode into specified variable.
Note: If .class file is already loaded then step no 4 to 9 will be followed only.

Use of new operator in java programming is very broad. new operator is responsible for many tasks which we have discussed above. It completes the discussion of What actually new operator does in java. For more queries please visit internal mechanism of object.

Sep 18, 2013

Internal Mechanism of public static void main(String args[])

Internal Mechanism of public static void main(String args[]) in java programming:


Java: public static void main(String args[]): Let us explaining public static void main(String args[]). Java is a platform independent language in which main() method is declared as public as well as static and its return type is void. In main method a String type array parameter is passed. Let us take a look why do we write main() method as public static void main(String args[]). We will explain each term step by step as below-


Why do we use public keyword:

public is an access specifier in java programming i.e. it defines the scope and visibility of class members. main() is also a class member when we declare main() method as public it can be called by JVM. main() is a method which is called by JVM automatically. There are two environment in java programming one is JDK environment and another is JVM environment. If JVM calls a method written in JDK environment then that method need to be declared as public. As we know that when ever we want to call a method out side of the class or package then that method must be declared as public. This is the concept behind main() method declared as public.

Why do we use static keyword:

static is an access access modifier in java programming i.e. it defines characteristics of class members. static member of the class are automatically loaded into memory without creating object of the class and non-static member needs to load into memory with object. So if we don't declare main() method as static then it would be a non-static method and would need to be called by object reference. Unfortunately JVM don't create any object automatically in this situation any instance member will not be instantiated through out its life.



Why do we use void return type:


main() method is automatically called by JVM. Its return type is void i.e. it returns no value. If it have any return type then it would be received by JVM and there is no procedure for using a value received by JVM. So we declare main() method with void return type.



Why do we use String args[]:


In main() method an array instance of String class is passed. Purpose of passing this type parameter is to receive the COMMAND LINE ARGUMENT when program is executed. In String args[], args is an array type variable of class String. Value inputted by command line argument is treated as String type however it is an integer value.


For example-


If user has inputted two integer numbers for addition that will be treated as String in this situation we need to convert these no. into integer type by using parsing technique.










It completes the great explanation of public static void main(String args[]).

Sep 13, 2013

How to create an Object in Java

How to create an Object in Java: Creating Object is very important step to execute our java program successfully. As we know we can call all non-static members only after creating object of the class so must need to create an object. We can execute non-static block without creating object below java5 version. In this post we will know how to create an object in java. Object is an instance variable of the class and it can be created by using "new" keyword.

"new" keyword is responsible for allocating memory to the object. Code to create an object is as follows-

Example-

new Java_program();
new Java_example();
new Test();

In above example we are creating an object by using new keyword. "new" kyword is allocating memory to the object anf returning reference of the object. This reference is needed to access the non-static members of the class so we have to store in a variable called a reference variable.

Example-


Java_program ob=new Java_program();

Here, ob is the reference variable which stores the referenece of the object. We can access non-static members using "ob"  as follows-
Example-

class Java_program
 {
   int a=10,b=5;
   void mul()
    {
       System.out.println("Multiplication is="+a*b);
    }
  public static void main(String argxyz[])
    {
      Java_program ob=new Java_program();
      ob.mul();
    }
 }


Internals Of Logical AND Logical OR in Java

Logical AND(1st condition && 2nd condition)-

Internals of Logical AND and Logical OR:Lets describe What is the internal mechanism of Logical AND and Logical OR.Logical AND is a Logical operator in java which returns a boolean value. If first condition false then in Logical AND, pointer does not check second condition. For second condition we use Bitwise AND.

 Logical OR(First condition || 2nd condition)-

Logical OR is also a Logical Opeartor which returns a boolean value. In Logical OR If first condition is true then pointer does not check 2nd condition . For check 2nd condition we use Bitwise OR.
For example-
Logical OR java         
int x= 10 && 20; 



Logical AND javaint y= 10 & 20;

Both operator Bitwise AND and Logical AND use with condition  but Logical AND does not use without condition. Bitwise AND use with and Without condition.

Sep 12, 2013

Internals Of Java Compiler

Internal mechanism of Compiler in java:Let us describe internals of java compiler.A Java Compiler is the collection of programs which is used to compile the java programs. Java Compiler checks java source code if syntactically correct then generates .class file (bytecode) and save into current working directory else java compiler generates compile time error.
for example-
  javac.exe testprogram.java
 Here javac is used to invoke java compiler and to load the source code from Hard Disk to Ram.

references:
  1. http://javabyvikas.blogspot.com/2013/08/compilation-java-program.html 
  2. http://en.wikipedia.org/wiki/Java_compiler

Sep 8, 2013

this reference in Java

this reference-

this keyword in java programming is used to refer the current class object. this keyword is used when we want to call methods without creating object. this reference stores in the memory when an object of the current class is instantiated. We can say "this" is a special keyword in java programming which is used to refer current class object or instance of any class. By using this keyword we can call methods of current class as well as constructor. Let us see use of this reference in java and its interesting example.
  • this is the keyword of java.
  • this reference holds the address of current class's object.
  • this reference can not be used inside any static context.
  • this reference can be used inside any non-static context.
  • this reference refers to the current class object.
  • this reference stores in the memory how many times object of the class is instantiated.
this  reference is used to call a non-static method of the current class without using the object. for example- if there is a method names "getdata", we can call this method as follows-
        this.getdata();

here, this keyword is holding the reference of the current class object so it can also call the any method of the same class. however we can also call the methods without this reference such as-
      getdata();
this reference is very useful in some cases like-

        class This1 
{
int k=200;
void m1()
{
int k=100;
System.out.println("this="+this);
System.out.println(k);
System.out.println(this.k);
}
public static void main(String[] args) 
{
This1 d1=new This1();
This1 d2=new This1();
This1 d3=new This1();
System.out.println("d1="+d1);
System.out.println("d2="+d2);
System.out.println("d3="+d3);
d1.m1();
d2.m1();
d3.m1();

}
}

Output-


this reference in java

It completes the discussion of this reference.we will discuss about this-call later. For more study about this reference please follow these links-

Aug 31, 2013

Basic Data Types in Java

Definition of Basic Data Types in Java:

Datatype (data-types) is the name of storage format which store the specific type's and its corresponding range's value.Different data types have been defined by java. These data types are frequently used by programmers. Data types in java are used to specify whether which type of value will be stored in specified variable. A variable is responsible for storing the particular value. But a variable does not store value itself. Variable is just a name for a memory location where a value is to be stored.Java has been defined different types of basic data types. We categorize them broadly into 2 category.

There are defined two types of data types in java-
  • Primitive Data Types
  • Non-Primitive/Reference Data Types

Primitive Data Types:

Primitive means ancient. As its meaning states Primitive Data Types are defined already in the Java API. Programmers can use them as their needs. There are eight types of Primitive Data Type defined in java. Let us take a look of all these eight primitive data types.
All eight primitive data types are as follows-

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char 

Data Type-byte

  • Byte data type in java is a 8-bit signed two's compliment integer.
  • Range of byte is from -128 to 127 i.e. minimum value is -128 and maximum value is 127.
  • Its default value is 0.
  • Byte data type in java is responsible to save spaces in large arrays, specifically in integer.
  • Byte data type is 4 times smaller than integer.
  • We can store a value in byte data type as- byte a=101, byte b= -40

Data Type-short

  • Short  data type in java is a 16-bit signed two's compliment integer.
  • Range of short is from -32768 to 32767 i.e. minimum value is -32768 and maximum value is 32767.
  • Short data type is also used to save memory.
  • Short data type is two times smaller than integer.
  • Its default value is 0.
  • We can store a value in short data type as- short a=10000, short b= -15000

Data Type-int:

  • Int data type in java is a 32-bit signed two's compliment integer.
  • Range of Int is from  -2147483648 to 2147483647 i.e. minimum value is -2147483648 and maximum value is 2147483647.
  • Int is specifically used as the default data type for integer values.
  • Its default value is 0.
  • We can initialize an int variable as- int a=100,int b= -2000

Data Type-long:

  • Long data type in java used when int data type is unable to store the value i.e. the value which is out of range of int data type can be stored in long data type.
  • Long data type is 64-bit signed integer.
  • Range of long is from  -9223372036854775808 to  9223372036854775807
  • Its default value is 0.
  • We can use a long type variable as- long a=150000L, long b= -250000L

Data Type-float:

  • Float data type in java used to store the fractional values i.e. the values which have decimal point. for ex- 10.25
  • float data type is  32-bit floating point .
  • Its default value is 0.0f.
  • float data type can not be used to store the currency value.
  • We can use float data type as- float a=25.45f

Data Type-double:

  • Double data type in java is also used to store the floating type values but of high range which can not be stored in float data type.
  • Double data type is 64-bit floating point.
  • It can also not be used to precise the value like currency.
  • Its default value is 0.0d.
  • We can use double data type as- double a=152.6

Data Type-boolean:

  • Boolean data type in java is used to store the logical values .
  • Boolean data type represents only one bit of information.
  • Boolean data type can store only two possible values true or false.
  • All relational operator returns the boolean type value.
  • Its default value is false.
  • We can use it as- boolean b=true

Data Type-char:

  • Char data type in java ,a 16-bit Unicode character is a very important data type..
  • Range of char data type lies between 0 to 65535 i.e. from '\uoooo' to '\uffff'.
  • Char data type is used to store any type of character. Some characters are hard to store in that situation we can make use of escape sequences to store that values.
  • We can store a value in char data type as - char a='A'



Reference Data types:

Reference data types are created by users. Reference data type are used to store the reference of the object i.e. reference data type are used to access the objects. Class,Interface,Array,Enum comes under the category of Reference Data type. A reference variable can store the reference of any reference data type and can refer to that class/interface/array/enum's object.
Default value of reference data type is null.
Example of reference data type in java-

Vehicle veh=new Vehicle();

In above example Vehicle is a class i.e. reference data type and veh is a reference variable of type Vehicle which is storing the reference of the Vehicle class's object.



We are providing a list of datatypes in a tabulated form.


Data types in java program



This is the complete description of Basic Data types in java.
Thank you. 



Aug 30, 2013

What is internal mechanism of Object creation code in java Programming

Internal Mechanism of Object creation code in java

Java:Internal mechanism of java object creating code in java-programming. Let us describe what is the main mechanism behind object creating code in java-programming. I am telling you what happens internally when an object gets created. 

If class name is My_class then object creation code will be-

My_class ob=new My_class();

We know that class is a reference data type in java. So "ob" is called as the reference variable of My_class. We know that assignment operator assigns some value from right hand side to left hand side. Here some value is being assigned to ob variable.
new operator is responsible for creating the object of the java program. We can not fetch address of the object so we assign reference of the object in a reference variable i.e."ob". Now we have the reference of object in "ob" variable by which we can access all non static variable of the My_class.


Here we are telling each term used in object creating code in java programming.


What is Object in java programming (object oriented programming)?

Objects in Java:An object oriented programming language

Definition of an Object:



An object in java programming is an instance of a class. Object is the implementation of the concept given by the class. Whenever an object is created all the non static member of the class gets activated. Object in java programming is responsible for allocating memory to all non static member of the class.
When object of the class is created , a context is created into heap memory area. In this heap memory all the non static member gets memory. The size of the context in heap is decided according to the members(if there are 2 integer variable in the class and 2 float variable then size of the context will be 16 bytes).
object_oriented_java_programming
Here is a demonstration of how a member gets memory in the heap memory area(HMA). Discussing back to the object, Basically object is used to access/call non static members of the class. Without creating object we are unable to use members of the class, but indirectly it is possible. This indirect technique of calling/accessing non static members of the class will be discussed in later posts.

Why we can't access/call non static member of the class without creating object:

We can't access/call non static members of the class without creating object because each member of the class needs memory to execute. This memory is allocated to them as object is created. So unless we create the object of  the class, we can't use these members.

How to create an Object In java:

We can  create an object by using "new" operator.

Syntax:

class name <reference variable>=new constructor_name();

Example:

If name of class is My_class then-

My_class ob=new My_class();

Code written above corresponds to create the object the My_class Class. Now question arises what behind the creation of object of My_class Class. We will know about scene behind this in Internal mechanism of Object creation code.








Aug 26, 2013

Compilation of Java Program

Compilation of Java Program







Compilation of Java program is necessary before its execution. Javac is the compiler of java which compiles the source code of java program and JVM is the interpreter which generates machine code from the compiled source code. Compiling a java program we need a compiler known as JAVAC. See how to compile a java program in java programming. Compilation of a java program contains following steps-

1-Type your java program in a text editor.

2-Save it with the name of class with .java extension in your java program.
3-Now open command prompt and go to directory in which your java program has been saved.
4-Set the path of javac using set path=”path”
5-Type javac and enter file name with .java extension and press enter.
6-If your program is error free it will generate one or more .class file.
7-Type java and then file name without any extension and press enter.

Now you will see output.

For example if your java program is saved with Test.java name. then you have to follow following steps-
  •   Javac Test.java (press Enter).
  •  Java Test (press Enter).

Let us see how to compile the java program:-

We are providing some images of compiling java program that are as follows-

  • Writing source code in an editor.
  •  Compiling java program.
  •  Executing java program


Following above steps you can execute your java program successfully.

Create a java program:


Compile program via common prompt:


Program execution (Program output):

Program output after compilation


For further reading please follow Internal Mechanism behind Java Compiler.


Java compiler references:-


Aug 20, 2013

Java Virtual Machine (JVM): JVM in Java Programming



JVM: Acronym of JVM(Java Virtual machine). JVM is the main part of java .To Know more about JVM in java programming, read this post and see related articles. Step by step java programming tutorial.


Java Virtual Machine(JVM), the powerful tool of java has made java unbeatable in the programming language's industry. JVM,In short Java Virtual Machine is also known as JVM. Unlike other language java has different way to execute the program. Every language needs a compiler to compile the source code. Java also has compiler to compile the source code but instead of compiler it also has an interpreter to interpret the compiled code.

JVM (Java Virtual Machine) : What is JVM-

Concept behind this approach is that unlike other language java is a platform independent language i.e. program written on java can be executed on any platform. Now question arises how it is possible.
Code written on java first compiled by java compiler and converted in to a middle level code which can only be understood by java interpreter named JVM. Now this middle level code also named as byte code is converted into machine code for further processing.
When a general compiler compiles the source code,it gets directly converted into machine code which can only be understood only by the platform on which it was compiled. But in java,code is processed in two phases, first creation of byte code and then its interpretation. Produced byte code is understandable by all platform with the help of JVM. Because all operating system have inbuilt JVM.
Now we can say JVM is the backbone of java. Here discussion of JVM gets over. Later we will learn many new concepts in details.


Java Virtual Machine(JVM) References:-


Thank You
Java References:-

Aug 19, 2013

Introduction to java programming language

Introduction to java programming language. Step by step explanation of java programming introduction. Here we explaining introduction and basic concepts of java programming.

Java has been defined by many authors. Most of java authors have been written much content in their posts. Readers of java can get sufficient information from those sites but they cannot know actual internal mechanism of java in those posts.

I will tell learners the reality and internal working of java in my blog. Java is a programming language made by James gosling and his team. Java was created only for electronic consumable devices but later on it grew its size and now it is divided into 3 broad fields…

1. J2SE
2. J2ME
3. J2EE

Initially core idea behind java development was to develop a language which can be used for the programming of the chips. Java developers want to handle machines by the use of remotes. They did it along with development of the area of java too.

Java is the most secure, most reliable and most powerful language which can provide a broad range of usages. Java is the platform independent language so it’s a most commonly used language in industries. We will discuss how java is platform independent later….
Thank You
Java References:-