Java Variable length

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
Who is Participating?
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.
you can make use of a char array for that. Since String is an object you cannot set the length of it
ceasleyAuthor Commented:
thanks . . .
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.