• 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

about java primitive ?

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a very basic java question about primitives.
int a = 5;
the value 5 is created in the memory. right ?
another location for the variable called a is created.
And this location actually points to the memory location
where 5 is. right ?
is this correct ?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
You have asked a very fundemental question about the internal workings of Java which may not be easy to explain or to understand. Well, here goes:
When you compile Java source code, you generate byte code for the JVM which fetches each bytecode one by one and executes a piece of native machine code equivalent to each Java bytecode.
Now, how does this relate to memory space on the machine?
The JVM makes extensive use of stacks where values are loaded on and popped off. Each position on the stack is referred to by a local variable array. So using your example code, the variable int a will be stored in the zeroth element of the local variable array. It will reference the first position of the stack that stores your value integer 5.
The stack will then be manipulated by instructions in the bytecode.
Is that clear as mud?
Don't lose sleep over it Mark because I don't think you will be asked about it in the exam.
chung
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite, Mark. For starters, it depends whether the integer variable is declared as a class member or in a local scope (i.e., inside a method, try block, loop, or other flow-control construct).
But wherever you declare x and y both to be 5, you will in fact have two 'literal' values in memory, not a shared one. This is in part is what distinguishes primitives from object references: primitives "hold" literal values (sometimes called register values in the C world), references "point to" values.
[ January 23, 2002: Message edited by: Michael Ernest ]
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the explaination. One last point.
but String literals can be shared ? right ?
but then ofcourse String literals are object though. just to confirm again....
thanks
mark

Originally posted by Michael Ernest:
Not quite, Mark. For starters, it depends whether the integer variable is declared as a class member or in a local scope (i.e., inside a method, try block, loop, or other flow-control construct).
But wherever you declare x and y both to be 5, you will in fact have two 'literal' values in memory, not a shared one. This is in part is what distinguishes primitives from object references: primitives "hold" literal values (sometimes called register values in the C world), references "point to" values.
[ January 23, 2002: Message edited by: Michael Ernest ]

 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes mark, string literals are shared. That's why when you write
String a = "hi";
String b = "hi";
then this test returns true
if (a==b)...
because both references are pointing to the same string object.
Rob
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear in mind this will not hold if you instantiate the Strings, a la:
String abc = new String("Yo");
String def = new String("Yo");
in this case (abc == def) is false (they don't point to the same object), but (abc.equals(def)) is true (they do have the same value).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic