• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

data types

 
yamuna dulanajani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference of
final int x=100;
and
final int x;
x=100;
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think? Why would you think that there is a difference? Or not? Does it compile? Does it run?
 
yamuna dulanajani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:What do you think? Why would you think that there is a difference? Or not? Does it compile? Does it run?




when it compile what happen?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is that the first case leads to a compile-time constant and the second case does not.
 
yamuna dulanajani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this can compile ,but

this gives a compile error ,
WHY?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yamuna dulanajani wrote:

this gives a compile error ,


No, it doesn't.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
I have added code tags to your code, which you should always use. You can see how much better it looks.
 
Thimal Deemantha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yamuna dulanajani wrote:
this can compile ,but

this gives a compile error ,
WHY?



The difference is that in the first case x is a compile time constant. i.e. the compiler knows that x will always have value 101, and by extension it knows that the if-condition will always be true and y will always be initialized to value -1. That's why it's legal to use y in the System.out.println() call: the compiler knows it was initialized to a value. In the other code sample x isn't a compile time constant and all the implications that held up in the first code sample don't. This leads the compiler to complain about the use of y, because it can't figure out whether or not it was initialized. At runtime, this is clearly the case, but the compiler just isn't smart enough to pick up on that.

got it from Jelle Klap
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Thimal Deemantha: It's great that you want to help, and I'm glad to see you gave credit to the author for what you're quoting. However, if you understood what Jelle was telling you, then we can see that the code the OP provided doest not in fact produce a compiler error.

In fact, this is the same code as you provided in your thread, and just like the code you provided, it's missing something would produce a compile error. As it is, it compiles just fine.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic