Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2011
    Location
    Eindhoven, The Netherlands
    Posts
    4
    Rep Power
    0

    Thumbs down How to save large amounts of similar objects?

    A String in java cannot be deleted, nor changed or overwritten.

    I have an Object.txt (20000 lines, 1 Object description per line) and a constructor Object(String line) to convert the .txt file into an Array of 20000 Objects.

    Objects[0] is no problem, read line[0] -> Object. But then?

    How can I avoid that I end up with 19999 lines as garbage, to be collected the week after next or later?

    Or is the idea wrong and is the protocol completely different?


    Nieuwenhuizen
    2011-05-12T21:27


    jk@nieuwenhuizen-jk.nl

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Please post some code that shows your problem. I'm not sure what you are asking.

  3. #3
    Join Date
    Mar 2011
    Location
    Eindhoven, The Netherlands
    Posts
    4
    Rep Power
    0

    Default

    /*
    After 34 years of almost every programming-language there has been, I changed
    to C++ by 1993 and was very happy with it until GNU's gcc -v 4.x.x. As
    preparation to Android I started a home-made crash-course Java after I guess
    80000 - 90000 hours of computing, last november.

    I must say that I regret that I did not do so 10 years earlier, but there are
    a few things I miss. One is the address operator (&), in Java one cannot see
    where an item is stored in memory. Nor ( I believe ) how much memory remained
    unused.

    I learned that Strings cannot be overwritten nor deleted and made this test:

    import java.io.*;

    public class StringTest {

    static String line;

    public static void main(String[] args) {

    line = "Peter is razy";
    line = "Mary is a fat cow";

    System.out.println(line);
    } // main
    } // StringTest

    As expected: The java compiler does not flag an error on using one variable
    twice, and the 'program' outputs last:

    Mary is a fat cow

    This means that the first, by not being overwritten nor deleted, still
    occupies part of my memory until the garbage collector removes it to-night,
    to-morrow, next month ( ? ).

    The other disappointment on Java is that I cannot see how much memory remained
    unused. If so, I could call names to the whole population of the town I live
    in and know if my guess is right ( please tell me if I am wrong ).


    This is my real problem:

    public class Person {
    String firstName;
    String lastName;
    String birthDate;

    Person(String line) {
    firstName = line.substring(0,20);
    lastName = line.substring(20,34);
    birthDate = line.substring(34,40);
    }
    } // Person


    // to be read Diskfile of 100000 items from a database: Persons.txt:

    Nieuwenhuizen Hans 331118
    Nieuwenhuizen Rob 370916
    Langendoen Frederik 251214
    Kingsma Patrick 681204
    Pax Frank 351026
    Pax Elly 380728
    Pax Jan 331118
    Lange Pauli 240419
    .....
    .....

    Liang, Daniel Y listing 9.8 page 351 in comprehensive version 8 tells me how to
    do it:

    quote
    -----

    12 while (input.hasNext()) {
    13 String line = input.next();
    14 Persons[i++] = new Person(line);
    15 )

    unquote
    -------

    and I fear that at Persons[97388] my memory will explode.

    And now that I contacted you: I do understand that a 'static' variable is one
    which has that name in C++ also: one and the same value for all instances,
    saving it once is sufficient.

    But why is it that I cannot call a non-static variable from a static text, which
    enforced me to make StringText.line static?

    Thanks a lot

    Nieuwenhuizen
    2011-05-13T09:40 CEST

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Alot there...

    Quote Originally Posted by nieuwenhuizen-jk View Post
    /*
    After 34 years of almost every programming-language there has been, I changed
    to C++ by 1993 and was very happy with it until GNU's gcc -v 4.x.x. As
    preparation to Android I started a home-made crash-course Java after I guess
    80000 - 90000 hours of computing, last november.

    I must say that I regret that I did not do so 10 years earlier, but there are
    a few things I miss. One is the address operator (&), in Java one cannot see
    where an item is stored in memory. Nor ( I believe ) how much memory remained
    unused.
    Learn to live without them.
    Java is not about manipulating memory addresses.

    Quote Originally Posted by nieuwenhuizen-jk View Post
    I learned that Strings cannot be overwritten nor deleted and made this test:

    import java.io.*;

    public class StringTest {

    static String line;

    public static void main(String[] args) {

    line = "Peter is razy";
    line = "Mary is a fat cow";

    System.out.println(line);
    } // main
    } // StringTest

    As expected: The java compiler does not flag an error on using one variable
    twice, and the 'program' outputs last:

    Mary is a fat cow

    This means that the first, by not being overwritten nor deleted, still
    occupies part of my memory until the garbage collector removes it to-night,
    to-morrow, next month ( ? ).
    Not quite. Those Strings are hard coded there, and so stored in the String literal pool by the compiler. it is possible the compiler is smart enough to know that line is instantly redirected to the second String and so not store the first, but I don't know if that's the case. In any case, neither String (in this example) is ever eligible for garbage collection.

    Quote Originally Posted by nieuwenhuizen-jk View Post
    The other disappointment on Java is that I cannot see how much memory remained
    unused. If so, I could call names to the whole population of the town I live
    in and know if my guess is right ( please tell me if I am wrong ).


    This is my real problem:

    public class Person {
    String firstName;
    String lastName;
    String birthDate;

    Person(String line) {
    firstName = line.substring(0,20);
    lastName = line.substring(20,34);
    birthDate = line.substring(34,40);
    }
    } // Person


    // to be read Diskfile of 100000 items from a database: Persons.txt:

    Nieuwenhuizen Hans 331118
    Nieuwenhuizen Rob 370916
    Langendoen Frederik 251214
    Kingsma Patrick 681204
    Pax Frank 351026
    Pax Elly 380728
    Pax Jan 331118
    Lange Pauli 240419
    .....
    .....

    Liang, Daniel Y listing 9.8 page 351 in comprehensive version 8 tells me how to
    do it:

    quote
    -----

    12 while (input.hasNext()) {
    13 String line = input.next();
    14 Persons[i++] = new Person(line);
    15 )

    unquote
    -------

    and I fear that at Persons[97388] my memory will explode.
    What is it you're trying to do?
    That code example will potentially cause your system to throw an OOM because you are attempting to read in the entire phone directory and store it in memory.
    Put simply, that's not how your code should be written.

    Tell us what you;re trying to do with this phone directory and we'll tell you how it should be done.

    Quote Originally Posted by nieuwenhuizen-jk View Post
    And now that I contacted you: I do understand that a 'static' variable is one
    which has that name in C++ also: one and the same value for all instances,
    saving it once is sufficient.

    But why is it that I cannot call a non-static variable from a static text, which
    enforced me to make StringText.line static?

    Thanks a lot

    Nieuwenhuizen
    2011-05-13T09:40 CEST
    Static means the attribute is associated with the Class.
    Normal attributes are associated with an instance of a class, so do not exist with there being an actual object. Consequently you cannot reference them from a static context because they do not exist in a static context.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    ...Normal attributes are associated with an instance of a class, so do not exist without there being an actual object.
    fixed that for you :)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    What can I say...it's Friday!
    :)

  7. #7
    Join Date
    Mar 2011
    Location
    Eindhoven, The Netherlands
    Posts
    4
    Rep Power
    0

    Default

    I am afraid not. Sorry. The first line of class StringTest was "String line". `javac` gave me 3 identical errors:

    "non-static variable line cannot be referenced from a static context" :
    line = ....
    ^
    line = ....
    ^
    .....printline(line);
    ^
    After adding 'static' before 'String line' it appeared to be accepted by `javac`, but I do not understand why. ( To be honest, that is my main problem with java ). Then I could finish my message.

    Thanks for your time
    N

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Do some research on what "static" means vs the contents of an instance of a class created by using new.

  9. #9
    Join Date
    Mar 2011
    Location
    Eindhoven, The Netherlands
    Posts
    4
    Rep Power
    0

    Default

    I did not mention the word "phone directory", and what I sent was a simplified example.

    I just need to load items described by one line for each in a textfile into an Array of Objects, constructed as Object(String). The object can be anything from spare bolts to enormous jet-engines.for AirBus 800.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by nieuwenhuizen-jk View Post
    I am afraid not. Sorry. The first line of class StringTest was "String line". `javac` gave me 3 identical errors:

    "non-static variable line cannot be referenced from a static context" :
    line = ....
    ^
    line = ....
    ^
    .....printline(line);
    ^
    After adding 'static' before 'String line' it appeared to be accepted by `javac`, but I do not understand why. ( To be honest, that is my main problem with java ). Then I could finish my message.

    Thanks for your time
    N
    I will restate.
    Static (in Java) means "part of the Class".
    So a static method does not require an instance of the class to be called, and similarly with a static attribute.

    Since main() (by its very nature) is static, you cannot refer to anything non-static in a class without first creating an instance of that class.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by nieuwenhuizen-jk View Post
    I did not mention the word "phone directory", and what I sent was a simplified example.

    I just need to load items described by one line for each in a textfile into an Array of Objects, constructed as Object(String). The object can be anything from spare bolts to enormous jet-engines.for AirBus 800.
    OK.
    But what you do with that effects how you should deal with that data.
    If you have a million lines in your file then turning that into an array of a million objects is clearly a rubbish design.

  12. #12
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    What can I say...it's Friday!
    :)
    Woo-hoo! no, wait - Friday 13th... :eek:

  13. #13
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    As far as the OP goes, the 'main' method is not intended for class code - it is an entry point for execution. In the main method will typically be code to instantiate and test instances of the class, or, for an application control class, the code to instantiate the application.

    Methods and fields that are static belong to the class type and are shared by all instance objects of that class. This allows type-specific data and actions to be implemented, such as a count of the number of instances created, or the declaration of type-specific constants. The main method is static for obvious reasons (when starting an application or testing, you need to create an instance from scratch).

    You can access static data from any method, but a static method can only access static data unless explicitly given an instance object.

Similar Threads

  1. BankAccount, how to reject negative amounts? PLEASE HELP!
    By armedrabbit in forum New To Java
    Replies: 6
    Last Post: 03-07-2011, 04:12 AM
  2. Similar exception
    By drucey in forum New To Java
    Replies: 0
    Last Post: 11-25-2010, 10:31 PM
  3. Something similar to FileSystemWatcher in Java
    By Tyron in forum Advanced Java
    Replies: 3
    Last Post: 11-09-2010, 08:50 PM
  4. Replies: 6
    Last Post: 01-19-2009, 09:28 PM
  5. Replies: 1
    Last Post: 12-28-2008, 11:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •