Skip to content

Commit 1e81166

Browse files
author
Ram swaroop
committed
added content
1 parent 17ec324 commit 1e81166

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

_posts/2015-05-14-variables-and-literals.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ the uninitialized variable, we can get different behavior depending on __what ty
420420
with (primitives or objects)__. The behavior also depends on __the level (scope) at which we are declaring our
421421
variable__.
422422

423-
Default values for Instance variables (Primitive and Non-primitive):
423+
#### Default values for Instance variables (Primitive and Non-primitive):
424424

425425
Variable Type | Default Value
426426
--------------|----------------
@@ -430,6 +430,41 @@ Variable Type | Default Value
430430
`char` | `'\u0000'`
431431
Object reference | `null` (not referencing any object)
432432

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+
433468
{% include responsive_ad.html %}
434469

435470
### Q&A

0 commit comments

Comments
 (0)