Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive

 
Angie Jin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. This is my first time here. It is really great!! It has solved a lot of questions of mine.Now I have one more question. Please help me.
I am always confused about the assignments between primitives.I don't know when the casting is required.Here is my understanding and my questions. Please correct and answer me.
double = int Yes
int = double No
float = double No
double = float ??
int = long No
long = int Yes
How about the case in method call? e.g. Method(int x, int y) can I call it like Method(double, double)? How about the reverse?
Thank you very much.
 
Marine
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double = int Yes
int = double No
float = double No
double = float ??
int = long No
long = int Yes
Ok when you are doing assignment think in this terms. The smaller type will always fit into a larger one right ?
double = float yes you can.
also remember if you don't specify float f = 10.2f with an f it will be treated as a double literal.
Now about method call i didn't understand something. Do you mean if your method is designed to accept int values and you send it a double value what would happen ? It's a mistake to do this. And will result in an error. But if you pass to that method a short or a char they are going to be promoted to int values. I hope this helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic