Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive return type in java

 
Kevin Liauw
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gretings dear ranchers , i'm sorry if this type of questions already exists



i'm aware that if the return type of our method is primitive value than we can return any primitive value that can implicitly converted to primitive type that was declared on method signature , now my question is

1. can anyone please teach me the compatibility of each of primitive value in Java ? all i know is char and int

Thank you

[ August 16, 2008: Message edited by: Little One ]

[ August 16, 2008: Message edited by: Little One ]

[ August 16, 2008: Message edited by: Kevin ]
[ August 16, 2008: Message edited by: Kevin Liauw ]
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Little One",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Venkat Sadasivam
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You find compatibility based on number of bits.

As per your example char is 16 bit and integer is 32 bit, hence implicit casting applied. If it is other case explicit casting is required.

The below link gives details of bit size for all primitive types.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Little One:
Gretings dear ranchers , i'm sorry if this type of questions already exists



i'm aware that if the return type of our method is primitive value than we can return any primitive value that can implicitly converted to primitive type that was declared on method signature , now my question is

1. can anyone please teach me the compatibility of each of primitive value in Java ? all i know is char and int

Thank you

We need to be careful about the choice of words. A char and an int are not "compatible". They are different primitive types and require different amounts of memory for storage. There are however, as you mentioned, some rules for conversions and promotions of primitives, some of which happen implicitly.

Chapter 5 of the Java Language Specification (JLS) talks about these rules. I am not sure of, off of the top of my head, a tutorial or such on the subject.
[ August 16, 2008: Message edited by: Mark Vedder ]
 
Kevin Liauw
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the answers everyone

So if the return primitive type can be implicitly converted to primitive type that was declared in the method signature that it's no problem, example

declared : byte
legal return value: byte

declared : short
legal return value : short , byte

declared : int
legal return value : short , byte , int , char

declared : long
legal return value : byte , short , int , char , long

declared : float
legal return value : byte , short , int , char , long , float

declared : double
legal return value : byte , short , int , char , long , float , double

declared : char
legal return value : char , number literal max(65535) min (0)

right ? correct me if i'm wrong

Thank you

@Rob
Yikes ?! i'm sorry i didn't know the naming policy , i'll have mine changed ASAP

[ August 16, 2008: Message edited by: Kevin ]
[ August 16, 2008: Message edited by: Kevin Liauw ]
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I shall send you to the Java Language Specification �5.1; read the different types of conversion, and which can be done with and without casts. If you have a primitive return type and return something which can be converted to that type without casting, it will work. There is a list there rather similar to what you have provided yourself.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic