This question already has answers here:
What does a variable being "effectively final" mean? [duplicate]
(2 answers)
Lambdas: local variables need final, instance variables don't
(10 answers)
Closed 11 days ago.
I understand the concept of final / effectively final within lambda , it essentially protects from side effects. However why the following code snippet works? Is it also not violating this behaviour?
import java.util.stream.Stream;
public class Learn {
int x = 0;
Stream<Integer> numbers() {
x = x +1;
return Stream.iterate(0, i -> {
int result = x + i;
x = i;
return result;
});
}
public static void main(String[] args) {
Learn l = new Learn();
l.numbers()
.skip(20) // Don't use the first 20
.limit(10) // Then take 10 of them
.forEach(System.out::println);
System.out.println("=====================");
System.out.println(l.x);
}
}
75025
121393
196418
317811
514229
=====================
317811
Related
This question already has answers here:
Best way to Format a Double value to 2 Decimal places [duplicate]
(2 answers)
Closed 3 years ago.
I tried doing this
import java.util.Arrays;
public class Array1
{
public static void main (String args [])
{
double [] a1 = {1.3,2.4,5.6,7.8,9.2};
double [] a2 = {1.0,3.4,4.2,5.3,6.7};
double [] a3 = new double[a1.length];
for(int i = 0; i<a3.length;i++)
{
a3[i] = a1[i] - a2[i];
System.out.print("\n"+a3[i]);
}
}
}
However, when it prints, it doesn't print doubles like 1.0. It prints doubles like 0.30000000004.
How may I fix this?
You can do it using String.format("%.1f", a3[i]), here by placing count after . you can decide the how many digits you want to show.
double [] a1 = {1.3,2.4,5.6,7.8,9.2};
double [] a2 = {1.0,3.4,4.2,5.3,6.7};
double [] a3 = new double[a1.length];
for(int i = 0; i<a3.length;i++)
{
a3[i] = a1[i] - a2[i];
String formatted = String.format("%.1f", a3[i]);
System.out.println(formatted);
}
This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 4 years ago.
I have a question about Java being pass-by-value. I know that variables declared outside a method will not change their values since when I call I method on the variables, the method will only be using the value assigned to them. But in this case, I do not understand why int result does not get a value of 2. As increment() will get the value of x, so 1 and increment it by 1 and store the value in the result variable.
public class Increment {
public static void main(String[] args) {
int x = 1;
System.out.println("Before the call, x is " + x);
int result = increment(x);
System.out.println("After the call, x is " + result);
}
public static int increment(int n) {
return n++;
}
}
Post increment operator n++ increments the value of n by 1, but returns the previous value. Therefore increment(x) returns x, not x+1.
This question already has answers here:
Why don't Java's +=, -=, *=, /= compound assignment operators require casting?
(11 answers)
Closed 4 years ago.
I'm new to Java programming and I was playing round with a code:
public class Main {
public static void main(String[] args){
byte x = 10;
x = x*10;
System.out.println(x);
}
}
This gives a compilation error and I know why. But what I don`t understand is why the code below does not give an error:
public class Main {
public static void main(String[] args){
byte x = 10;
x *= 10;
System.out.println(x);
}
}
From what I know
x *= x;
and
x = x*x;
are same.
What am I missing then?
The compound assignment operators will perform automatic type-casting. You could find more details on https://www.geeksforgeeks.org/compound-assignment-operators-java/
This question already has answers here:
What is the difference between & and && in Java?
(15 answers)
Closed 6 years ago.
When I run a simple program, both of them ( & and && ) seem to work interchangeably. Could someone please explain when I should use each case?
public class Test1 {
public static void main(String [] args){
int counter = 0;
int dice1;
int dice2;
for (int loop = 0; loop <= 1000; loop++) {
dice1= (int) (Math.random()*6)+1;
dice2= (int) (Math.random()*6)+1;
if (dice1 == 6 && dice2 == 6){
counter ++;
}
}
System.out.println("Counter = " + counter);
}
}
This program also seems to work when I use & instead of &&. So what's the difference?
The main difference is that the && and || operators do some kind of "lazy evaluation"
In this line of code:
method1() & method2();
both methods will be called independant of the return value of method1. On the other hand in this line of code:
method1() && method2();
the method2 will only be called if method1 returns true.
When using || the second method will only be called when the first method returns false.
This question already has answers here:
Java increment and assignment operator [duplicate]
(6 answers)
Closed 7 years ago.
public static void main(String[] args) {
int x = 10;
x = x++;
x = x++;
x = x++;
System.out.println(x);
}
Why is the output 10 when the expected output is 13?
Post increment operator x++ returns the original value of x. Therefore x=x++ assigns the old value of x back to x.
This is probably what you weant to do
public static void main(String[] args) {
int x = 10;
x++;
x++;
x++;
System.out.println(x);
}