Skip to content

Commit 0c7c8f8

Browse files
committed
adding java8
1 parent d233689 commit 0c7c8f8

8 files changed

+304
-72
lines changed

JavaWebStartTest/pom.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>JavaWebStartTest</groupId>
8+
<artifactId>jnlptest</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>

java8_features/README.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Major Changes Categories in JDK8
2+
================================
3+
1. Java Language
4+
2. Java Compiler
5+
3. Java Libraries
6+
4. Java Tools
7+
5. Java Runtime(JVM)
8+
9+
1. New Java Language Changes
10+
----------------------------
11+
1.1 Lambdas and Functional Interfaces
12+
1.2 Interface's Default and Static Methods
13+
1.3 Method References
14+
1.4 Repeating annotations
15+
1.5 Better Type Inference
16+
1.6 Extended Annotations Support
17+
18+
19+
2. New Java Compiler Changes
20+
----------------------------
21+
2.1 Parameter names
22+
23+
24+
3. New Java Libraries Changes
25+
-----------------------------
26+
3.1 Optional
27+
3.2 Streams
28+
3.3 Date/Time API
29+
3.4 Nashorn JavaScript engine
30+
3.5 Base64
31+
3.6 Parallel Arrays
32+
3.7 Concurrency
33+
34+
4. New Java Tools
35+
-------------------
36+
4.1 jdeps
37+
4.2 jjs
38+
39+
40+
5. New JVM Changes
41+
--------------------
42+
5.1 The JVM options -XX:PermSize and –XX:MaxPermSize have been replaced by -XX:MetaSpaceSize and -XX:MaxMetaspaceSize respectively.
43+
44+
45+
NOTE:
46+
=====
47+
48+
Heap Space:
49+
The heap stores all of the objects created by your Java program.
50+
The heap's contents is monitored by the garbage collector, which frees memory from the
51+
heap when you stop using an object (i.e. when there are no more references to the object.
52+
53+
Stack:
54+
Stack stores primitive types like ints and chars, and are typically local variables
55+
and function return values. These are not garbage collected.
56+
57+
PermGen(MetaSpace):
58+
The permanent generation is special because it holds meta-data describing user classes
59+
classes that are not part of the Java language). Examples of such meta-data are objects describing
60+
classes and methods and they are stored in the Permanent Generation. Applications with large code-base
61+
can quickly fill up this segment of the heap which will cause java.lang.OutOfMemoryError:
62+
PermGen no matter how high your -Xmx and how much memory you have on the machine.

java8_features/pom.xml

+12
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,17 @@
88
<artifactId>java8features</artifactId>
99
<version>1.0-SNAPSHOT</version>
1010

11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<configuration>
17+
<source>1.8</source>
18+
<target>1.8</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
1123

1224
</project>

java8_features/src/main/java/Java8FeatureTest.java

