• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!