This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

short assignment.  RSS feed

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since compiler sees untyped variables as int better to cast before hand.
 
Your buns are mine! But you can have this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!