• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1626
  • Last Modified:

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
0
ceasley
Asked:
ceasley
  • 2
1 Solution
 
kiranhkCommented:
No.
0
 
kiranhkCommented:
you can make use of a char array for that. Since String is an object you cannot set the length of it
0
 
ThummalaRaghuveerCommented:
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
 
ceasleyAuthor Commented:
thanks . . .
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now