my dog learned polymorphism*
The moose likes Java in General and the fly likes data types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "data types" Watch "data types" New topic
Author

data types

yamuna dulanajani
Greenhorn

Joined: Nov 01, 2012
Posts: 3
what is the difference of
final int x=100;
and
final int x;
x=100;
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

What do you think? Why would you think that there is a difference? Or not? Does it compile? Does it run?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
yamuna dulanajani
Greenhorn

Joined: Nov 01, 2012
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

The difference is that the first case leads to a compile-time constant and the second case does not.
yamuna dulanajani
Greenhorn

Joined: Nov 01, 2012
Posts: 3

this can compile ,but

this gives a compile error ,
WHY?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

yamuna dulanajani wrote:

this gives a compile error ,


No, it doesn't.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38491
    
  23
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

Joined: Nov 02, 2012
Posts: 9
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

@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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: data types