Tuesday, November 29, 2016

Difference between == and equals method with example

Hello Guys,

How == operator and equals method works in java.
So many people ask this question in Java interview and some also give a little program like mentioned below to explain.

Here,we have example for learning about very basic stuff of Java

You can directly run the below program and can see the output.


public class Test {

 public static void main(String[] args) {
String s1= "hi";
String s2= new String("hi");

    if(s1==s2)
System.err.println("Ist case: s1==s2:result:- "+"++++" +" hascode of S1:= "+s1.hashCode()+ " S2: "+ s2.hashCode());

System.out.println("Ist case: s1.equals(s2):result:- "+ s1.equals(s2));

s2=s1;

   if(s1==s2)
  System.err.println("2nd case: s1==s2:result:- "+"_-___"+ "hascode true or false of S1:= "+(s1.hashCode() == s2.hashCode()) );


System.out.println("2nd case: s1.equals(s2):- "+ s1.equals(s2));

    }
}

OutPut:-
----------
Ist case: s1.equals(s2):result:- true
2nd case: s1==s2:result:- _-___hascode true or false of S1:= true
2nd case: s1.equals(s2):- true


Any Suggestion and comments are most welcome!

Thanks,
Vikas



How to Print HashMap in Java


Hello Guys,

How to Print HashMap is very common and frequently ask question now a days for testing programming skills in Java.

So,lets try to understand the  program given below.


import java.util.HashMap;
import java.util.Set;


public class Test extends Thread{

public static void main(String[] args) {
//Creating HashMap key and value as String.
HashMap<String, String> st = new HashMap<String, String>();
//Putting some key and value in Map

st.put("K1", "V1");
st.put("K2", "V2");
st.put("K3", "V3");
st.put("K4", "V4");
st.put("K5", "V5");

//Now iterate or loop Map by using KeySet method of Map
for(String name: st.keySet()){

String key= name.toString();
//Here we are calling get method of Map class by passing key which is we got from KeySet()
String value= st.get(name).toString();
System.out.println("Key:- "+ key +"value:- "+ value );
}
}
}
note:-
1)KeySet returns set view of the keys contained in map.
2)Changes to map will reflect to set and vice-versa also possible.

Its very simple Program and can be use by beginners who just started learning java and collection.

Any comment or Suggestion is most welcome!


Thanks,
Vikas






Tuesday, October 25, 2016

java.lang.OutOfMemoryError: Java heap space

Out Of Memory issue occurred mostly at 2 places.
1)    The java.lang.OutOfMemoryError: Java heap space
2)    The java.lang.OutOfMemoryError: PermGen space

Before discussing the OutOfMemory, would like to give brief detail about heap and non-heap spaces.

Heap
The JVM has a heap that is the runtime data area from which memory for all class instances and arrays are allocated. It is created at the JVM start-up.

If you are familiar with different generations on the heap.
You may aware about new, old and permanent generation of heap space.

Non-Heap
The JVM has memory other than the heap, referred to as non-heap memory. It is created at the JVM startup and stores per-class structures such as runtime constant pool, field and method data, and the code for methods and constructors, as well as interned Strings.

2nd issue or PermGen Space related is coming when Non-Heap Memory is full.

1st Reason is:-
Permanent generation(PermGen) of the heap is used to store String pool and various Metadata required by JVM related to Class, method and other java primitives.

If Project is big, you can easily run out of memory if you have too many unnecessary classes or a huge number of Strings in your project and also not managing object creation.
To resolve this kind of issue you can increase the size of permGen by
export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m

2nd Reason is:-
Memory leak through classLoader.
Most likely occurred by webserver or application server.

Sharing some points with you.

1)Check the Jconsole(for running jconsole, just open cmd and type jconsole&)


In highlighted area you can see the heap and non-heap memory is too high and can cause OutOfMemory.

As we know GC is being called and it will release the space in heap, you can see the figure attached below.
Here I came to know that, server is loading same class again and again on each job run.
So for solving this ,I restrict the class to load again and removed unnecessary String literal also.


So solution can be used:

1) Remove any class which load every time on each call,for an example loading JWSDynamicClientFactory for each soapcall.
2)Remove any unused or unnecessary String creation.
3) Check File I/O operation id happening properly or not


Any suggestion and Questions is most welcome.

Thanks,
Vikas




Friday, August 5, 2016

How to check if file is modified or not in JAVA

Hi Guys,

Below program is use to track the changes in the file.


package com.vks.example;

import java.io.File;

public class FileModifed {

  private static long lastModified = 0L;


static {

lastModified=new File("C://example.txt").lastModified();
  }
public static void main(String[] args) {

//calling isFileModied method in loop to check if file is modified or not.A infinite for loop to check file is modified or not.
  for (int i = 0; i >= 0; i++) {

if (isFileModified())
break;
  }

}

public static boolean isFileModified() {

  File file = new File("C://example.txt");
  if (lastModified >= file.lastModified()) {
  System.out.println(lastModified + "-------- Not Modified-------" + file.lastModified());
return false;
  } else {
lastModified = file.lastModified();
System.out.println("-------- Modified-------");
return true;
  }
    }

}

Any comments or Suggestion are most welcome!!

Thanks,
Vikas