• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Primitive data type

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

1:
int can be placed to double, which is implicit cast.

2:
double no need of explaintion,
y = (int) d, which truncates the fractions

3:
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

4:
defining constant for long datatype

5:
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic