Lamda
ArrayList<Employee> e = new ArrayList<Employee>();
e.add(new Employee("Balu", 1));
e.add(new Employee("zahir", 2));
e.add(new Employee("Kannan", 3));
e.add(new Employee("Mani", 4));
System.out.println(e);
//sorting order using eno
Comparator<Employee> c=(e1, e2)->(e1.eno<e2.eno?-1:e1.eno>e2.eno?1:0);
Collections.sort(e, c);
e.stream().forEach(System.out::println);
//alphabetical order using name
Collections.sort(e, (e1,e2)->(e1.name.compareTo(e2.name)));
e.stream().forEach(System.out::print);
Static
if a field is declared static, then exactly a single copy of that field is created and shared among all instances of that class.
From the memory perspective, static variables go in a particular pool in JVM memory called Metaspace (before Java 8, this pool was called Permanent Generation or PermGen, which was completely removed and replaced with Metaspace).
ArrayList<Employee> e = new ArrayList<Employee>();
e.add(new Employee("Balu", 1));
e.add(new Employee("zahir", 2));
e.add(new Employee("Kannan", 3));
e.add(new Employee("Mani", 4));
System.out.println(e);
//sorting order using eno
Comparator<Employee> c=(e1, e2)->(e1.eno<e2.eno?-1:e1.eno>e2.eno?1:0);
Collections.sort(e, c);
e.stream().forEach(System.out::println);
//alphabetical order using name
Collections.sort(e, (e1,e2)->(e1.name.compareTo(e2.name)));
e.stream().forEach(System.out::print);
Static
if a field is declared static, then exactly a single copy of that field is created and shared among all instances of that class.
From the memory perspective, static variables go in a particular pool in JVM memory called Metaspace (before Java 8, this pool was called Permanent Generation or PermGen, which was completely removed and replaced with Metaspace).
- Since static variables belong to a class, they can be accessed directly using class name and don’t need any object reference
- static variables can only be declared at the class level
- static fields can be accessed without object initialization
- Although we can access static fields using an object reference (like ford.numberOfCars++) , we should refrain from using it as in this case it becomes difficult to figure whether it’s an instance variable or a class variable; instead, we should always refer to static variables using class name (for example, in this case, Car.numberOfCars++)
No comments:
Post a Comment