[Webinar] Streamline your web hosting managementRegister Today


Variable conversion

Posted on 2000-02-01
Medium Priority
Last Modified: 2010-04-01
How can you convert different types of variables?

I mean as in VB where all you needed to do was put for instance:
dim x as int
x = 25
dim txt as String
txt = Str(x)
where txt would be "25"

Is there anyways easy to do this?
Question by:Nicolas_St_Amour
1 Comment
LVL 16

Accepted Solution

imladris earned 30 total points
ID: 2479054
Variables will all convert to String with little effort. For instance, to show them on the console you can just concatenate them into a String:

int x=10;
long y=20;
char c='A';

System.out.println("VARS "+x+" "+y+" "+c);

In fact you can turn them into a String in general like that:

String txt="VARS "+x+" "+y+" "+c;

Note that for this to work, the compiler needs to know it is supposed to create a String. So to turn just x into a String you could do this:

String txt=""+x;

Also, there is a class associated with primitive types which provides toString methods:

String txt=Integer.toString(x);

For Integer there are also variations to generate String representations in different bases.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

590 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