• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification on primitive return type

 
Dipan Chokshi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone
From the K&B book page # 124, they said "In a method with a primitive return type, you can return any value or variable
that can be implicitly converted to the declared return type. Example


My question is what are the implicitly convertible primitive type? I tried below

public boolean foo(): return c(char) // error
public byte foo(): return c(char) // error
public short foo(): return c(char) // ok
public long foo(): return c(char) // ok
public float foo(): return c(char) // ok
public double foo(): return c(char) // ok

So for the exam we need to check all combination or there is better way to remember them?

Thanks in advance.
 
Krishna Srinivasan
Ranch Hand
Posts: 1876
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dipan,

There are two types of conversions: widening and narrowing. What you are telling is widening conversion which don't require any explicit conversion.

So, what is a widening conversion?when dealing with primitives, a widening conversion occurs when you convert one type, A, to another type, B, when type B has a larger (or wider) range than A. This is the type of cast that occurs when you cast a byte to an int.

From a byte to a short, an int, a long, a float, or a double
From a short to an int, a long, a float, or a double
From a char to an int, a long, a float, or a double
From an int to a long, a float, or a double
From a long to a float or a double
From a float to a double

 
Suketu Patel
Greenhorn
Posts: 23
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dipan,

When you do any arithmetic calculations on type byte, short or char they implicitly converted to int, Try this

byte b1 = 100;
byte b2 = 10;

byte b3 = b1 + b3 // Not compile

But if you assign it to int like this

int i = b1 + b2 // This will compile

because whenever you do any arithmetic operations on these types or just even just simply assign this byte, short or char to int variable Java will implicitly convert it to int and you do not need to convert it to explicitly using (int).

I have checked the code you provided with each type... It will automatically converted to int, long, float or double but not converted to byte or short. Obviously you cant convert any numeric datatype to Boolean.

Please check once more with short type it will not converted automatically.



Other than this just keep in mind that any low level datatype can be implicitly converted to higher level datatype... Check Krishna Srinivasan post.
 
Dipan Chokshi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishna Srinivasan wrote:
From a byte to a short, an int, a long, a float, or a double
From a short to an int, a long, a float, or a double
From a char to an int, a long, a float, or a double
From an int to a long, a float, or a double
From a long to a float or a double
From a float to a double

Now i understand. Data range is good point to remember. Thanks!
 
Dipan Chokshi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S B Patel wrote:
I have checked the code you provided with each type... It will automatically converted to int, long, float or double but not converted to byte or short. Obviously you cant convert any numeric datatype to Boolean.

You are right. Its not converting to short. Thanks!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dipan Chokshi wrote:So for the exam we need to check all combination or there is better way to remember them?

It is all about ranges and some common sense.

The boolean data type has only two possible (literal) values: true and false. You can't assign any other primitive data type to a boolean (not even with an explicit cast).

All other primitive data types are numeric data types (even char). These data types can be divided into two main groups: integral number (byte, char, short, int, and long) and floating-point numbers (float and double). All of these numeric data types are signed data types (range from -X to +Y), except for char which is an unsigned data type (range from 0 to 65535).

When executing calculations Java uses a "standard" datatype. When you are calculating with integers, the result of the calculation will be an int. If you are doing some calculation with floating-points (decimals), the result will be a double. More detailed information can be found in this thread.

Some numeric data types will be implicitly converted to another numeric data type (depending on the range of the numeric data type). And if it can't be implicitly converted, you can always add an explicit cast. Because char is unsigned and all other numeric data types are signed, you always need an explicit cast to convert any numeric data type to char. In this thread you'll find tons of code examples about working with byte (and other numeric) variables. And this thread explains why a long (64-bit) fits into a float (32-bit).

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S B Patel wrote:int i = b1 + b2 // This will compile

Are you sure? Because I get a compiler error because the semicolon at the end of the statement is missing

S B Patel wrote:byte b3 = b1 + b3 // Not compile

This doesn't compile for several other reasons than the one mentioned: missing semicolon at the end of the statement and b3 can not be used in the right hand side of the assignment.

What if b1 and b2 were made final. Would the code snippet still fail to compile with a compiler error?And what about this code snippet?And this one?

S B Patel wrote:It will automatically converted to int, long, float or double but not converted to byte or short. Obviously you cant convert any numeric datatype to Boolean.

Be careful! Java is case-sensitive, so there is a difference between boolean (the primitive data type) and Boolean (the wrapper class of the boolean data type). But in this case it makes no difference because you can't convert any numeric datatype to boolean or java.lang.Boolean.

Hope it helps!
Kind regards,
Roel
 
Suketu Patel
Greenhorn
Posts: 23
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel

S B Patel
 
Dipan Chokshi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
When executing calculations Java uses a "standard" datatype. When you are calculating with integers, the result of the calculation will be an int. If you are doing some calculation with floating-points (decimals), the result will be a double. More detailed information can be found in this thread.

Some numeric data types will be implicitly converted to another numeric data type (depending on the range of the numeric data type). And if it can't be implicitly converted, you can always add an explicit cast. Because char is unsigned and all other numeric data types are signed, you always need an explicit cast to convert any numeric data type to char. In this thread you'll find tons of code examples about working with byte (and other numeric) variables. And this thread explains why a long (64-bit) fits into a float (32-bit).

Hope it helps!
Kind regards,
Roel


I appreciate your time , reference and example. Thanks Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic