File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Primitive return type in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Primitive return type in java" Watch "Primitive return type in java" New topic
Author

Primitive return type in java

Kevin Liauw
Greenhorn

Joined: Aug 28, 2007
Posts: 8
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 ]

Confucius once said:<br />"by three way we may obtain wisdom:<br />1. Reflection , which is the noblest<br />2. Imitation , which is the easiest<br />3. Experience , which is the bitterest "
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

"Little One",

Please read your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Venkat Sadasivam
Ranch Hand

Joined: May 10, 2008
Posts: 139
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


“Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”<br>
-Martin Fowler
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: Aug 28, 2007
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38334
    
  23
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Primitive return type in java