Tuesday, 27 January 2015

Program for Shifting characters in a String eg. String "abcde" should be printed as "eabcd"



import java.util.Scanner;

public class SB
{
public static void main(String []args) {

Scanner in = new Scanner(System.in);

         System.out.println("Enter a string to shift characters");
      String  test = in.nextLine();


 For further visit    Javainhouse
Read More »

Saturday, 24 January 2015

Find length of String without using any length or size method

import java.util.Scanner;


public class SB

{

public static void main(String[] args) {

{
String test;
      Scanner in = new Scanner(System.in);

     For further program please visit my new blog http://javainhouse.blogspot.com










Read More »

Print "welcome bangalore" as welcome erolagnab

import java.util.Scanner;


public class SB

{

public static void main(String[] args) {

{
String test="welcome bangalore";

String reverse="";


 int i=0;

 String a[]=test.split(" ");


 for (int j = a[1].length()-1; j >= 0; j--)
 {

reverse = reverse + a[1].charAt(j);
 }


 a[1]=reverse;
 test=a[0] +" "+ a[1];
 System.out.println(test);



}
}
}











Read More »

Thursday, 22 January 2015

Program in java to check whether a String is palindrome or not

import java.util.*;
public class Palindrome
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
 
Program in java to check whether a String is palindrome or not       System.out.println("Enter a string to check palindrome");
      original = in.nextLine();
 
      int length = original.length();
 
      
     
      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);
 
      if(reverse.equals(original))
      System.out.println("Yes String is palindrome");
      else
       System.out.println("NO String is not palindrome"); 
   }
}
Read More »

Program to split a String without using split() method



Program to split a String without using split() method import java.util.*;
public class SplitString
{
public static void main(String[] args) {
   String[] s = {"Netherlands_Iceland_Norway_Denmark"};
   String[] finalString = mymethod(s);      
   for (int i = 0; i < s.length; i++) {
       System.out.println("" + finalString[i]);
   }
}

static public String[] mymethod(String[] mystring) {
   String ss[] = new String[mystring.length];
   for (int j = 0; j < mystring.length; j++) {
       ss[j] = mystring[j].replace('_', ' ');
   }
   return ss;
}
}

Read More »

Monday, 19 January 2015

Introduction to JAXB and its Architecture

JAXB means JAVA API for XML Binding.

Introduction to JAXB and its Architecture In simple words, it means an API used to bind XML with JAVA objects.

JAXB provides an efficient and easy way to bind XML data to JAVA which is very helpful for java developer ,Mostly used in case of web services as data travel between web services and client in form of xml which is further needed to be converted into java object as a need for java application.This need can be fulfilled by JAXB.

Mainly two things can be understood as working of JAXB.
 1.       Conversion of java objects to XML which is known as Marshalling.
 2.       Conversion  of XML data to java object ,known as unmarshalling.



JAXB Architecture:



JAXB Architecture


JAXB Architecture consists of following components:

Schema Compiler
Schema Generator
Binding Runtime Framework

Schema Compiler is used to bind source schema with schema derived program elements.

Schema Generator maps set of Program elements to derived schema .This mapping is described by annotations.

Binding Runtime Environment : Provides unmarshalling (reading) and marshalling (writing) operations for accessing, manipulating, and validating XML content using either schema-derived or existing program elements.
Read More »

Saturday, 20 December 2014

How Hashmap works internally in java

Read More »

Wednesday, 10 December 2014

Difference between equals() and ==

Difference between equals() and ==

Difference between equals() and ==

Hi all , these days i\'m about to write a post on very fashionable interview question that is what\'s the difference between equals and == .It is asked in every object familiarised language interview whether or not its Java or ASP.NET For this you must apprehend the essential difference between each .

1. The very first difference in both is that equals ()  is a  method == is an operator . this can be the terribly basic factor you must understand equals and ==
As in java there\'s no concept of operator overloading thus we cannot modify the behavior of == operator.

2. “= = “operator : “= =” is a binary operator used to compare both primitive sorts like int ,Boolean and objects .
While examination primitive sort its works properly and returns right output
For example :


int a=1;
int b=2;

if(a==b)

else


Output : Not Equal;

While examination 2 objects using “==” ,a problem arises is that “= =” checks reference of each objects not price and returns output correspondingly .

This can be illustrated with the assistance of associate example :










String variable1=new String(“First”);
String variable2=new String(“First);

If(variable1==variable2)

else


Output: Not Equal




In the example above as variable1 and variable2 ar referring to 2 totally different objects despite of same price .Accordingly “==” operator can come false compared each







String variable1=new String(“First”);
String variable2=variable1;



If(variable1==variable2)

else


Output: Equal

In the example above as variable1 and variable2 ar referring to 2 same objects .Accordingly “==” operator can come true compared each




3. equals () :  equals() method is used to compares the worth of 2 objects . this method is defined in Object class and can even be overridden to change the working

String variable1=new String(“First”);
String variable2=variable1;



If(variable1.equals(variable2))

else


Output: Equal


In the example above as variable1 and variable2 are referring to 2 totally different objects with same price .Accordingly equals() will come true compared each.

you may like :

How objects are stored in heap ?
Read More »

Sunday, 7 December 2014

How to create custom Exception in Java

How to create custom Exception in Java

Custom Exception

Why we need custom Exception ?
How to create a custom exception ?
Write code to create a Custom Exception

All these are questions usually asked in a Java interview .So my today's post on how to create a custom exception in java .

As we know There are two types of Exception :
1. Checked Exception
2. Unchecked Exception

Both these exceptions have a common parent class Exception .So ,in same order if we have to create a custom exception ,we need to extend Exception class.

Ok , let me explain the process step by step .

1. Any custom exception should extend Exception Class .

2. There should be a proper name given to the Exception ,For example if there is an exception related to Invalid age name can be given as InvalidAgeException
public class InvalidAgeException extends Exception {}



public class InvalidAgeException extends Exception {
private int age;

public InvalidAgeException ()
{
super();
}
public InvalidAgeException (String message, int age)

{
super(message);

this.age = age;

} 

}


In the above way we can define two constructors of InvalidAgeException with message and without message

Usage of custom Exception:

Below is the program with usage of custom exception.

public class CustomExceptionDemo 


{ 

private static final Map<Integer, String> employee = new HashMap<>(); 

static

 {
employee.put(100, "Mahesh");

employee.put(101, "Suresh");

 employee.put(102, "Bran");

 employee.put(103, "Troy"); }

 public static void main(String args[]) { 

CustomExceptionDemo t = new CustomExceptionDemo();

t.getAge(1000); } 

public String getAge(int age)

 { 

if (employee.get(age) == null)

{ 

throw new InvalidAgeException ("No such employee exists", age);

} return employee.get(name);

}

}





SO friends this was my post on Custom Exception .
Read More »

Sunday, 30 November 2014

What is design pattern in java?

design pattern in java

Design Pattern  :



Design pattern are  answer to common occurring problem in software system development . Design pattern are language independent solution that are developed to solve common object oriented issues .
They if used sensibly helps to extend code maintainability , reduces learning curve whereas developing a bit of code ,makes development quicker ,reduces TCO (total value  output) and helps to simply understand solution to new developer.


Singleton Design Pattern :
Singleton Design pattern uses a singleton class which should have a single instance.
Singleton design pattern ensures that at a single point of time only a single instance should be present in JVM Singleton class should also provide a method which acts as a single point for global access of that class.

Common approach for a singleton pattern :
1. Singleton class should have a private constructor so that no other class can instantiate it .
2. Singleton class should have a private static variable of the class that will be the only instance of that class.
3. A public static method which is the only global access point for outer world.

You may like :
Difference between equals() and = =

Read More »