• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Help with primitive initialization

 
Ranch Hand
Posts: 170
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

it seems that I cannot understand the following example, could you please help?
There is a compiler error in line 4 that j has not been initialized.
But why is there this error since we initialize it in line 1??

 
Marshal
Posts: 72406
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always say where such material comes from.
That is the sort of code style that should earn somebody the sack, or a straight fail in an exam. It is the sort of thing they put in cert exams and cert exam books, however. The only variable initialised in that line is k; neither the local i nor j has a value associated with it. Remember there are two is, which makes it even more complicated.
 
Ioanna Katsanou
Ranch Hand
Posts: 170
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your answer ..
I found this in the https://www.udemy.com/course/java-se-11-developer-1z0-819-ocp-course-part-1/  courses in Udemy.

So if I understood well the case is as follows:

We have a static int i before the main method. That means that this is a class variable and is initialized to 0.
In line 1 we declare three local variables : i, j and k and from these three only local variable k is initialized to 0.
In line 2, we initialize local variable i , to the value of the class variable i plus the local variable k , so local i = class i + k = 0+0 = 0
In line 3 , we just change the value of the already initialized k into the local variable's i value ++ plus the k , so local k = local i ++   +k= 1+0 = 1
and in line 4, we try to change the value of the class variable i but j has not been initialize yet.

Is that correct?

 
Saloon Keeper
Posts: 23409
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is equivalent to

That contrasts with C, Java's predecessor, where the values of i and j would be indeterminate. Java always initializes its variables to a default value if no explicit initialization is done.

Also note this construct: where all variable values initialize to 10. That form is fairly common in C, although it's widely discouraged and I don't use it.
 
Campbell Ritchie
Marshal
Posts: 72406
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[campbell@campbellsComputer]$ java InitialisaitonDemo.java
InitialisaitonDemo.java:6: error: variable i might not have been initialized
       System.out.println(i);
                          ^
1 error
error: compilation failed

No, the only variable to be initialised is k.
The behaviour of JShell slightly is different. It presumes the declaration is a field declaration in which case i and j have default initial values. You can convert the declaration to a local variable by wrapping it in {...} on JShell.
 
Attractive, successful people love this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic