• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

parameters

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the following code we can't declare short b = 33 or byte b = 33 inspite of those two we can use other variables, why can't we use those two?

class A{
public static void main(String ar[]){
byte a = 5;
short b = 33;
short c = b % a;
System.out.println(c);
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Hasitha_Randika,"

I need you to take another look at the JavaRanch Naming Policy, and note that a space must separate your first and last name. Replace the underscore with a space, and you'll be fine. You can edit your display name here. Thanks!

In your code, the byte and short variables 'a' and 'b' can be assigned int literal values because the values are compile-time constants that fit within the variable types.

However, the modulus operation promotes these to type int and returns an int as the result. Because this expression is not a compile-time constant, it cannot be assigned to a variable of type short without an explicit cast. For example...
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Weber,the way you explained as Compile-time constants is good and knowledge fetching

Thanks
Niranjan
 
Onion rings are vegetable donuts. Taste this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic