Solved

mapAB Challlenge

Posted on 2016-09-29
35
200 Views
Last Modified: 2016-10-21
Hi,

I am working on below challenge.
http://codingbat.com/prob/p107259
Psedo code:
1. check if mp has a and b as key
2. if yes append keys and values and return map

I wrote my code as below and passing all tests
public Map<String, String> mapAB(Map<String, String> map) {
  if(map.containsKey("a")&&map.containsKey("b") ){
    map.put("ab",map.get("a")+map.get("b"));
  }
  return map;
}

Open in new window


Any improvements or alternate approaches?      

please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 21
  • 9
  • 4
  • +1
35 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 41823042
Nothing to improve here. It just works as it should.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41823044
Looks fine.  I can't see any obvious problems/inefficiencies.
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41823073
In the current case it's almost correct (A grade). Cause the task does not specify whether the value can be null or not.

As concurrent access to the map is possible, cause it's not excluded by the task, the optimal solution is (A+):

public synchronized Map<String, String> mapAB(Map<String, String> map) {
  if(map.containsKey("a") && map.containsKey("b") ){
    map.put("ab", map.get("a" ) + map.get("b"));
  }
  
  return map;
}

Open in new window


When the value cannot be null, then the following is better, cause it performs slightly better for the normal Map classes:

public synchronized Map<String, String> mapAB(Map<String, String> map) {
  String a = map.get("a");
  String b = map.get("b");
  if(a != null && b != null)
  {
    map.put("ab", a + b);
  }
  
  return map;
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 41823457
if a or b are null map.get("a" ) or map.get("b") gives null pointer exception? please advise
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41823462
Test it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823508
public Map<String, String> mapAB(Map<String, String> map) {
  if(map.containsKey(null)&&map.containsKey("b") ){
    map.put("ab",map.get("a")+map.get("b"));
  }
  return map;
}

Open in new window

'
like above?
passing a as null in the a place?

Expected      Run            
mapAB({"b": "There", "a": "Hi"}) → {"b": "There", "a": "Hi", "ab": "HiThere"}      {"b": "There", "a": "Hi"} missing: "ab": "HiThere"      X      
mapAB({"a": "Hi"}) → {"a": "Hi"}      {"a": "Hi"}      OK      
mapAB({"b": "There"}) → {"b": "There"}      {"b": "There"}      OK      
mapAB({"c": "meh"}) → {"c": "meh"}      {"c": "meh"}      OK      
mapAB({"b": "bbb", "c": "ccc", "a": "aaa", "ab": "nope"}) → {"b": "bbb", "c": "ccc", "a": "aaa", "ab": "aaabbb"}      {"b": "bbb", "c": "ccc", "a": "aaa", "ab": "nope"}      X      
mapAB({"b": "bbb", "c": "ccc", "ab": "nope"}) → {"b": "bbb", "c": "ccc", "ab": "nope"}      {"b": "bbb", "c": "ccc", "ab": "nope"}      OK
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41823514
>> if a or b are null map.get("a" ) or map.get("b") gives null pointer exception
If you're talking about the String variables with the names 'a' and 'b' the above statement is incorrect.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823542
>> if a or b are null map.get("a" ) or map.get("b") gives null pointer exception
If you're talking about the String variables with the names 'a' and 'b' the above statement is incorrect.

yes. i have to pass Strings only right for this method? how do i test with null in this case to see NPE throws or some other exception throws?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41823560
ste5an was talking about the situation where the value stored for the key "a" (or "b") is null.
(However, you can't test that on the codingbat website, since you don't have control over the tests)
In that case your code would lead to a NPE.
To avoid that you could write:

public Map<String, String> mapAB(Map<String, String> map) {
  if (map.containsKey("a") && map.containsKey("b")) {
      if (map.get("a")==null && map.get("b")==null) {
          map.put("ab", null);
      } else {
          map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
      }
  }
  return map;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41823576
i will test in eclipse probably to recreate NPE
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823764
package test;

import java.util.Map;

public class TestMap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));

	}

	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		      if (map.get("a")==null && map.get("b")==null) {
		          map.put("ab", null);
		      } else {
		          map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		      }
		  }
		  return map;
		}
}

Open in new window


above code in eclipse gives error at line 9


Multiple markers at this line
      - Syntax error, insert ")" to complete MethodInvocation
      - The method mapAB(Map<String,String>) in the type TestMap is not applicable for the
       arguments ()
      - Syntax error, insert ";" to complete BlockStatements
      - Syntax error, insert "}" to complete Block


please advise on how to fix it
0
 
LVL 34

Expert Comment

by:ste5an
ID: 41823765
By using map.get("xyz") in the codebat lab.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823773
public Map<String, String> mapAB(Map<String, String> map) {
  if(map.containsKey("a")&&map.containsKey("b") ){
    map.put("ab",map.get("xyz")+map.get("b"));
  }
  return map;
}


//map.get("xyz") 

Open in new window


like abobve
Expected      Run            
mapAB({"b": "There", "a": "Hi"}) → {"b": "There", "a": "Hi", "ab": "HiThere"}      {"b": "There", "a": "Hi", "ab": "nullThere"}      X      
mapAB({"a": "Hi"}) → {"a": "Hi"}      {"a": "Hi"}      OK      
mapAB({"b": "There"}) → {"b": "There"}      {"b": "There"}      OK      
mapAB({"c": "meh"}) → {"c": "meh"}      {"c": "meh"}      OK      
mapAB({"b": "bbb", "c": "ccc", "a": "aaa", "ab": "nope"}) → {"b": "bbb", "c": "ccc", "a": "aaa", "ab": "aaabbb"}      {"b": "bbb", "c": "ccc", "a": "aaa", "ab": "nullbbb"}      X      
mapAB({"b": "bbb", "c": "ccc", "ab": "nope"}) → {"b": "bbb", "c": "ccc", "ab": "nope"}      {"b": "bbb", "c": "ccc", "ab": "nope"}      OK      
Correct for more than half the tests

Your progress graph for this problem

0
 
LVL 34

Assisted Solution

by:ste5an
ste5an earned 250 total points
ID: 41823790
Yup, no NPE.. "nullThere".
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823803
i wonder what is wrong with my eclipse class?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823876
what are arguments for method mapAB??
is it below  single map with key, value both as string??

Map<String, String> map

??

is  it two string or other map ?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41826332
You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));

Open in new window

but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));

Open in new window


On codingbat this
{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41826553
package test;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
		Map<String, String> map = new HashMap();
		map.put("b", "There");
		map.put("a", "Hi");
		System.out.println("--->"+mapAB(map));
	}	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		      if (map.get("a")==null && map.get("b")==null) {
		          map.put("ab", null);
		      } else {
		          map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		      }
		  }
		  return map;
		}
}
/*You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));
On codingbat this{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi")
*/

Open in new window


above give correct output
--->{a=Hi, ab=HiThere, b=There}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41826564
package test;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
		Map<String, String> map = new HashMap();
		map.put("b", null);
		map.put("a", "Hi");
		System.out.println("--->"+mapAB(map));
	}	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		      if (map.get("a")==null && map.get("b")==null) {
		          map.put("ab", null);
		      } else {
		          map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		      }
		  }
		  return map;
		}
}
/*You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));
On codingbat this{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi")
*/

Open in new window


above gave below output

--->{a=Hi, ab=Hi, b=null}


bsically i am passing b value as null and got output ab=Hi

Is that is how i have to tests null here?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41832369
As concurrent access to the map is possible,

what is meaning of concurrent access to map?



When the value cannot be null, then the following is better, cause it performs slightly better for the normal Map classes:

public synchronized Map<String, String> mapAB(Map<String, String> map) {
  String a = map.get("a");
  String b = map.get("b");
  if(a != null && b != null)
  {
    map.put("ab", a + b);
  }
 
  return map;
}

do you mean instead of above as below( i.e instead of cannot it is can???)

When the value can be null, then the following is better, cause it performs slightly better for the normal Map classes:

public synchronized Map<String, String> mapAB(Map<String, String> map) {
  String a = map.get("a");
  String b = map.get("b");
  if(a != null && b != null)
  {
    map.put("ab", a + b);
  }
 
  return map;
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41832380
package test;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
		Map<String, String> map = new HashMap();
		map.put("b", [b]null);[/b]
		map.put("a", "Hi");
		System.out.println("--->"+mapAB(map));
	}	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		      if (map.get("a")==null && map.get("b")==null) {
		          map.put("ab", null);
		      } else {
		          map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		      }
		  }
		  return map;
		}
}
/*You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));
On codingbat this{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi")
*/

Open in new window


what is difference between passing Null as above and passing "" as value as below
package test;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
		Map<String, String> map = new HashMap();
		map.put("b"[b], ""[/b]);
		map.put("a", "Hi");
		System.out.println("--->"+mapAB(map));
	}	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		      if (map.get("a")==null && map.get("b")==null) {
		          map.put("ab", null);
		      } else {
		          map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		      }
		  }
		  return map;
		}
}
/*You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));
On codingbat this{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi")
*/

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 41833081
Is that is how i have to tests null here?
Yes.

what is difference between passing Null as above and passing ""
"" is just the empty string and won't lead to NPExceptions like null will.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41837376
what is difference between passing Null as above and passing ""
"" is just the empty string and won't lead to NPExceptions like null will.
below code did not gave NPE even i used Null as value right?
when it gives null vlaue gives NPE where as "" wont give??
package test;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
		Map<String, String> map = new HashMap();
		map.put("b", null);
		map.put("a", "Hi");
		System.out.println("--->"+mapAB(map));
	}	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		      if (map.get("a")==null && map.get("b")==null) {
		          map.put("ab", null);
		      } else {
		          map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		      }
		  }
		  return map;
		}
}
/*You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));
On codingbat this{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi")
*/

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 41837972
>> below code did not gave NPE even i used Null as value right?
Right. That's because you avoid NPE by checking for it:
if (map.get("a")==null && map.get("b")==null) {

Open in new window

If you wouldn't do that check, you would have an NPE.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41847327
i see line 15

 if (map.get("a")==null && map.get("b")==null) {
0
 
LVL 7

Author Comment

by:gudii9
ID: 41847329
If you wouldn't do that check, you would have an NPE.

you mean like below throws NPE?

package test;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
		Map<String, String> map = new HashMap();
		map.put("b", null);
		map.put("a", "Hi");
		System.out.println("--->"+mapAB(map));
	}	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		     // if (map.get("a")==null && map.get("b")==null) {
		         // map.put("ab", null);
		      //} else {
		          map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		     // }
		  }
		  return map;
		}
}
/*You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));
On codingbat this{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi")
*/

Open in new window


i still did not get NPE rather i got below meaningful output to my eyes??

--->{a=Hi, ab=Hi, b=null}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41847348
public Map<String, String> mapAB(Map<String, String> map) {
  if(map.containsKey("a")&&map.containsKey("b") ){
    map.put("ab",map.get("xyz")+map.get("b"));
  }
  return map;
}


//map.get("xyz") 

Open in new window


i modified as below which fixed all tests
public Map<String, String> mapAB(Map<String, String> map) {
  if(map.containsKey("a")&&map.containsKey("b") ){
   String x= map.get("a")+map.get("b");
   map.put("ab",x);
  }
  return map;
}

Open in new window


looks like i have to keep key a and key b and additionally key ab
0
 
LVL 7

Author Comment

by:gudii9
ID: 41847351
no sorting comes here based on keys or values??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41847774
>> i still did not get NPE
That's because of
map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));

Open in new window

which also checks for null
0
 
LVL 7

Author Comment

by:gudii9
ID: 41848742
package test;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
		Map<String, String> map = new HashMap();
		map.put("b", null);
		map.put("a", "Hi");
		System.out.println("--->"+mapAB(map));
	}	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		     // if (map.get("a")==null && map.get("b")==null) {
		         // map.put("ab", null);
		      //} else {
		          //map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		          map.put("ab", map.get("a")+ map.get("b"));
		          
		     // }
		  }
		  return map;
		}
}
/*You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));
On codingbat this{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi")
*/

Open in new window

above alsoe did not throw NPE
i got below output

--->{a=Hi, ab=Hinull, b=null}


how to tweak my code to see NPE??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41849527
Replace
map.put("b", null);
map.put("a", "Hi");

Open in new window

resp.
map.put("ab", map.get("a")+ map.get("b"));

Open in new window

by
map.put("b", "Hi");
map.put("a", null);

Open in new window

resp.
map.put("ab", map.get("a").concat(map.get("b")));

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41850397
what is meaning of resp.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41850406
package test;
import java.util.HashMap;
import java.util.Map;
public class TestMap {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
		Map<String, String> map = new HashMap();
		map.put("b", null);
		map.put("a", "Hi");
		System.out.println("--->"+mapAB(map));
	}	
	public static Map<String, String> mapAB(Map<String, String> map) {
		  if (map.containsKey("a") && map.containsKey("b")) {
		     // if (map.get("a")==null && map.get("b")==null) {
		         // map.put("ab", null);
		      //} else {
		          //map.put("ab", (map.get("a")==null ? "" : map.get("a")) + (map.get("b")==null ? "" : map.get("b")));
		         // map.put("ab", map.get("a")+ map.get("b"));
			  map.put("ab", map.get("a").concat(map.get("b")));
		          
		     // }
		  }
		  return map;
		}
}
/*You shouldn't write:
System.out.println("--->"+mapAB({"b": "There", "a": "Hi"}));
but
Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi");
System.out.println("--->"+mapAB(map));
On codingbat this{"b": "There", "a": "Hi"}
is just a notation for a map containing the keys "b" and "a" having the resp. values "There" and "Hi".
In java code you create such a map this way:

Map<String, String> map = new HashMap();
map.put("b", "There");
map.put("a", "Hi")
*/

Open in new window

above throwed NPE


Exception in thread "main" java.lang.NullPointerException
      at java.lang.String.concat(String.java:2027)
      at test.TestMap.mapAB(TestMap.java:20)
      at test.TestMap.main(TestMap.java:11)


why below line did not throw NPE
map.put("ab", map.get("a")+ map.get("b"));

where as below line throwed NPE??

        map.put("ab", map.get("a").concat(map.get("b")));

please advise
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 41853314
In this code
 map.put("ab", map.get("a").concat(map.get("b")));

Open in new window

you call a function concat() upon a null object. That results in an NPE.

This is code
map.put("ab", map.get("a")+ map.get("b"));

Open in new window

apparently is more or less the same as writing
map.put("ab", "null" + map.get("b"));

Open in new window

The '+' operator concatenates the toString() representation of the two arguments.
And toString() representation of a null object is "null".
So that doesn't lead to a NPE
0
 
LVL 7

Author Comment

by:gudii9
ID: 41854353
The '+' operator concatenates the toString() representation of the two arguments.
And toString() representation of a null object is "null".
So that doesn't lead to a NPE

i got it. thank you
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question