Menus

Dec 24, 2018

Explain different ways of creating a thread.Which one would you prefer and why ?


Java defines two ways by which a thread can be created.

By implementing the Runnable interface.
By extending the Thread class.


#Implementing the Runnable Interface
class MyThread implements Runnable
{
public void run()
{
System.out.println("concurrent thread started running..");
}
}

What is dependency injection?


IoC is all about inverting the control. To explain in layman's term, suppose you drive a car to your work place, it means you control the car. IoC principle suggests to invert the control, meaning instead of driving the car yourself, you hire a cab where another person will drive the car. Thus it is called inversion of the control from you to the cab driver. You don't have to drive a car yourself and let the driver do the driving so that you can focus on your main work.

IoC principle helps in designing loosely coupled classes which make them testable, maintainable and extensible.

The Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about removing dependencies from your code.

Nov 23, 2018

Is java pass by value or pass by reference?


Java is Strictly Pass by Value.
Lets take an example for PRIMITIVE TYPE: Here an int is passed to the function “change()” as parameter and as a result the change in the value of that integer is not reflected in the main method.Because, Java creates a copy of the variable being passed in the method and then do the manipulations like in c/c++. Hence the change is not reflected in the main method.

And for the object type: In Java, all primitives like int, char, etc are similar to C/C++, but all non-primitives (or objects of any class) are always references. So it gets tricky when we pass object references to methods. Java creates a copy of references and pass it to method, but they still point to same memory reference. Mean if set some other object to reference passed inside method, the object from calling method as well its reference will remain unaffected. Actually java passes object reference as a value too.

Why wait and notify is declared in Object class instead of Thread ?






Suppose a gas station has a single toilet, the key for which is kept at the service desk. The toilet is a shared resource for passing motorists. To use this shared resource the prospective user must acquire a key to the lock on the toilet. The user goes to the service desk and acquires the key, opens the door, locks it from the inside and uses the facilities.

Meanwhile, if a second prospective user arrives at the gas station he finds the toilet locked and therefore unavailable to him. He goes to the service desk but the key is not there because it is in the hands of the current user. When the current user finishes, he unlocks the door and returns the key to the service desk. He does not bother about waiting customers. The service desk gives the key to the waiting customer. If more than one prospective user turns up while the toilet is locked, they must form a queue waiting for the key to the lock. Each thread has no idea who is in the toilet.

Why char array is preferred to store password than String in Java?





1) Strings are immutable: Strings are immutable in Java and therefore if a password is stored as plain text it will be available in memory until Garbage collector clears it and as Strings are used in String pool for re-usability there are high chances that it will remain in memory for long duration, which is a security threat. Strings are immutable and there is no way that the content of Strings can be changed because any change will produce new String.

With an array, the data can be wiped explicitly after its work is complete. The array can be overwritten and and the password won’t be present anywhere in the system, even before garbage collection.

2) Security: Any one who has access to memory dump can find the password in clear text. So Storing password in character array clearly mitigates security risk of stealing password.

3) Log file safety: With an array, one can explicitly wipe the data , overwrite the array and the password won’t be present anywhere in the system.
With plain String, there are much higher chances of accidentally printing the password to logs, monitors or some other insecure place. So char[] is less vulnerable.

Mar 17, 2017

Exception Handling In Java

Course Contents:
1.   Error and exception
2.   Types of exception
3.   Mechanism for exception handling

Error
It is common to make mistakes while developing as well as typing a program. A mistake might lead to an error causing the program to produce unexpected results. An error may produce an incorrect output or may terminate the execution of the program abruptly of even may cause the system to crash. It is therefore important to detect and manage properly all the possible error conditions in the program so that the program will not terminate or crash during execution.

Types of errors
Errors are broadly classified into two categories:
- Compile time errors
- Run-time errors

Jul 29, 2016

Package and Interface in Java

Course contents:
  1. Content
  2. Defining a package
  3. Access protection
  4. Importing packages
  5. Defining interfaces
  6. Applying interfaces 

An interface is similar to a class that can contain only constants, group of related method signatures, and nested types. There is no method body that is we cannot define methods inside the interface.

Interfaces cannot be instantiated they can only be implemented by classes or extended by other interfaces. Those classes that implement the interface must define the methods that are declared inside the interface. An interface is defined much like class.

The general syntax of interface definition is
Access specifier interface interfacename
{
 return type method-name1(parameter-list);
 return type method-name1(parameter-list);
 return type method-name1(parameter-list);
 return type method-name1(parameter-list);
 type variable-name1 = value;
 type variable-name2 = value;
 ……………..
 …………….
 return type methodnameN(parameter-list);
 type variable-nameN = value;
}
Here, access specifier is optional. When an access sepecifier is not specified then interface is only available to members of the package in which it is declared. When it is declared as public, the interface can be used by any other code.

Java nested class, call-by calue, call-by-reference

Nested class
A class defined within another class is known as nested class. The scope of a nested class is bounded by the scope of its enclosing class. Thus if class B is defined within class A, then B is known to A, but not outside of A.

A nested class (that is inner class) has access to the members, including private members of the class in which it is nested. However, the enclosing class does not have access to the members of the nested class.
WAP that demonstrate the concept of an inner class
class Outer
{
 int outer_x=100;
 void test()
 {
 Inner inn = new Inner();
 inn.display();
 }
 class Inner// this is a inner class
 {
 void display()
 {
 System.out.println("Display:outer_x = " +outer_x);
 }
 }
}

class InnerClassDemo
{
 public static void main(String args[])
 {
 Outer out = new Outer();
 out.test();
 }
}

Contact Form

Name

Email *

Message *