Core Java - Program sample

1) Object Comparison:

import java.util.HashMap;
import java.util.Map;

public class TwoObjects {
   
    public static void main(String args[]){
       
        TwoObjects t1 = new TwoObjects();
        TwoObjects t2 = new TwoObjects();       
        System.out.println(t1==t2);
        System.out.println(t1.equals(t2));         
        Map<Object, String> map = new HashMap<Object, String>();       
        map.put(t1, "Shailesh");
        map.put(t2, "Bhaskar");       
        System.out.println(map.get(t1));
        System.out.println(map.get(t2));       
    }

}
Output:
false
false
Shailesh
Bhaskar


2) Character check in a String:

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

            String value = "R2D2SHA5";
            // Loop through characters in this String.
            for (int i = 0; i < value.length(); i++) {
                char c = value.charAt(i);

                // See if the character is a letter or not.
                if (Character.isLetter(c)) {
                System.out.println(c + " = LETTER");
                } else {
                System.out.println(c);
                }
              }
          }
}


Output:
R = LETTER
2
D = LETTER
2
S = LETTER
H = LETTER
A = LETTER
5


Comments

Popular posts from this blog

Java Coding Best Practices

Docker to ELK