Computer Science Canada

Need Help with Java

Author:  CyCLoBoT [ Sat Sep 20, 2003 12:39 am ]
Post subject:  Need Help with Java

I need help with the following questions. Any help would be really appreciated.

1) Write a literal representing the long integer value twelve billion.

2) Write a hexadecimal integer literal representing the value fifteen.

Author:  rizzix [ Sat Sep 20, 2003 4:32 pm ]
Post subject: 

1) 12000000000L
2) 0xf

Author:  krishon [ Mon Sep 22, 2003 7:42 am ]
Post subject: 

................whoa wuts goin on, lol

Author:  rizzix [ Mon Sep 22, 2003 8:43 am ]
Post subject: 

First a literal is something like "hello world", 120.. etc
some thing that can be written and not necessarly represented by variables only.
As a matter of fact all primitives in their written form are literals. You can't really have an object as a literal. but in java's case a String (like "Hello World") would be the an object that is a literal.

here are a couple of literals:
int: 12
long: 12L
float:12.0F
double: 12.0
String: "Hello World"
char: 'A'
boolean: true
boolean: false

as you can see 12 and 12L are two different things. 12 allocates enough space for an int variable to hold it, while 12L allocates enough space for a long variable to hold it.

when the numbers are far too big like 12000000000000 etc.. that num won't fit in a int variable, thus this is a syntax error:
code:

int bignum = 1200000000000;


take a look at this:
code:

long bignum = 12;

you might have noticed i assigned a int literal to a variable of type long! Shouldn't that give a syntax error? No. Here Java's promotion scheme comes into play. If the variable type is greater than that of the value it is being assgined, that value is automatically promoted to the greater type. in this case 12 is converted to 12L. Another time this type of "promotion" takes place is when adding multiplying etc.. then everthing gets promoted to the greatest type possible.

but this is a syntax error:
code:

long bignum = 1200000000000;

since 1200000000000 is an int literal (but too big) .

Author:  rizzix [ Mon Sep 22, 2003 8:52 am ]
Post subject: 

F or f (case does not matter) is hexadecimal for 15.

in java to denote a hexadecimal (base 16) number you prefix it with 0x
and to denote a octal (base 8) number you prefix it with a 0
so these are identical:
code:

int num = 15;
int num = 0xF;
int num = 017;

Author:  krishon [ Mon Sep 22, 2003 4:56 pm ]
Post subject: 

ahhhh.ok i c


: