Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: limiting string length

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default limiting string length

    Hi.
    I defined a java class thus:
    class Info{
    public String name;
    public String version;
    public String arch;
    double CPUSpeed;
    double ranUtil, CPUUtil;
    };
    but each object of such a class takes many bytes. How can I limit it to one fourth of a Kilo-Byte?

    Thanks in advance


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    560
    Thanks
    0
    Thanked 103 Times in 88 Posts

    Default Re: limiting string length

    You cant. There is no way for you to actually know how much memory these objects cost. It could be different for every JVM, it could be different every time you run your program.
    You are simply not supposed to know or even care about this.

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: limiting string length

    I mean wwith SQL, Pascal, we could specify that for example, this be a string that hols 30 characters. Is it possible with java? What is the difference between char str[30] and String under Java?

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    560
    Thanks
    0
    Thanked 103 Times in 88 Posts

    Default Re: limiting string length

    Nope, its not possible. A string is an object, the characters of the string are its private member attribute and you have absolutely no control over it.

    The difference between a string and a char array is that all the libraries out there want to use a string, and, believe me, you want to use a String as well.

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: limiting string length

    Quote Originally Posted by Cornix View Post
    Nope, its not possible. A string is an object, the characters of the string are its private member attribute and you have absolutely no control over it.

    The difference between a string and a char array is that all the libraries out there want to use a string, and, believe me, you want to use a String as well.
    No way!
    I am opposed to use String. I don't want to waste my RAM.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    560
    Thanks
    0
    Thanked 103 Times in 88 Posts

    Default Re: limiting string length

    Then dont use java, because with this attitude you wont get far with it. The java language has a certain philosophy, and what you just said is violating that philosophy quite heavily. Its just not the right language for you.
    Even if you use a char array, there is no guarantee that you will use less ram. You might end up using more. Strings are optimized, JVM's are very different and might also be optimized to work better with Strings. The JIT might be optimized to use Strings better. There are too many factors to say anything certain about this.

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: limiting string length

    Quote Originally Posted by Cornix View Post
    Then dont use java, because with this attitude you wont get far with it. The java language has a certain philosophy, and what you just said is violating that philosophy quite heavily. Its just not the right language for you.
    Even if you use a char array, there is no guarantee that you will use less ram. You might end up using more. Strings are optimized, JVM's are very different and might also be optimized to work better with Strings. The JIT might be optimized to use Strings better. There are too many factors to say anything certain about this.
    But the result is that I cannot create a queue of 1000 elements of objects of the class I created, be they optimized or not.

    I am not gonna become a java programmer, not now, but I now have to do my homework in Java, not any other language.

Similar Threads

  1. [SOLVED] string length problem
    By ma5sacre in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2014, 12:02 PM
  2. Limiting Letters in a String
    By 0w1 in forum Loops & Control Statements
    Replies: 4
    Last Post: September 9th, 2013, 05:40 AM
  3. Weird String .length() method
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 11th, 2012, 10:30 AM
  4. Maximum length of a string
    By ranjithfs1 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 6th, 2012, 08:47 AM