File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in primitive assignment... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in primitive assignment..." Watch "doubt in primitive assignment..." New topic
Author

doubt in primitive assignment...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362


my doubt is that:
1)eventhough i declare d as final why compilation error is coming in line1.

2)why it is giving compilation error in line 3.


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

1) it is giving error as you are assigning byte to char. Although char has a larger size than byte, but it cannot store negative values while byte can. So if byte has a negative value, then assigning it to char will result in a loss of the original value.

2) it is giving compilation error because when you multiply any data type below integer, the result is always an int.

eg



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
i agree with your explanation about second doubt.
in case of first doubt:
i declared byte variable as final so i think it is a compile time constant so no casting is required in line1.

correct me if i am wrong...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

but my dear the compile time constant has a negative value. So the statement will look like this to the compiler

char ch = -12; //not allowed

as you can see that you are trying to assign a negative value to a char data type which is not allowed...
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
I suppose it will not give compiler error with the range of (0 to 127).


Thanks,<br />Subhasish
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yes ankit you are right.
if i am giving positive values in the range 0 to 127 it is compiling fine.

thanks a lot..
Vaes Bart
Greenhorn

Joined: Oct 28, 2008
Posts: 13

Explicit casting will force it to compile, like this:


Trying to print it will give (on my machine) a "~".
 
 
subject: doubt in primitive assignment...