@@ -420,7 +420,7 @@ the uninitialized variable, we can get different behavior depending on __what ty
420
420
with (primitives or objects)__ . The behavior also depends on __ the level (scope) at which we are declaring our
421
421
variable__ .
422
422
423
- Default values for Instance variables (Primitive and Non-primitive):
423
+ #### Default values for Instance variables (Primitive and Non-primitive):
424
424
425
425
Variable Type | Default Value
426
426
--------------|----------------
@@ -430,6 +430,41 @@ Variable Type | Default Value
430
430
` char ` | ` '\u0000' `
431
431
Object reference | ` null ` (not referencing any object)
432
432
433
+ Therefore, for the below program:
434
+
435
+ {% highlight java linenos %}
436
+ public class Book {
437
+ private String title; // instance reference variable
438
+ private int noOfPages; // instance primitive variable
439
+ public String getTitle() {
440
+ return title;
441
+ }
442
+ public int getNoOfPages() {
443
+ return noOfPages;
444
+ }
445
+ public static void main(String [ ] args) {
446
+ Book b = new Book();
447
+ System.out.println("The title is " + b.getTitle());
448
+ System.out.println("No. of pages are " + b.getNoOfPages());
449
+ }
450
+ }
451
+ {% endhighlight %}
452
+
453
+ The output will be:
454
+ ` The title is null `
455
+ ` No. of pages are 0 `
456
+
457
+ __ NOTE:__ ` null ` is not the same as an empty ` String ("") ` . A ` null ` value means the reference variable is not referring
458
+ to any object on the heap.
459
+
460
+ #### Array Instance Variable
461
+
462
+
463
+
464
+ #### Default values for Local (also called Stack or Automatic) variables (Primitive and Non-primitive):
465
+
466
+
467
+
433
468
{% include responsive_ad.html %}
434
469
435
470
### Q&A
0 commit comments