aspose file tools*
The moose likes Beginning Java and the fly likes Supidly basic question about primitives Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Supidly basic question about primitives" Watch "Supidly basic question about primitives" New topic
Author

Supidly basic question about primitives

Matt Player
Greenhorn

Joined: Jan 11, 2009
Posts: 11
Just to confirm my own understanding, I can't parse a primitive (e.g an "int") to another primitive type (e.g a "double") without first of all wrapping that "int" into an Integer? In fact, apart from arithmetic functions I cant do very much with an int (or any primitive) at all?

Cheers, Matt.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I think you are confused about "parsing", "wraping", and "casting". Also, if you haven't read this then you need to. It's a very well written explanation about primitives.


SCJA
~Currently preparing for SCJP6
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

>>I cant do very much with an int (or any primitive) at all?

No, you can't do much with primitives, except build Java objects, which essentially allows you to do everything.

Remember, pretty much every standard Java object can be broken down and flattened out into nothing but the basic Java primitive types from which it is made.

Different primitive types use memory, those magnetic, binary ones and zeroes, in different ways. Obviously, a boolean value uses a bit of memory to represent true or false, while a char uses a series of bits to represent a character. So, moving between primitive types that used memory for different purposes can create some weird results, as you would expect. Also, some primitive types just use more bits of memory than others, so again, moving between a large long and a small short just has data truncation problems.

In the code sample below, which all compiles, you can see a double is 'cast' into an int. Even though they're different types with different sizes, a cast can force the JVM to do what you want it to do. "Casting" is an important term to get familiar with in Java.

You'll also notice I take a String of numbers and turn it into an int. The method is called 'parseInt.' When we take a string and turn it into a number, that's referred to as 'parsing.' Just all part of the lingo that you've got to get familiar with if you want to bluff your way through a Java job interview. How's my bluffing doing?



*Output is:

10
12345


-Cameron McKenzie
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Matt Player wrote:Just to confirm my own understanding, I can't parse a primitive (e.g an "int") to another primitive type (e.g a "double") without first of all wrapping that "int" into an Integer?

Except boolean (char is actually numeric), all primitives can be "converted" into any other. You may need a cast like Cameron's code shows, but that's the most trouble you will get into.

The following conversions can be done without cast:
byte -> short -> int -> long -> float -> double
char -> int

For any other conversion, you will need an explicit cast.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10403
    
    8

No question is stupid..stupid is wondering about something and never bothering to find out the meaning or explanation


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
Consider Paul's rocket mass heater.
 
subject: Supidly basic question about primitives