# Data Types

• (2 Pages)
• 1
• 2

## 16 Replies - 1599 Views - Last Post: 27 May 2012 - 09:12 AM

Reputation: 0
• Posts: 98
• Joined: 12-December 11

# Data Types

Posted 11 May 2012 - 04:12 AM

Given :
int upper=0;
double lower=0;
which Java code segment contains a syntax error:
A. upper = upper;
B. lower = upper;
C. lower = lower;
D. upper= lower;

I know double lower = 0 is wrong because double's can not store whole numbers. But I dont understand the answer options. Can anyone help me?
Is This A Good Question/Topic? 0

## Replies To: Data Types

### #2 DarenR

• D.I.C Lover

Reputation: 301
• Posts: 2,160
• Joined: 12-January 10

## Re: Data Types

Posted 11 May 2012 - 05:10 AM

a double can have an int but an int cant have a double. The reason is the amount of memory int can hold-- i think ints hold something like -2,147,483,647 to 2,147,483,647
while a double holds 1.7976931348623158e+308.

like this int = 1
double = 1.1

even though the int looks like 1 it can be infered as 1.0 thus it can be a double but a double as 1.1 can not be infered as 1.0

### #3 jon.kiparsky

• Pancakes!

Reputation: 6735
• Posts: 11,010
• Joined: 19-March 11

## Re: Data Types

Posted 11 May 2012 - 05:20 AM

c9-adams, on 11 May 2012 - 06:12 AM, said:

Given :
int upper=0;
double lower=0;
which Java code segment contains a syntax error:
A. upper = upper;
B. lower = upper;
C. lower = lower;
D. upper= lower;

I know double lower = 0 is wrong because double's can not store whole numbers. But I dont understand the answer options. Can anyone help me?

If you're having trouble with this question, you need to review the data types and what they mean. Particularly since this sentence

Quote

I know double lower = 0 is wrong because double's can not store whole numbers

doesn't really make any sense.

If you're in a class, this is certainly covered in your textbook. It's also covered in the Sun tutorials.

DarenR, on 11 May 2012 - 07:10 AM, said:

a double can have an int but an int cant have a double. The reason is the amount of memory int can hold-- i think ints hold something like -2,147,483,647 to 2,147,483,647
while a double holds 1.7976931348623158e+308.

This is right-ish, but not very right. You might want to review data types as well.

The issue is not how much memory each can hold. It is true that an int is a 32-bit type and a double is a 64-bit type, but you'd have the same issue with assigning from a float to an int., or a double to a long. The issue, as you suggest, is one of conversion, specifically loss of precision.

This post has been edited by jon.kiparsky: 11 May 2012 - 05:21 AM

### #4 SpartanGuy07

Reputation: 32
• Posts: 149
• Joined: 08-September 11

## Re: Data Types

Posted 11 May 2012 - 07:41 AM

Quote

I know double lower = 0 is wrong because double's can not store whole numbers. But I dont understand the answer options. Can anyone help me?

First off, this is not true.

c9-adams, on 11 May 2012 - 07:12 AM, said:

Given :
int upper=0;
double lower=0;
which Java code segment contains a syntax error:
A. upper = upper;
B. lower = upper;
C. lower = lower;
D. upper= lower;

So you know that an int has to be a whole number. With a double, you know you can store decimals, however nothing says a whole number is not acceptable.

So lets say instead of 0 and 0.
int upper = 5
double lower = 2.3
Which of the following assignments will not work (as expected):
A. upper = 5
B. lower = 5
C. lower = 2.3
D. upper = 2.3

Keep in mind that this won't necessarily stop you from compiling your program. It's a matter of loss of precision, like jon had mentioned.

This post has been edited by SpartanGuy07: 11 May 2012 - 07:43 AM

### #5 jon.kiparsky

• Pancakes!

Reputation: 6735
• Posts: 11,010
• Joined: 19-March 11

## Re: Data Types

Posted 11 May 2012 - 07:51 AM

SpartanGuy07, on 11 May 2012 - 09:41 AM, said:

Keep in mind that this won't necessarily stop you from compiling your program. It's a matter of loss of precision, like jon had mentioned.

