• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

doubt in primitive assignment...

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose it will not give compiler error with the range of (0 to 127).
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes ankit you are right.
if i am giving positive values in the range 0 to 127 it is compiling fine.

thanks a lot..
 
Greenhorn
Posts: 13
Netbeans IDE Opera Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explicit casting will force it to compile, like this:


Trying to print it will give (on my machine) a "~".
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic