Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

do java primitives have defualt values

843789Oct 19 2009 — edited Oct 20 2009
I am trying to determine if java primateives (like an int) have default values.

On sun's web page I read this:

"It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad
programming style." http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html

Yet, when I declare an int and then try to print it, I get a compiler error if I do not set a value to that int.

So, I know this is bad programming, but I am just wondering which way it is. Are there default values or not?

thanks,

-Adam
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 17 2009
Added on Oct 19 2009
8 comments
378 views