• Post Reply Bookmark Topic Watch Topic
  • New Topic

Primitive data types - example for usage, please?  RSS feed

 
Alba Merula
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I'm green as one can be, and learning data types. Someone said that a byte is perfectly fine for storing age, for example, because the range is okay, and it won't take more memory than it absolutely must. Boolean ("true-false"), and String ("text", eg. names) are easy as well, but I was wondering:
when you think about the other types, what kind of info comes to your minds as to keep in them? (What is a "typical float" thing, or a "typical double", etc.?)

Thanks a lot in advance!

Merula
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
does that help you?

and Welcome to JavaRanch Alba
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just as an FYI - String is not a primitive data type.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alba

Avoid using the box types like Integer, Double etc unless the values held can be null. Too many people use the box types when primitives will suffice.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can go and spend £100 and buy enough memory to record the name and address of everybody on earth. And you are worrying about the 24 bits’ different between an int and a byte? Use ints for all your whole numbers, as long as they are small enough to fit into the 32 bits. Not like thisThe best way to teach people about primitives in Java™ is like this
  • 1: An int is a whole number.
  • 2: A double is a floating-point number.
  • 3: A char is an unsigned whole number which maps to the Unicode character set.
  • 4: A boolean can be true or false.
  • 5: There are four other primitive types.
  • You can learn about float short byte and long later.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    James Boswell wrote:Avoid using the box types like Integer, Double etc unless the values held can be null. Too many people use the box types when primitives will suffice.

    That's a bit of a blanket statement. If the values are held in a collection, you have no choice but to use the boxed type. What is probably worth doing is to avoid situations where you're doing a lot of boxing/unboxing, because it can chew up a lot of time and space (and it's not always obvious that it's happening).

    ie, once you have an Integer, keep it an Integer.

    Winston
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alba Merula wrote:when you think about the other types, what kind of info comes to your minds as to keep in them? (What is a "typical float" thing, or a "typical double", etc.?)

    Well personally, I can't remember the last time I used float, since double does exactly the same thing and is more accurate and flexible. It's also the Java "default": if I write 2.0, that constant will be made a double, not a float.

    Tip: One thing you should avoid is using double/float for currency (ie, fixed-point) values. Use BigDecimal instead.

    And then of course, there's char...

    Winston
     
    James Boswell
    Bartender
    Posts: 1051
    5
    Chrome Eclipse IDE Hibernate
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston

    That's exactly the point I am making. Because the code compiles and runs as expected (with a load of boxing/unboxing is going on behind the scenes), I have seen plenty of cases where primitives should have been used instead.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    James Boswell wrote:That's exactly the point I am making. Because the code compiles and runs as expected (with a load of boxing/unboxing is going on behind the scenes), I have seen plenty of cases where primitives should have been used instead.

    Oh OK; although that's not quite what you said.

    My basic rule of thumb is that I always try to box/unbox explicitly.

    Small values for boxed types are cached, so you won't actually be taking up much more space by using them; the real issue is autoboxing, eg
    if (someCondition)
    where 'someCondition' is a Boolean. It won't matter in single-use cases, but put it in a loop that's run a few million times and you may have a performance issue.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 56578
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A float should only be used where you definitely can cope with the reduced precision. For example, Color has a constructor which takes floats.Since you are trying to get 256 different values between 0f and 1f (inclusive) in each float, who needs 15 sig fig of precision?
    Or some Maps have constructors with a load factor, which is a float, because you aren’t going to notice the difference between 0.75f and 0.7fd of the size of your Map until its size is well into the millions.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!