Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance Variables and Arrays: Un-Initialized Values  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My internal certification had a confusing puzzle for practice:
Why the difference in the output of the following code?


OUTPUT:
a=null
b=null
c=null


and that of this?


OUTPUT:
b=0
c=0
 
Marshal
Posts: 59392
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should always tell us where such code is from.
We don’t simply hand out answers; what do you think the difference is, and why?
 
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java automatically initialises variables and provides default values. Take a look at the link and see what the differences are in your classes
 
Hilary Mann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You should always tell us where such code is from.
We don’t simply hand out answers; what do you think the difference is, and why?



Edited, please see again and answer.
 
Hilary Mann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rose Ellis wrote:Java automatically initialises variables and provides default values. Take a look at the link and see what the differences are in your classes



Please re-read the edited post with output and answer accordingly.
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:... and why?

 
Hilary Mann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Darryl Burke wrote:

Campbell Ritchie wrote:... and why?



If I am not mistaken, we can discuss 'why' here...
 
Ranch Hand
Posts: 187
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int [] a,b,c; :--> It is an array declaration for all variables a,b and c.
and
int b,c; :--> is an instance variable declaration of b and c.

By default in java, array initialized with null and instance variable with 0(if int).

You are confusing with syntax of int []...

In java you can declare array in both the following ways:
1) int [] a,b,c;
2) int a[],b[],c[];
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gaurangkumar Khalasi wrote:By default in java, array initialized with null and instance variable with 0(if int).


The reason being that an array is an object, not a primitive.

Winston
 
Campbell Ritchie
Marshal
Posts: 59392
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gaurangkumar Khalasi wrote: . . .
In java you can declare array in both the following ways:
1) int [] a,b,c;
2) int a[],b[],c[];

. . . but you ought always to use the first form. It is better style because it makes it clear that the variables are of type int[]. I think it was a mistake to permit the second form, which is the normal form in C.
 
Rose Ellis
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Gaurangkumar Khalasi wrote: . . .
In java you can declare array in both the following ways:
1) int [] a,b,c;
2) int a[],b[],c[];

. . . but you ought always to use the first form. It is better style because it makes it clear that the variables are of type int[]. I think it was a mistake to permit the second form, which is the normal form in C.



how about

int[] a;
int[] b;
int[] c;
 
Campbell Ritchie
Marshal
Posts: 59392
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That’t better still.
 
author
Sheriff
Posts: 23553
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hilary Mann wrote:

Campbell Ritchie wrote:You should always tell us where such code is from.
We don’t simply hand out answers; what do you think the difference is, and why?



Edited, please see again and answer.



Not sure what you mean here. It now says "internal practice". Are you saying that you wrote this example?

We would like the source please. This could be the book, the mock test, the web-site, etc. where this example came from.

Henry
 
Hilary Mann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Hilary Mann wrote:

Campbell Ritchie wrote:You should always tell us where such code is from.
We don’t simply hand out answers; what do you think the difference is, and why?



Edited, please see again and answer.



Not sure what you mean here. It now says "internal practice". Are you saying that you wrote this example?

We would like the source please. This could be the book, the mock test, the web-site, etc. where this example came from.

Henry



My trainer made demos/quiz up for teaching and making us understand the concepts...
...These are not from any book or source.
 
Campbell Ritchie
Marshal
Posts: 59392
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the clarification
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!