Java Essentials

  1. Is Java pass-by-value OR pass-by-reference ? It is always pass by value. When we pass the object, we actually pass the address of the object as value. So any changes made to the object will be part of the object passed.
    Java manipulates objects ‘by reference,’ but it passes object references to methods ‘by value.
  2.   Array vs ArrayList in Java
  3. Define an array in Java
    You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).For primitive types:

    int[] myIntArray = new int[3];
    int[] myIntArray = {1,2,3};
    int[] myIntArray = new int[]{1,2,3};

    For classes, for example String, it’s the same:

    String[] myStringArray = new String[3];
    String[] myStringArray = {"a","b","c"};
    String[] myStringArray = new String[]{"a","b","c"};
    int[][] a = new int[][]{
                          {10, 20, 30, 40},
                          {15, 25, 35, 45},
                          {27, 29, 37, 48},
                          {32, 33, 39, 50},
  4.  Java String split
  5.  Substring in Java
  6. Get the object type – obj.getClass().getName()
  7. Exception:
  8. String and String Builder – Both are passed by reference only. Basically when we pass these object we are essentially passing the reference by value.
    String are immutable but StingBuilder(and StringBuffer) are mutable.
    Read post 1 and post 2 here on SO.
  9. Parse string to integer Two methods exists.
    Integer.parseInt(String) which returns a primitive int, whereas Integer.valueOf(String) returns a new Integer() object.
  10. Differences between HashMap and Hashtable?  This post is also good.
    See how to use HashMap in this example –
    Map.Entry is a key and its value combined into one class. The allows you to iterate over Map.entrySet() instead of having to iterate over Map.keySet(), then getting the value for each key. See code here or SOCheck other method to iterate the HashMap.
  11. HashMap Basic Operations
  12. HashSet
  13. ArrayList
  14. Java Comparator – Code 1, Code 2, Article 1 , Article 2,  Code
  15. length –
    ArrayList<String> listOfBanks = new ArrayList<>();
    int size = listOfBanks.size();int[] arr = new int[10];
    int arrayLength = arr.length;

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s