This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hey guys i'm new at all this and this question i'm about to ask sounds really silly, but any help would be great, Iv'e been giving this assignment but i'm stuck on the last segment of code I also have to print a line of somthing the trouble is i'm not really 100% i know what this programme is doing thanks.
This Program is to understand the primitive types, type cast, which type can cast implicitly, which type need explicit casting.
int can be placed to double, which is implicit cast.
double no need of explaintion,
y = (int) d, which truncates the fractions
z=(int)3.14, here 3.14 means double constant(so next line is z=3.14f which tells float constant), so if you explicitly cast to int, which make z= 3.00
defining constant for long datatype
byte constant should have value less then 255
if you add 2 byte variable result will be in int so we need explicit cast to byte(you may loose data)
But i am not able to understand what is this
int name = "rich";
because we can not assign String constant to int, and its not char constant also.
Joined: Sep 17, 2011
thank you for your help so far the last segment was meant to read:
String name = "rich";
x = name;
x = (int) name;
the assingment demonstrates declaring and assigning variable and what can and can't be done with primitaive data types, it then goes on to say which line of code don't compile which i can see now thanks to your help but i'm still stuck on the last segment and wher and what i'm outputting thanks
Did you get the line id = 43251234153313241; to compile? That number occupies too much memory for an int, and the compiler will only accept it as a long with the final L as you have in the next line.
You can read about widening and narrowing conversions in the Java™ Language Specification (JLS), and in the next section you find out about an even more confusing type of conversion. But the JLS is by no means easy to read.
The compiler will accept the int value 10 for a byte because the 10 is a compile-time constant and the compiler can tell it is within the correct range for a byte.
In the case of casting a double or float to an integer type, you see under "narrowing" conversions that rounding-towards-zero (also called truncation, as you have already been told) occurs.
And I don't think you will get int name = "rich"; to compile however hard you try.