Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Jul 20, 2013

Concurrent Modification Exception in Collections in Java

When more than one iterator accessing the same collection reference , then this concurrent modification exception will occur.
Ex:-
      List list = new LinkedList();
       Iterator it1 = list.iterator();
       Iterator it2 = list.iterator();
          it1.next();
           it1.remove();
          it2.next();

The above code will surely end up in concurrent modification exception.
   
 Say now, if this is the case , how to tackle this exception.
  You can have any number of iterators , make all your iterators readable , make only one read and write.
Tags:-
   

Oct 5, 2012

Pre and Post Increment Operator in Java

         In Java we have pre and post increment operator .. Pre increment will increase the value at the start and take the value. Post Increment will take the value and increment it.
For instance,
 if a=5;  a++ and ++ a will be printed as 5 and 6 receptively finally the value of a will be 6 for a.
       Now lets play a game with a logic. If you understand the above mentioned elementary part, then you can win this game easily.
 int a =3;
int k= a++ + a++ + ++a;
Whats the value of k ?
..
..
k=13, try it out.    k= 3 + 4 + 6=13 and a=6.
People also visited  String To Byte Array And Byte Array To String Code In Java   Creating JAR File Using Eclipse IDE    Accessors And Mutators In JAVA

Sep 28, 2012

String to Byte Array and Byte Array to String code in Java

       String to Byte Array  conversion  Logic and vice versa Byte Array to String in Java. When i came across such a situation to covert my String to Byte Array i tough it will be a complex logic.. But Java Libraries gives me a easy one line step that ease my job.


public byte[] stringToByte(String s) {
return s.getBytes();
}



public String byteToString(byte[] b) {
return new String(b);
}

People also Visited





Feb 21, 2012

Creating JAR File using Eclipse IDE

Jar is a collection of ally your class files.IDE makes your work  simpler than ever. Eclipse IDE whichs been commonly used by almost all the JAVA Developers, supports the funtionality to create a jar file from your project.

Steps:

  •   Right Click your project, you will be getting an option called export, click on that 
  • Now you will be getting an window, to select Java/ Jar File  
  • Now click Next, there you mention the name of Jar File and location where you want the files to be generated.
  • Click Finish , the Jar file will be generated @ that location. To execute the Jar file , open command prompt and type  "java -jar  "

Feb 20, 2012

Java Program without a Main Function

Writing a simple program in JAVA to print "Hello world " is not a big task for you, if you are a programmer in JAVA. But i m going to teach you how to write a program without a main function.
  You may ask me , every program starts with Main Function and how come its possible to do without a main method. Java provides you to use static initialization block to achieve this..

Ex:

public class Sample{
 static{
System.out.println("Hello World !");
 
}


By running this class, you will get an error message stating that "java.lang.NoSuchMethodError: main
Exception in thread "main" " . To avoid this, add System.exit(0); at the end of static initialization block.

public class Sample{

static{
System.out.println("Hello World !");
  System.exit(0);
}
}

Feb 16, 2012

Working with Constructors

In Java, When we  are creating a new instance of the class, the constructor of the class will be called. If a class doesn't have a constructor then it will assume there's a default constructor. But constructors are used to assign some initial values  to class fields.
Ex:
public class Employee{


  private int empID;

  private String name;


   public Employee(){
    this.empID = 1
    this.name = "Un-defined"
   }
}
We all know that we cant have two constructor in a class, with the same name and same parameters but a different data-type. But while i m analyzing this limitation, suddenly a doubt arise to me whether we can have two constructors with a single parameter, but the data Type of these parameters are different.

Ex:

public class Employee{


// Constructor 1
   public Employee(int empID){
   }


// Constructor 2

  public Employee(String name){
   }



}

Feb 15, 2012

Immutable Class in Java

A class is immutable, only if when the instance of the class cant modify its content.. This can be achieved by

  • Making a class as Final
  • Make all the fields and methods as private.
Ex:
  • String Class 
    • When you create an instance of String class, say ex: new string(); then you cant modify the content  of String class with this object reference. 

Oct 14, 2011

Accessors and Mutators in JAVA

Accessors:
API's or methods which were used to get the current state of an object.

Mutators:
API's or methods which were used to change the state of an object.
Ex: set(int a);

An object has mainly two things state and behaviour.. State denotes the instance variables of a class and Behaviour denotes the methods or API's in class[ which is blueprint of an object] .
class Animal{
private int color;

  public void getColor(){
  return color;
  }
public setColor(int color){
this.color = color;
}
}

In this program, getColor is an Accessor setColor is Mutator

Aug 18, 2011

Mulitple Catch Blocks in JDK 7

public static void main(String args[]) {
Scanner scnr = new Scanner(System.in);
String number = scnr.next();
try {
if (number.length() > 5) {
throw new IllegalArgumentException();
}
Integer.parseInt(number);

} catch (NumberFormatException | IllegalArgumentException e) {
e.printStackTrace();
}
}

Aug 17, 2011

Print a Diamond using JAVA


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Diamond {

public static void main(String[] args) {
Diamond diam = new Diamond();
diam.checkInput();
}

private void draw(int N, int counter, String sym) {
int max=0;

for (int i = 0; i < N;) {

for (int j = 0; j <= (N - i) / 2; j++) {
System.out.print(" ");
}
for (max = 0; max <= i; max++) {
System.out.print(sym);


}

System.out.println();

i = i + counter;


}


for (int i = (max - counter); i > 0;) {

for (int j = 0; j <= (N - i) / 2; j++) {
System.out.print(" ");
}
for (int k = i; k > 0; k--) {
System.out.print(sym);
}

System.out.println();
i = i - counter;

}
}

private String getInput() {
String value = null;

BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
try {
value = buf.readLine();

} catch (IOException e) {
e.printStackTrace();
}
return value;
}

private void checkInput() {
System.out.print("Enter the Value of N:");
int N = Integer.parseInt(getInput());
System.out.print("\n Enter the Value for counter: ");
int counter = Integer.parseInt(getInput());
System.out.println("\n Enter the Diamond Symbol:");
String symbol = getInput();

System.out.println("Vaildating Check wait for 2 sec");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {

e.printStackTrace();
}
if( N>0  && N%2 !=0  && counter >0 && counter%2 == 0 && counter
draw(N, counter, symbol);
}
else{
System.out.println("invalid data");
}



}

}