File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Supidly basic question about primitives

 
Matt Player
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No question is stupid..stupid is wondering about something and never bothering to find out the meaning or explanation
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic