Last active
March 18, 2016 01:18
-
-
Save bestalign/f0b9adf2595105effeb3 to your computer and use it in GitHub Desktop.
Top 10 Mistakes Java Developers Make
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Arrays.asList(arr).contains(targetValue); | |
// OR | |
for(String s: arr){ | |
if(s.equals(targetValue)) | |
return true; | |
} | |
return false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Set<String> set = new HashSet<String>(Arrays.asList(arr)); | |
return set.contains(targetValue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Super() { | |
System.out.println("Super"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Super { | |
String s; | |
public Super(String s) { | |
this.s = s; | |
} | |
} | |
public class Sub extends Super { | |
int x = 200; | |
public Sub(String s) { // error here | |
} | |
public Sub() { // error here | |
System.out.println("Sub"); | |
} | |
public static void main(String[] args) { | |
Sub s = new Sub(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(arr)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> list = Arrays.asList(arr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String result=""; | |
for(String s: arr){ | |
result = result + s; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. use double quotes | |
String x = "abc"; | |
//2. use constructor | |
String y = new String("abc"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String a = "abcd"; | |
String b = "abcd"; | |
System.out.println(a == b); // True | |
System.out.println(a.equals(b)); // True | |
String c = new String("abcd"); | |
String d = new String("abcd"); | |
System.out.println(c == d); // False | |
System.out.println(c.equals(d)); // True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d")); | |
Iterator<String> iter = list.iterator(); | |
while (iter.hasNext()) { | |
String s = iter.next(); | |
if (s.equals("a")) { | |
iter.remove(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d")); | |
for (int i = 0; i < list.size(); i++) { | |
list.remove(i); | |
} | |
System.out.println(list); | |
// output | |
// [b, d] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d")); | |
for (String s : list) { | |
if (s.equals("a")) | |
list.remove(s); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void add(List list, Object o){ | |
list.add(o); | |
} | |
public static void main(String[] args){ | |
List<String> list = new ArrayList<String>(); | |
add(list, 10); | |
String s = list.get(0); | |
} | |
// Exception will be thrown.. | |
// Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String | |
// at ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment