This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. Assigning values Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Reg. Assigning values" Watch "Reg. Assigning values" New topic
Author

Reg. Assigning values

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327

When we assign a value to say a byte variable as below :
byte b = 3;
Here we see that the right side literal value should
be in the range of the byte i.e. ( -128 to 127 ) , so the
assignment is valid and code compiles and runs fine.
My doubt :
1. For the character variable assigining a int value should be in the
range of (0-65535).
Does it work the same way as mentioned above?
2. Is it that beyond the range of the variable to which we are assigning
it is considered as an int.?
( I am referring specifically to assigning to byte,short,char variables )
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Hi,
you can create char = 655; it is compile and run time success
thanks
daniel
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Angela Narain:

When we assign a value to say a byte variable as below :
byte b = 3;
Here we see that the right side literal value should
be in the range of the byte i.e. ( -128 to 127 ) , so the
assignment is valid and code compiles and runs fine.
My doubt :
1. For the character variable assigining a int value should be in the
range of (0-65535).
Does it work the same way as mentioned above?
2. Is it that beyond the range of the variable to which we are assigning
it is considered as an int.?
( I am referring specifically to assigning to byte,short,char variables )

Hi Angela,
Small example here.

HIH,
Guru's correct me!
chandrashekar
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi all,
Integral literals are treated as ints. When they appear in an assignment, for example, in <code>byte b = 3</code> the '3' is an int but Java applies assignment conversion and because '3' is within the range of a byte, the assignment succeeds.
The same is true for <code>char c = 655</code>.
See JLS §5.2
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It will not recognize values which are not in this range.

Hi Jane Griscti,
As we can see from the example above, I am sure that the code will compile and run when we use
char c = 655. [Narrowing conversion involving int literals] But when we are trying to print out the value, why is it not printing any value. It is printing a '?' . what does that mean. Can you please clarify on that.
thanks
Chandrashekar!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Hi all,
I got the info after reading it from Khalid's book. (pg
"Not all Unicode Characters can be represented in other encoding schemes. In that case, the ? character is usually used to denote any such character in the resulting output, during translation from Unicode.
thanks
Chandra!
------------------
Where there is a Will, there is a Way.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Reg. Assigning values
 
Similar Threads
can someone expalin the output ???
p169 K&B int assignment question.
Casting
Assinging Compile time long constant to byte fails
Doubt in Numeric conversion(Khalid Mughal)