Month: May 2017

Unordered Data using HashMap in Java

Understanding through Code import java.util.HashMap; import java.util.Map; public class HashMapDemo { public static void main( String[] args ){ Map<String, String> map = new HashMap<>(); map.put("India", "First"); map.put("US", "Second"); map.put("Canada", "Third"); System.out.println(map); map.put("Russia", "Fourth"); System.out.println(map); String mapString = map.get("US"); System.out.println("The US stands at: " +mapString); System.out.println(mapString); map.remove("Russia"); } } Output {Canada=Third, US=Second, India=First} {Canada=Third, US=Second, India=First, Russia=Fourth} The US stands at: Second Second Notes HashMap is unordered data collection. HashMap is implementation of Map. Just like ArrayList is an implementation of List. Maps are in Key, Value pairs. You can look up an item in HashMap with the help of Key.

Resizable Array in Java with ArrayList

Understanding through Code import java.util.List; import java.util.ArrayList; public class ArrayListDemo { public static void main( String[] args ){ List<String> list = new ArrayList<>(); list.add("India"); list.add("US"); list.add("Canada"); System.out.println(list); list.add("Russia"); System.out.println(list); list.remove(1); System.out.println(list); String nation = list.get(0); System.out.println("The first nation is: " +nation); int pos = list.indexOf("Canada"); System.out.println("Canada is at: " +pos); } } Output [India, US, Canada] [India, US, Canada, Russia] [India, Canada, Russia] The first nation is: India Canada is at: 1 Notes Collection framework is a set of interfaces or classes that make management of data easy. The two most commonly used frameworks are: ArrayList and HashMap. <String> is ... Read more

Passing Arguments in Java

Passing Values to Methods Passing to a method by copy The method receives a copy of variable. Passing to a method by reference. The method receives a reference to the original object. That is, any changes made to the object within the method will be reflected outside the method In Java, variables are always passed by copy Passing Primitive Values void incValue( int i ) { i++; System.out.println("Inside method values of i is: " +i); } int iOriginal = 10; System.out.println("iOriginal before: " +iOriginal); incValue(iOriginal); System.out.println("Original after: " +iOriginal); After running the above pseudo code we find that: Original before: ... Read more

Method Overloading in Java

Functions are called methods in Java You can create a number of methods with same name in Java. But, you must distinguish them by: Number of arguments Arguments data type Understanding through Code import java.util.Scanner; public class MethodOverloading { public static void main( String[] args ){ String s1 = getInput("Enter value 1: "); String s2 = getInput("Enter value 2: "); String s3 = getInput("Enter value 3: "); double result = addValues(s1, s2); System.out.println("The answer is: " +result); double result1 = addValues(s1, s2, s3); System.out.println("The answer for 3 values is: " +result1); double result2 = addValues(s1, s2, s3, s1, s2, s3); ... Read more

Try and Catch in Java

Exception When your code can throw an exception (error on run time), you can wrap that code in try and catch block. Example: ArrayIndexOutOfBoundsException Code public class TryAndCatch { public static void main( String[] args ){ String str = "Hello"; char[] charArr = str.toCharArray(); try { char lastChar = charArr[charArr.length]; System.out.println(lastChar); } catch(ArrayIndexOutOfBoundsException e) { e.printStackTrace(); //System.out.println("Index number out of scope"); } } } Output java.lang.ArrayIndexOutOfBoundsException: 5 at TryAndCatch.main(TryAndCatch.java:8) Multiple Catch Blocks public class TryAndCatch { public static void main( String[] args ){ String str = "Hello"; char[] charArr = str.toCharArray(); try { char lastChar = charArr[charArr.length - 1]; System.out.println(lastChar); ... Read more