Data Types in Java | Primitive and Non-Primitive Data Types | Edureka
static void sameName(int x){
System.out.println(x);
}
/// Function Overloading - happens at compile time
//
// To have a function with same name
// either arguments count should be different or type of argument should be diff (if args count is same)
static String sameName(String x){
return "8" + x;
}
public static boolean isVowel(char c) {
return "aeiouAEIOU".indexOf(c) != -1;
}
List<Integer> list = new ArrayList<>();
List<String> x = new ArrayList<>(Arrays.asList("initial", "values"));
list.add(2);
list.add(3);
list.set(1, 5); // replaces element at index 1 with 5
list.remove(0); // removes element at 0-index, throws IndexOutOfBounds error if wrong index is provided
int length = list.size();
// For loop for iterating over the List
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// enhanced for loop(for-each) for iteration
for (String i : myList) {
System.out.println(i);
}
// Iterator
Iterator<String> it = myList.iterator();
// Condition check for elements in List
// using hasNext() method returning true till
// there is single element in a List
while (it.hasNext()) {
System.out.println(it.next());
}
// Lambda expression printing all elements in a List
myList.forEach((temp) -> {
System.out.println(temp);
});
// Return Empty List
return List.of();
List<Integer> originalList = new ArrayList<>();
originalList.add(3);
originalList.add(5);
List<Integer> res = originalList;
res.add(7);
System.out.println(originalList); // [3, 5, 7] 🤯 originalList variable also got modified. since the two ref variables are pointing to same object/reference
System.out.println(res); // [3, 5, 7]
List<Integer> properCopy = new ArrayList<Integer>(originalList); // A new arrayList
properCopy.add(99);
System.out.println(originalList); // [3, 5, 7] 😌 out originalList is safe now
// other ways to create a copy of List
Collections.copy(properCopy, originalList);
public int[] sortByBits(int[] arr) {
Integer[] intArr = Arrays.stream(arr).boxed().toArray(Integer[]::new);
Arrays.sort(intArr, new BitCountComparator());
return Arrays.stream(intArr).mapToInt(Integer::intValue).toArray();
}
int[] arr = new int[5];
int[] arr2 = new int[]{1, 2, 4, 5, 56}; // no need to specify the size
int[] arr3 = {1, 2, 4, 5};
arr3[0] = 90;
// LOOPING
for (int i=0; i<arr.length; i++) {} //modify
for ( int a : arr) {...} //access only
int[] res = arr2.clone(); // cloning array