• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

short assignment.

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

From the following class, when i assign "4" into short it's accepting the value. but if i pass to some method it's gives compilation error.
becuase same value become int type.pls explain to me why.

Thanks, Raghu.K

class Test
{
static void Short(short a){
System.out.println(" short a "+ a);
}

public static void main(String args[])
{
short s = 4;
System.out.println(" short s "+ s);
Short(4);

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

I have made small change in your program at line 9.compare your code with
above code and understand the difference.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, your method names are nutty. Short is the name of a pretty famous wrapper class. Wears gold chains and baggy pants - surprised you never heard of him.

Anyways, any time you use a number in code, the JVM assumes it is an int.

4 <-- that's in int. So is 5. So is 9.

Any time you use an integer, the compiler sees it as an int.

Any time you use a floating type number, the JVM sees it as a double. Similar idea.

So, your code will run if you either cast 4 into a short, or, just pass in s, which is typed as a short - the compiler will never argue with a typed variable.

class Test
{

/*That method name is killing me!!! upper case S.
Does the method think it's a class or a constructor?
Namings are important.
*/

static void Short(short a){
System.out.println(" short a "+ a);
}

public static void main(String args[])
{
short s = 4;
System.out.println(" short s "+ s);
//Test.Short(4); /*4 is an int, not a short*/
Test.Short((short)4); /*this will work, casting the 4 into a short*/
Test.Short(s); /*this will work, passing the s, which is typed as a short*/
/*I added Test in front of short.
Fully qualify static methods, even when you don't have to.*/



}

}
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you code deals with implicit narrowing of data type int(4) to short(in the signature of method). Java doesn't allow this in case of method call. In method call only widening is allowed therefore compiler is complaining for that.
[ August 30, 2006: Message edited by: Vaibhav Chauhan ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why "byte a = 1;" compiles, but "Byte c = new Byte(1);" error out?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since compiler sees untyped variables as int better to cast before hand.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic