• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

variables?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java how variables are initialized by jvm?
Ex:
public class Test{
int i = 5;
int j;
public void tester(){
i = 10;
j =5;


}
}

How the variables are initialized during the above situations?
1.Object is just created
2.While executing the method "tester".
Don't tell me that these are primitives and are directly initialized to default values first and then assigned values, Please let me know the flow of jvm
.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shiva prasad. wrote: . . . these are primitives and are directly initialized to default values first and then assigned values, . . .

They are initialised to default values and then assigned values.

If you want to know the flow of execution, put lots of System.out.println() instructions in your code. Print out the line numbers and the values of the variables. Or use a pencil and paper, but don’t expect us to do your work for you.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Don't tell me that these are primitives and are directly initialized to default values

Not all primitives are given a default value - or rather it depends on the variable and not type to get a default value.
 
shiva prasad.
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:

Don't tell me that these are primitives and are directly initialized to default values

Not all primitives are given a default value - or rather it depends on the variable and not type to get a default value.



Hi Jhon Jai , I want an example to understand the point you have mentioned.
As far as my knowledge the jvm first reads the statements then assigns default values to the variables declared,
then again jvm checks for the variables which are initialized the during declaration/initialization and assigns them with initialized values.

Am I correct let me know,please explain with an example.
And thanks for answering my qusetion.

Hi Campbell Ritchie thanks for your suggestion.
Let me know can I put a sysout statemnt which can print the values of variables while the jvm reads code statements before creation of object?
Of course I can debug my code and achieve that.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shiva prasad. wrote:
Hi Jhon Jai , I want an example to understand the point you have mentioned.
As far as my knowledge the jvm first reads the statements then assigns default values to the variables declared,
then again jvm checks for the variables which are initialized the during declaration/initialization and assigns them with initialized values.


Local variables declared in the method are not initialized with default values.

shiva prasad. wrote:
Hi Campbell Ritchie thanks for your suggestion.
Let me know can I put a sysout statemnt which can print the values of variables while the jvm reads code statements before creation of object?
Of course I can debug my code and achieve that.


You can use System.out.println (Used to print to the standard output) to print the variable contents only after it has been declared (and initialized if they are local variables)
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shiva prasad. wrote: . . . Let me know can I put a sysout statemnt which can print the values of variables while the jvm reads code statements before creation of object? . . .

No. Those are instance variables, and they do not exist before you create your object. You can try putting your print statements as the first line of the constructor, however, or in an instance initialiser.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shiva prasad. wrote:
Don't tell me that these are primitives and are directly initialized to default values first and then assigned values, Please let me know the flow of jvm
.



You should read about the constructors and instance initializers (as what Campbell mentioned) to get to know the flow. However, you can get to know the FLOW only by putting a SOP (System.out.println) statements before and after each statement. Or you can use a debug facility in your IDE (Eclipse, Netbeans whatever you use).
 
reply
    Bookmark Topic Watch Topic
  • New Topic