-72
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package language;
2+
3+
/**
4+
* - The function interface is an interface with just one single method.
5+
* - As such, it may be implicitly converted to a lambda expression.
6+
* - The java.lang.Runnable and java.util.concurrent.Callable are two great examples of functional interface
7+
* and there are a lot more have been added to jdk 8.
8+
*
9+
* Created by hdhamee on 6/13/16.
10+
*/
11+
public class Java8FunctionalInterfaces {
12+
13+
public static void main(String[] args) {
14+
//Implementing the functional interface by creating an anonymous inner class versus using lambda expression.
15+
carryOutWork(new ComplexFunctionalInterface() {
16+
@Override
17+
public void doWork() {
18+
System.out.println("Do work in SimpleFun impl...");
19+
}
20+
});
21+
carryOutWork(() -> System.out.println("Do work in lambda exp impl..."));
22+
//OR
23+
SimpleFuncInterface simpleFuncInterface = () -> System.out.println("Another way to call lambda: Do work in lambda exp impl...");
24+
simpleFuncInterface.doWork();
25+
}
26+
27+
28+
29+
30+
public static void carryOutWork(SimpleFuncInterface sfi){
31+
sfi.doWork();
32+
}
33+
34+
// Java 8 adds special annotation @FunctionalInterface to avoid from adding more methods to this interface.
35+
// The interface can also declare the abstract methods from the java.lang.Object class,
36+
// but still the interface can be called as a Functional Interface.
37+
@FunctionalInterface
38+
public interface SimpleFuncInterface {
39+
public void doWork();
40+
public String toString();
41+
public boolean equals(Object o);
42+
}
43+
44+
// Default and static methods do not break the functional interface contract and may be declared.
45+
@FunctionalInterface
46+
public interface ComplexFunctionalInterface extends SimpleFuncInterface {
47+
48+
default public void doSomeWork(){
49+
System.out.println("Doing some work in interface impl...");
50+
}
51+
52+
default public void doSomeOtherWork(){
53+
System.out.println("Doing some other work in interface impl...");
54+
}
55+
56+
static void doTest(){
57+
System.out.println("Static method...");
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package language;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by hdhamee on 6/13/16.
7+
*/
8+
public class Java8Lambdas {
9+
10+
/**
11+
* - A lambda(also known as closures) expression is like a method.
12+
* - it provides a list of formal parameters and a body (which can be an expression or a block of code)
13+
* expressed in terms of those parameters.
14+
* - They allow us to treat functionality as a method argument or code as a data.
15+
* - Lambdas can be passed as a function/method parameter.
16+
* - Java's anonymous classes are also a kind of closure as they can be passed as method parameter.
17+
*
18+
* - The basic syntax of a lambda is either:
19+
* (parameters) -> expression
20+
* OR
21+
* (parameters) -> { statements; }
22+
*/
23+
public static void main(String[] args) {
24+
25+
String[] array = new String[]{"a", "b", "d"};
26+
Arrays.asList(array).forEach( e -> System.out.println( e ) );
27+
28+
29+
// To explicitly specify the type of parameter, by default compiler infers the type
30+
Arrays.asList( array ).forEach( ( String e ) -> { System.out.println( e ); } );
31+
32+
33+
//Lambdas may reference the class members and local variables (implicitly making them effectively final if they are not).
34+
String separator = ",";
35+
Arrays.asList(array).forEach((String e ) -> System.out.print( e + separator ) );
36+
37+
38+
// Lambdas may return a value. The type of the return value will be inferred by compiler.
39+
// The return statement is not required if the lambda body is just a one-liner.
40+
Arrays.asList( array ).sort( ( e1, e2 ) -> {
41+
int result = e1.compareTo( e2 );
42+
return result;
43+
} );
44+
45+
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package language;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.function.Consumer;
7+
import java.util.function.Function;
8+
import java.util.function.Supplier;
9+
10+
/**
11+
* Created by hdhamee on 6/13/16.
12+
*/
13+
public class Java8MethodReferences {
14+
15+
public static void main(String[] args) {
16+
17+
// Suppliers is a function interface and represents a function that accepts no arguments and produce
18+
// a result of some arbitrary type.
19+
20+
//Supplier referencing a constructor method:
21+
Supplier<User> userSupplierConstructor = User::new;
22+
User user1 = userSupplierConstructor.get();
23+
24+
//Supplier referencing a static method:
25+
Supplier<User> userSupplierStatic = UserFactory::produceUserStatic;
26+
User user2 = userSupplierStatic.get();
27+
28+
//Supplier Referencing a instance method:
29+
UserFactory userFactory = new UserFactory();
30+
Supplier<User> userSupplierInstance = userFactory::produceUser;
31+
User user3 = userSupplierInstance.get();
32+
33+
//Consumers represent a function that accepts a single argument of an arbitrary type and produce no result
34+
35+
// consumer using lambda expression
36+
Consumer<User> userConsumerLambda = (u) -> System.out.println("Username: " + u.getUsername());
37+
userConsumerLambda.accept(user3);
38+
39+
// consumer using method referencing
40+
Consumer<User> userConsumerMth = UserFactory::printName;
41+
userConsumerMth.accept(user3);
42+
43+
44+
45+
46+
//referencing a static method
47+
List<Double> numbers = Arrays.asList(4.0, 9.0, 16.0, 25.0, 36.0);
48+
List<Double> squaredNumbers = Java8MethodReferences.findSquareRoot(numbers,Double::new);
49+
System.out.println("Square root of numbers = "+squaredNumbers);
50+
}
51+
52+
private static List findSquareRoot(List list, Function<Double, Double> f){
53+
List<Double> result = new ArrayList<>();
54+
list.forEach( x -> result.add(f.apply(Math.sqrt((Double) x))));
55+
return result;
56+
}
57+
58+
59+
static class UserFactory {
60+
public User produceUser() {
61+
return new User();
62+
}
63+
64+
public static User produceUserStatic() {
65+
return new User();
66+
}
67+
68+
public static void printName(User user){
69+
System.out.println(user.getUsername());
70+
}
71+
}
72+
73+
static class User {
74+
public String getUsername(){
75+
return "hari ram";
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package language;
2+
3+
import java.lang.annotation.Repeatable;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
7+
/**
8+
* Created by hdhamee on 6/13/16.
9+
*/
10+
public class Java8RepeatingAnnotations {
11+
12+
@Retention(RetentionPolicy.RUNTIME)
13+
public @interface Filters {
14+
Filter[] value();
15+
}
16+
17+
@Retention(RetentionPolicy.RUNTIME)
18+
@Repeatable(Filters.class )
19+
public @interface Filter {
20+
String value();
21+
};
22+
23+
@Filter( "filter1" )
24+
@Filter( "filter2" )
25+
public interface Filterable {
26+
}
27+
28+
public static void main(String[] args) {
29+
for( Filter filter: Filterable.class.getAnnotationsByType(Filter.class)) {
30+
System.out.println( filter.value() );
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)