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/
Related
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
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 2 years ago.
I created a code for Fibonacci numbers, but it doesn't compile and I don't know why, can someone help.
(And I also think that the last like with k-1 + k-2 doesn't make any sense.
public class Fibonacci {
public static void main(String[] args) {
int n = Integer.parseInt(args[n]);
System.out.println(FibNum(n));
}
public static int FibNum(int k) {
if (k == 0) return 0;
if (k == 1) return 1; else return k - 1 + k - 2;
}
}
int n = Integer.parseInt(args[n]); how could it work? variable n in not initialized
This question already has answers here:
Float and double datatype in Java
(9 answers)
Closed 6 years ago.
Can someone show me an example of example how I could use Double in the following code?
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
int myFirstNumber = (5+10) + (6*8);//Declaring an integer myFirstNumber
int mySecondNumber = 10;
int myThirdNumber = 3;
int myTotal = myFirstNumber + mySecondNumber + myThirdNumber;
System.out.println(myFirstNumber);//prints variable myFirstNumber
System.out.println("myFirstNumber ");//
System.out.println(myTotal);
}
}
Use either float or double (or their boxed counterparts, Float and Double) when you need to deal with non-integer values. The code you posted has no apparent need for that, so it's hard to answer your question. But one possibility would be if you wanted to compute, say, the average of the three numbers:
public class HelloWorld { public static void main(String[] args) {
System.out.println("Hello, World!");
int myFirstNumber = (5+10) + (6*8);//Declaring an integer myFirstNumber
int mySecondNumber = 10;
int myThirdNumber = 3;
int myTotal = myFirstNumber + mySecondNumber + myThirdNumber;
float average = myTotal / 3.0f;
System.out.println(myFirstNumber);//prints variable myFirstNumber
System.out.println("myFirstNumber ");//
System.out.println(myTotal);
System.out.println(average);
}
As to when to use float vs. double (the title of your question), use double when you can tolerate less rounding error than you get with float.
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);
}
This question already has answers here:
What is x after "x = x++"?
(18 answers)
Closed 8 years ago.
I was executing the following code
class First
{
public static void main(String arg[])
{
char x= 'A';
x = x++;
System.out.println(x);
}
}
Here the output is A.
My question is why didn't x get incremented before printing.
You're using the post-increment operator incorrectly - you don't need to use assignment as well. And in this case it undermines what you're trying to do.
For context, remember that the post-increment operator increases the value, and returns the old value. That is, x++ is roughly equivalent to:
int x_initial = x;
x = x + 1;
return x_initial;
Hopefully now you can see why your code is failing to change x. If you expand it, it looks like:
char x= 'A';
char y;
{
y = x;
x = x + 1;
}
x = y;
System.out.println(x);
and the net effect of the assignment, is to set x back to what it was originally.
To fix - you can simply call x++. Or, if you want to make it clear there's some sort of assignment happening, x += 1 or even just x = x + 1 would do the same thing.
class First
{
public static void main(String arg[])
{
char x= 'A';
x = x++; // it is post increment as ++ sign is after x
System.out.println(x);
}
}
Post Increment(x++) : First execute the statement then increase the value by one.
Pre Increment (++x) : First increase the value by one then execute the statement.