Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Just an experiment (cause' I'm new to Java).

 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please illustrate.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first error message is pretty clear and logical.

As for the second one, you are trying to put System.out.println(); statement outside of method body which results in an error.
 
Rasul Patrick
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following program:



Output is 0.

The article on this site states that In the absence of a user-defined constructor, Java defines a default constructor. This constructor doesn't accept any method arguments. It calls the constructor of the base class and assigns default values to all the instance variables. and assign them to null.
Is there any difference between Zero and null in Java because the output of the program is Zero ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That article is more‑or‑less correct. But not quite correct. You should read the Java Language Specification (JLS) for the official version.
The JLS uses the official term, superclass, rather than base, and it tells you it has an empty body or a super(); call only.
It does not initialise any fields at all. The fields already contain their default values, and the default constructor simply leaves them unchanged. You will have to search the JLS for the details; I didn’t find them. This Java Tutorials page might help.
You are correct that nulls are only applied to reference types. For primitives the fields are initialised to 0, which is interpreted as false for booleans and the “null character” for chars.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote: . . . This Java Tutorials page might help. . . .
And then again, it might not help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic