Solved

declaration of variable problem

Posted on 2008-10-06
4
181 Views
Last Modified: 2010-03-30
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
Comment
Question by:allelopath
  • 2
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 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;

Open in new window

0
 
LVL 24

Expert Comment

by:sciuriware
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 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 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());

Open in new window

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 22658325
Correct!

;JOOP!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 49
solarwind tftp server 2 32
Securing Jmx Console and web console 2 48
eclipse java  build path 6 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now