Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Variable length

Posted on 2004-10-22
4
Medium Priority
?
1,625 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
Comment
Question by:ceasley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:kiranhk
ID: 12383240
No.
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12383246
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:
ThummalaRaghuveer earned 200 total points
ID: 12383770
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
ID: 12383865
thanks . . .
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

636 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