Java

Java Fibonacci examples

Few Java examples to find the Fibonacci numbers. 1. Java 8 stream 1.1 In Java 8, we can use Stream.iterate to generate Fibonacci numbers like this : Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]}) .limit(10) .forEach(x -> System.out.println("{" + x[0] + "," + x[1] + "}")); Output {0,1} {1,1} {1,2} {2,3} {3,5} {5,8} {8,13} {13,21} {21,34} {34,55} P.S Review the above output, the first value is what we wanted. 1.2 Final version. Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]}) .limit(10) .map(t -> t[0]) .forEach(x -> System.out.println(x)); Output 0 1 1 2 3 5 8 13 21 34 1.3 Sum all the Fibonacci...

Read more...

Java – Convert String to int

In Java, you can use Integer.parseInt() to convert a String to int. 1. Integer.parseInt() Examples Example to convert a String “10” to an primitive int. String number = "10"; int result = Integer.parseInt(number); System.out.println(result); Output 10 2. Integer.valueOf() Examples Alternatively, you can use Integer.valueOf(), it will returns an Integer object. String number = "10"; Integer result = Integer.valueOf(number); System.out.println(result); Output 10 NoteIn summary, parseInt(String) returns a primitive int, whereas valueOf(String) returns a new Integer() object. 3. NumberFormatException If the string does not contain a parsable integer, a NumberFormatException will be thrown. String number = "10A"; int result...

Read more...

Java – what is -Xms and -Xmx parameter?

In Java, -Xms set initial Java heap size, while -Xmx set the maximum Java heap size. 1. Some java -Xms -Xmx examples : # Start with 128MB of memory, and allow the Java process to use up to 1024MB of memory. java -Xms128m -Xmx1024m # Start with 256MB of memory, and allow the Java process to use up to 4G (4096MB) of memory. java -Xms256m -Xmx4g 2. The default initial and maximum Java heap size is allocated based on this ergonomics algorithm , also read this article – Find out your Java heap...

Read more...