Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Creating primitive data types from contents of String

Reply
Thread Tools

Creating primitive data types from contents of String

 
 
Jesper Sahner
Guest
Posts: n/a
 
      11-13-2004
Hi!

Let's say that s is a String containing a name. How do I then
(dynamically) create an integer with that specific name like:

String s="abc";
int abc;

String s="def";
int def;

....

E.g. the String-names could be some names read from a file.

Regards,
Jesper
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      11-13-2004
Jesper Sahner <(E-Mail Removed)> scribbled the following:
> Hi!


> Let's say that s is a String containing a name. How do I then
> (dynamically) create an integer with that specific name like:


> String s="abc";
> int abc;


> String s="def";
> int def;


> ...


> E.g. the String-names could be some names read from a file.


You can't. Variable names, at least local ones (method scope) are an
entirely compile-time concept and can't be manipulated at run-time.
What are you really trying to accomplish with this?

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"It's time, it's time, it's time to dump the slime!"
- Dr. Dante
 
Reply With Quote
 
 
 
 
Oscar kind
Guest
Posts: n/a
 
      11-13-2004
Jesper Sahner <(E-Mail Removed)> wrote:
> Let's say that s is a String containing a name. How do I then
> (dynamically) create an integer with that specific name like:
>
> String s="abc";
> int abc;


You don't. After all: how would you reference it?

If you need to create labels for numbers/objects/... dynamically, use a
Map. Also note that you cannot use primitive types like int, char,
boolean, etc. Use Integer, Crahacer, Boolean, etc. instead.


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
 
Reply With Quote
 
Alberto
Guest
Posts: n/a
 
      11-13-2004
I'm not sure why would you like to do that. However, if you are trying
to dynamically create an object depending on input entered, you may
want to read about "Reflection". Keep in mind this will only work on
data objects, not on primitives.

You won't be able to create a primitive, they are not objects. You
could use wrapper classes (Objects) for those primitives (or you could
write your own). If you still insit in storing and defining a variable
from input, a good approach could be to use a data structure (create
your own object) to do the abstraction for you.

 
Reply With Quote
 
Jesper Sahner
Guest
Posts: n/a
 
      11-14-2004
Hi again!

Consider the following problem:
You have some information stored in a data-file, and in addition you
have a header-file with a description of the record-layout
(variable-name, position, length, format, label etc.).

The task then is to read from the data-file using the
header-information. If e.g. the header contains a description of a
variable 'var1' of type 'double' and another variable 'var2' of type
'int' then the Java-code should declare these variables on basis of
the header-information like:
double var1;
int var2;

Then you could make some calculations involving var1 and var2, write
the result to a new file etc. - very similar to a database-lookup.

How would you do this?

Regards,
Jesper
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      11-14-2004
Jesper Sahner wrote:
<snip>
> The task then is to read from the data-file using the
> header-information. If e.g. the header contains a description of a
> variable 'var1' of type 'double' and another variable 'var2' of type
> 'int' then the Java-code should declare these variables on basis of
> the header-information like:
> double var1;
> int var2;
>
> Then you could make some calculations involving var1 and var2, write
> the result to a new file etc. - very similar to a database-lookup.


Again, your motive escapes me (and others, apparently).
A variable is merely a convenient handle to either a primitive or an
Object. The value or object being referenced has no need to know the
name(s) (if any) which refer to it.
BTW, I just LOVED the post which mentioned the scenario of foo( 3 )!
Guess what? There's no variable name associated with the value!
Please think carefully about what you're trying to achieve. It might
turn out to be impossible, and for good reason.
Perhaps if you describe what you're trying to accomplish? ...

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.

 
Reply With Quote
 
Casey Hawthorne
Guest
Posts: n/a
 
      11-14-2004
Does the following page describe what you are trying to do?

http://mindprod.com/jgloss/eval.html

eval
In many languages you can take a dynamically created String such as
"6*(4+6^2)-cos(20)" and ask to have it evaluated, as if it were a
miniature computer program. The function to do this often has a name
such as eval. Java has no such function. What can you do? Here are
four different approaches:
....

--
Regards,
Casey
 
Reply With Quote
 
hiwa
Guest
Posts: n/a
 
      11-14-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Jesper Sahner) wrote in message news:<(E-Mail Removed). com>...

I would write a simple text processing program which generate a
relevant part -- variable declaration part -- of the target Java
source code.

As mentioned earlier, names in the source program are only relevant to
compiler. In order to feed them to the compiler, we can not use
nothing but ordinary source code. We have to make source text from
your header etc.
 
Reply With Quote
 
Jesper Sahner
Guest
Posts: n/a
 
      11-14-2004
Hi!

As I have described the task is somewhat similar to a database-lookup.

It is true that a variable is just a reference to an object or a
primitive type. However it is important that these variables have
names corresponding to the header-information. If the header states,
that the file to be read contains two variables 'var1' and 'var2' of
type 'double' and 'int' then it is important that they are given the
right names 'var1' and 'var2' when read from the file, because
typically the code following the read depends on the right names -
like a database-lookup.

Regards,
Jesper
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      11-14-2004
Jesper Sahner <(E-Mail Removed)> scribbled the following:
> Hi!


> As I have described the task is somewhat similar to a database-lookup.


> It is true that a variable is just a reference to an object or a
> primitive type. However it is important that these variables have
> names corresponding to the header-information. If the header states,
> that the file to be read contains two variables 'var1' and 'var2' of
> type 'double' and 'int' then it is important that they are given the
> right names 'var1' and 'var2' when read from the file, because
> typically the code following the read depends on the right names -
> like a database-lookup.


I still can't fathom why you think you have to use actual variable names
for this. If the field names are so important, just use a big Map where
the keys are the names "var1" and "var2" etc. and the values are Doubles
or Integers or whatever objects you use to encapsulate what you read
from the file.
Trying to dynamically read or write variable names in the program source
code at run-time is pretty much always a sign of a design problem or a
misunderstanding of how compiled languages work.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
no more primitive data types in Java (JDK 10+). What do you think? Nasser M. Abbasi Java 74 04-29-2012 05:28 PM
Default primitive values from primitive Class<?> object. Daniel Pitts Java 7 10-23-2008 04:30 PM
Primitive vs. non-primitive l-value richardclay09@yahoo.co.uk C++ 7 05-09-2005 02:52 PM
Loading classes with primitive types in constructor munki Java 5 10-08-2003 01:24 PM
Collections API for primitive types =?ISO-8859-1?Q?S=F8ren_Bak?= Java 0 08-27-2003 06:59 PM



Advertisments