0

Im learning java from basics. One of the book they claimed int h = f = 10; will work but in real time its not working. Please explain.

My code:

int h = f = 10;

Why it is not working?

Koopakiller
  • 2,838
  • 3
  • 32
  • 47
Prakash Bala
  • 315
  • 5
  • 12

3 Answers3

1

Try this :

int h, f;
h = f = 10;

but keep in mind, this will only work with immutable objects, like Integer or String. You cannot use the same for an object of a class because they would be pointing on the same instance and if you modify one, you'll modify the others !

faflo10
  • 386
  • 5
  • 13
0

We have to declare h and f first.

int a, b, c, d;

a= b = c = d = 10;
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
0

Because f is not declared yet. This will work:

int f, h;
h = f = 10;
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156