File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Primitive data type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Primitive data type" Watch "Primitive data type" New topic

Primitive data type

stuart harper

Joined: Sep 17, 2011
Posts: 2
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.

Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
I'm not sure either what this segment of code is supposed to be doing because you can't assign a string to int and you can't convert a string to int.
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

Hi stuart harper
Welcome to Ranch

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.

stuart harper

Joined: Sep 17, 2011
Posts: 2
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
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46412
Welcome again

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.
I agree. Here's the link:
subject: Primitive data type
It's not a secret anymore!