Actually, the compiler will not allow implicit downcasts.
```double d = 2.3;
int i= d;
```

will block compilation unless you do it with an explicit cast:
```double d = 2.3;
int i= (int)d;
```

This will perform a downcast from double to int: i will now contain the greatest integer such that i <= d.
(not exactly true - where's the lie in that? +rep for the OP if you can spot it!)

### #6 Dogstopper

Reputation: 2806
• Posts: 10,826
• Joined: 15-July 08

## Re: Data Types

Posted 11 May 2012 - 07:55 AM

I spotted it.
Spoiler

### #7 jon.kiparsky

• Pancakes!

Reputation: 6735
• Posts: 11,010
• Joined: 19-March 11

## Re: Data Types

Posted 11 May 2012 - 08:07 AM

Man, you're harsh. Snatching rep from the mouths of newbies!

### #8 Dogstopper

Reputation: 2806
• Posts: 10,826
• Joined: 15-July 08

## Re: Data Types

Posted 11 May 2012 - 08:09 AM

Oh yeah. Total rep whore. But no, that's why I put in a spoiler. I'd like to see a newbie get it too.

### #9 SpartanGuy07

Reputation: 32
• Posts: 149
• Joined: 08-September 11

## Re: Data Types

Posted 11 May 2012 - 08:09 AM

jon.kiparsky, on 11 May 2012 - 10:51 AM, said:

Actually, the compiler will not allow implicit downcasts.
```double d = 2.3;
int i= d;
```

will block compilation unless you do it with an explicit cast:
```double d = 2.3;
int i= (int)d;
```

My mistake then, I thought last time I did that I only received a warning for loss of precision.

Either way, my point was that it is technically possible.

This post has been edited by SpartanGuy07: 11 May 2012 - 08:12 AM

### #10 jon.kiparsky

• Pancakes!

Reputation: 6735
• Posts: 11,010
• Joined: 19-March 11

## Re: Data Types

Posted 11 May 2012 - 08:16 AM

Dogstopper, on 11 May 2012 - 10:09 AM, said:

Oh yeah. Total rep whore. But no, that's why I put in a spoiler. I'd like to see a newbie get it too.

Naw, you can't solve a solved problem. Your turn, then - come up with a puzzler for him. Something around data types.

Ooh, here's a good one. What's the best way to round a positive integer using only primitive operations? (ie, no method calls)

This one is a little tricksy, you have to think about it mathematically. However, if you've been following this thread, you know everything you need to know about java to do this.

### #11 Dogstopper

Reputation: 2806
• Posts: 10,826
• Joined: 15-July 08

## Re: Data Types

Posted 11 May 2012 - 08:22 AM

Here's a good one in addition to yours:

How can you swap two positive integers without using a third variable.

### #12 jon.kiparsky

• Pancakes!

Reputation: 6735
• Posts: 11,010
• Joined: 19-March 11

## Re: Data Types

Posted 11 May 2012 - 08:36 AM

Dogstopper, on 11 May 2012 - 10:22 AM, said:

Here's a good one in addition to yours:

How can you swap two positive integers without using a third variable.

Now that one's not easy. Think a little bit of a hint might be in order?

### #13 SpartanGuy07

Reputation: 32
• Posts: 149
• Joined: 08-September 11

## Re: Data Types

Posted 11 May 2012 - 10:38 AM

Dogstopper, on 11 May 2012 - 11:22 AM, said:

Here's a good one in addition to yours:

How can you swap two positive integers without using a third variable.

Spoiler

Still thinking on Jon's...

This post has been edited by SpartanGuy07: 11 May 2012 - 10:43 AM

### #14 Dogstopper

Reputation: 2806
• Posts: 10,826
• Joined: 15-July 08

## Re: Data Types

Posted 11 May 2012 - 06:07 PM

Spartan, you are close, but it doesn't work with a negative number as a. This should also not require any if statements or otherwise.

### #15 SpartanGuy07

Reputation: 32
• Posts: 149
• Joined: 08-September 11

## Re: Data Types

Posted 14 May 2012 - 07:43 AM

Haha darn I didn't take negative into account. The only other way I can think of is

Spoiler

maybe?

This post has been edited by SpartanGuy07: 14 May 2012 - 07:49 AM