1

If a String Object references to a null value, is there any memory allocated for this value?

e.g. String str = ""; String str = null;

str's value is null, or empty String, is this allocated somewhere?

TEXT ADDED what happens when a String reference points to null (how do you specify this null and still not allocating memory for this "specification"?), how is this information stored? because the reference is practically storing an address.

aurelius
  • 3,946
  • 7
  • 40
  • 73
  • Actually, you've allocated a value to `str`, being `""`. http://stackoverflow.com/questions/4802015/difference-between-null-and-java-string – Joetjah Jan 06 '15 at 12:50
  • 1
    `str's value is null, or empty String` I hope you know that both doesn't mean the same ... – Tom Jan 06 '15 at 12:51

2 Answers2

2
String nullObj = null;
String emptyStr= ""

Both are completely different.

In case of null, it refers to nothing. But still memory allocates. null takes 4 bytes if it is 32-bit systems and 8 bytes if it is 64-bit system.

For empty string (""), that's 100% a String object and memory allocates.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Case -1 :

String s = null;

here, memory is allocated only for the reference s. null is not an object. So no memory is allocared for that.

Case -2 :

String s = ""; // empty string 

Here, memory is allocated to both String reference s as well as the actual String value "" (Yes, "" is different from null)

rturrado
  • 7,699
  • 6
  • 42
  • 62
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • so then the reference points to null, how this is stored? because the reference is practically storing an address – aurelius Jan 06 '15 at 12:59
  • 1
    @aurelius - In JVM, the references which point to null are handled differently. So, if you have `String s = null`, the underlying byte code instruction would be `aconst_null` followed by `astore_1`(store null reference in variable `s`). This means `null` values are handled in a different way using a different opcode. – TheLostMind Jan 06 '15 at 13:06
  • so practically, if the JVM generates some code, specific for this null values, then there is some memory allocated... – aurelius Jan 06 '15 at 13:20
  • 1
    @aurelius - 1. Here,the *compiler* generates code which will be *run* by the JVM :). Next, don't get confused between *instructions* and *objects*. `aconst_null` is an *instruction*. There is no object called `null` which occupies memory. Only the field (`s`) in which this null reference is loaded will occupy 4 bytes / 8 bytes depending on whether the system is 32 bits or 64 bits. – TheLostMind Jan 06 '15 at 13:24
  • so if you do String s = null; then the s reference (address) will be null? --- i am a bit confused now... – aurelius Jan 06 '15 at 13:28
  • @aurelius - No. The reference will not be null. The value to which the reference `s` points will be `null`. Reference is like a *sign board*, it always points to something. It can either point to `Sydney` or point `upwards towards nothing in particular` :P – TheLostMind Jan 06 '15 at 13:42
  • so then, from "The value to which the reference s points will be null" null is a value located somewhere in memory...? – aurelius Jan 06 '15 at 15:47
  • @aurelius - No. `aconst_null` is an instruction. It pushes a *null reference* on to the stack. So, `a null reference itself is different from normal references`. – TheLostMind Jan 07 '15 at 07:12
  • ok, i think i get it now... the "s" reference which points to null, is a reference that is pushed in a "null" references location from stack, and that's how the JVM threats it different, and knows that the value to which the reference points too is null? Is this how it goes? – aurelius Jan 07 '15 at 07:21
  • 1
    @aurelius - The `null` constant reference is pushed *into the variable s*. – TheLostMind Jan 07 '15 at 08:10
  • ok, got it, thank you so much, all the best! – aurelius Jan 07 '15 at 12:00