Thursday 21 November 2013

Eclipse Shortcuts Key

By Using shortcuts key make a developer more productive. Eclipse provides keyboard shortcuts for the common actions. Using shortcuts is usually preferable as you can perform actions much faster.

FILE NAVIGATION – ECLIPSE SHORTCUTS

CTRL+SHIFT+R – Open a resource. You need not know the path and just part of the file name is enough.
CTRL +E – Open a file (editor) from within the list of all open files.
CTRL +PAGE UP or PAGE DOWN – Navigate to previous or next file from within the list of all open files.
ALT +Left Arrow or ALT+Right Arrow – Go to previous or next edit positions from editor history list.

Java Editing-

CTRL+SPACE – Type assist
CTRL+SHIFT+F – Format code.
CTRL+O – List all methods of the class and again CTRL O lists including inherited methods.
CTRL +SHIFT +O – Organize imports.
CTRL+SHIFT +U – Find reference in file.
CTRL+ / – Comment a line.
F3 – Go to the declaration of the variable.
F4 – Show type hierarchy of on a class.
CTRL +T – Show inheritance tree of current token.
SHIFT +F2 – Show Javadoc for current element.
ALT+ SHIFT +Z – Enclose block in try-catch.

GENERAL EDITING SHORTCUTS

F12 – Focus on current editor.
CTRL +L – Go to line number.
CTRL +D – Delete a line.
CTRL+<- or -> – Move one element left or

Search from an editor

Ctrl + . Go to the next problem / error
Ctrl + , Go to the previous problem / error
F4 on a variable Show type hierarchy
Ctrl + J Incremental search without popup dialog, just starting typing to search. Press Ctrl + J to find the next match
Ctrl + K Searches the selected text or if nothing is selected the last search from the Find dialog.
Ctrl + Shift + G In the Java editor, s\earch for references in the workspace
Ctrl + Shift + P Select the matching bracket. Cursor needs to be placed before or after a bracket.

Running programs
Ctrl + F11 Run last launched
Alt + Shift + X, J Run current selected class as Java application

Create new lines
Shift + Enter Adds a blank line below the current line and moves the cursor to the new line. The difference between a regular enter is that the currently line is unchanged, independently of the position of the cursor.
Ctrl+Shift+Enter Same as Shift + Enter but above

Variable assignment
Ctrl + 2, L Assign statement to new local variable
Ctrl + 2, F Assign statement to new field

Re-factoring
Alt + Shift + R  Rename
Ctrl + 2, R Rename locally (in file), faster than Alt + Shift + R
Alt + Shift + T Opens the context-sensitive re-factoring menu, e.g. displays

Must known shortcuts
Ctrl + S Saves current editor
Ctrl + 1 Quickfix; shows potential fixes for warnings, errors or shows possible actions
Ctrl + Space Content assist/ code completion
Ctrl + Q Goes to the last edited position
Ctrl+ D Deletes current line in the editor
Ctrl + Shift + O Adjusts the imports statements in the current Java source file
Ctrl + 2, L or F Assign statement to new local variable or field
Ctrl + Shift + T Open Type Dialog
Ctrl + O Shows quick outline of a class
Ctrl + F11 Run last launched application

@References-
Eclipse Official site

Thursday 7 November 2013

Java Interview Question Part-3


Q- Difference Between Serialization and Externalizable interface?
Serialization is Marker interface which doesnt contains any method.The implemented class just gives the instruction to JVM to behave differently for the implemented class.the main goal of serilaliztaion is persist the state of an object into a stream.
By implementing java.io.Serializable, you get "automatic" serialization capability for objects of your class. No need to implement any other logic, it'll just work. The Java runtime will use reflection to figure out how to marshal and unmarshal your objects.

In earlier version of Java, reflection was very slow, and so serializing large object graphs was a bit of a performance problem. To handle this situation, the java.io.Externalizable interface was provided, which is like java.io.Serializable but with custom-written mechanisms to perform the marshalling and unmarshalling functions (you need to implement readExternal and writeExternal methods on your class).It can be useful when there is a transient variable in your class and the value can be calculated from the object's state.

Q- Write a program in java to find the lenghth of string without using any length or size|().

int findStringLength(String str)
{
char[] elements = str.toCharArray();
int count=0;
for(char c : elements){
count++;
}
return count;
}


Q.what is generic Eraser in java?
When we implement the generic then compiler does these things
1. Type casting if necessary to preserve type safety.
2. Resolve all the cases at compile time. Make safe to run.
3. Generate a bridge to gain polymorphism.

And after this compiler removes the generic type and at runtime it is executed without specific type.
It means bytecode of List is same as bytecode of List.

As per the oracle docs,

Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming. To implement generics, the Java compiler applies type erasure to:

Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
Insert type casts if necessary to preserve type safety.
Generate bridge methods to preserve polymorphism in extended generic types.
Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.



Q.If new object is created by thread then where object's attributes are going to be created? stack or heap?

class Myclass{

String str;
MyObject object;

public void m1(){

str = new String("hello");
object = new MyObject();
int i = 10;
}
Suppose now thread A creates object of this class and calls method() then where data, object and i will be created? heap or stack of thread?

Object is allocated in Heap, but reference push to the stack. Also you should know that each thread has his own stack.
All four objects: MyClass, MyObject, String str and "hello" are created in Heap area. In fact there is no way an object can be "created" in stack area. Nevertheless, variable 'i' would be created in the stack of m1().
It does not matter whether "Thread" indirectly creates an object or JVM, objects would always be created in the Heap area only.





Find Duplicate Characters In String using Java

package com.zia.test; import java.util.HashMap; import java.util.Map; import java.util.Set; public class findDuplicateCharacter { /**...