Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# declaration of variable problem

Posted on 2008-10-06
Medium Priority
198 Views
I want to do something like this:

if (some condition) {
int x;
}
else {
double x;
}

x = something;

I want x to be either int or double, depending on some condition
How can this be done?
0
Question by:allelopath
[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
• 2
• 2

LVL 60

Accepted Solution

Kevin Cross earned 2000 total points
ID: 22655009
Technically you can do something like this:
http://java.sun.com/j2se/1.3/docs/api/java/lang/Number.html

You can then use .doubleValue() or .intValue().
``````boolean isInt = true;
Number x;

if (isInt) {
x = new Integer(0);
} else {
x = new Double(0);
}

x = 1.0;
``````
0

LVL 24

Expert Comment

ID: 22656775
In effect, what you want is impossible in many languages.
Only in C (and derived languages) there is a thing called a 'union':

union
{
int i;
double d;
} x;

Then you can say: x.i = 6;   or x.d = 7.8
But even then you can not use the integer entered as a double.

Can you tell us what you really want.
I can not believe you want to save a few bytes of space .................

;JOOP!
0

LVL 60

Assisted Solution

Kevin Cross earned 2000 total points
ID: 22658060
Here, I will do the impossible again!

@sciuriware, just messing with you...

@allelopath, you cannot have the variable instantiated only under the if statement to use later as variables under the ifs in your code above are scoped only to the if statement containing them.  To fix that problem you must move the declaration of x above the if.  Once you do that you have the problem of only being able to declare one type.  Either declare a type using UNION which quite honestly is no different than creating a class called IntDouble that has members i and d and do the same thing in languages without union.  Alternative, is use a base class of types you want like in this case we can use Number which is base class for Integer and Double.  Those are not the primitive types you are using, but as you can see you can get the primitives easily using .intValue() or .doubleValue().

Hope that helps.
``````boolean isInt = true;
Number x = 1.78;

JOptionPane.showMessageDialog(null, (isInt)?x.intValue():x.doubleValue());
``````
0

LVL 24

Expert Comment

ID: 22658325
Correct!

;JOOP!
0

## Featured Post

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
###### Suggested Courses
Course of the Month5 days, 2 hours left to enroll