Solved

Java Variable length

Posted on 2004-10-22
1,620 Views
Last Modified: 2008-02-01
Is it possible to set the length of a variable in java like you can in VB?

Visual Basic Example:

Private Variable As String * 50
0
Question by:ceasley
    4 Comments
     
    LVL 8

    Expert Comment

    by:kiranhk
    No.
    0
     
    LVL 8

    Expert Comment

    by:kiranhk
    you can make use of a char array for that. Since String is an object you cannot set the length of it
    0
     
    LVL 2

    Accepted Solution

    by:
    You cannot set length for a String in java.....

    In java a String can be of any length......

    Initial memory allocation will be the length of the string U are storing in that String variable.

    and with String in java all operations that U do for String like say U have appended it with another charecter actually creates a new String Object containg the modified value and the variable reference is changed to new string object.

    say String a = "hi"

    modify it with a = a+", how are U doing"

    Initial value "hi" is a different object and modified "hi, how are U doing" is a different object.

    before modificatioin a  references "hi" after changing a references a different object containing "hi, how are U doing".

    In java for String i think U cannot restrict a string not to exceed a given length. Its length just gets adjusted by what U are storing in it dynamically.

    If U want to restrict the length U have to do it programatically do it by ensuring that String.length is always less than 50.

    U can do it by using char array but that way U would be missing some of the good features of String.
    0
     

    Author Comment

    by:ceasley
    thanks . . .
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now