Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java input = "" isn't the same as input = null?

I'm running a J2ME Application and run into some serious memory problems.
So I built in another step to clear the huge input string and process its data and clear it. But it didn't solve the problem until I set input = null and not input = "".

Shouldn't it be the same in terms of memory management? Can somebody explain me the difference please?

Thanks,
rAyt

for(int x = 0; x <= ChunksPartCount; x++)
{
    _model.setLoading_bar_progress((x * ChunkSize));
    input += web_service.FullCompanyListChunksGet(x, ChunkSize);

    if((x * ChunkSize) > 5000)
    {
        ReadXML(input);
        input = null;
    }
}

Edit:
I still want to flag an answer as the solution. I think mmyers remarks are going in the right direction.

like image 962
Henrik P. Hessel Avatar asked Jul 24 '09 18:07

Henrik P. Hessel


People also ask

Is null the same as in Java?

Null is a reserved keyword in the Java programming language. It's technically an object literal similar to True or False. Null is case sensitive, like any other keyword in Java. When programming in Java, it's important to write null in lowercase.

How do you input a null in Java?

If you want to use a BufferedReader you can do the following. BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br. readLine()) != null) { //Do what you want with the line. }

Why am I getting null output in Java?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type. In the absence of a constructor, the getArticles() and getName() methods will return a null reference.

Is null the same as empty Java?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.


1 Answers

Every variable is actually a pointer to "Data" in memory.

input = "" assigns input to a string object. It has a length (0) and an empty array, as well as a few other pieces of data associated with it.

input.length() will return 0 at this point.

input = null makes input point to "Invalid". Null is kind of a special case that means this pointer points to NOTHING, it's unassigned.

input.length() will now throw an exception because you are calling .length on nothing.

like image 136
Bill K Avatar answered Oct 04 '22 02:10

Bill K