Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

The Java Lesson 3: Identifiers and primitive data types. Page 2

JavaFAQ Home » Java Lessons by Jon Huhtala Go to all tips in Java Lessons by Jon Huhtala


Bookmark and Share
All Java Lessons contents page | Java Lesson 1 | Java Lesson 2 | Java Lesson 3 | Java Lesson 4 | Java Lesson 5 | Java Lesson 6 | Java Lesson 7 | Java Lesson 8 | Java Lesson 9 | Java Lesson 10 | Java Lesson 11 | Java Lesson 12 | Java Lesson 13 | Java Lesson 14 | Java Lesson 15 | Java Lesson 16 | Java Lesson 17 | Java Lesson 18 | Java Lesson 19 | Java Lesson 20 | Java Lesson 21 | Java Lesson 22 | Java Lesson 23 | Java Lesson 24 | Java Lesson 25 | Java Lesson 26 | Java Lesson 27 | Java Lesson 28 | Java Lesson 29 | Java Lesson 30 | Java Lesson 31 | Java Lesson 32 | Java Lesson 33 | Java Lesson 34 | Java Lesson 35 | Java Lesson 36 | Java Lesson 37 | Java Lesson 38 | Java Lesson 39 | Java Lesson 40 | Java Lesson 41 | Java Lesson 42 | Java Lesson 43 | Java Lesson 44 | Java Lesson 45 | Java Lesson 46

Primitive data types

1. Uses of identifiers in Java | 2. Primitive data types in Java

  • Are part of the language (built-in)

  • Represent different kinds and sizes of data

  • Are always the same regardless of the platform

Type

Bit Size Usage Signed Range of Values
boolean 1 true or false no true or false
char 16 Unicode character no 0 to 216 - 1
byte 8 very small integer number yes -27 to 27 - 1
short 16 small integer number yes -215 to 215 - 1
int 32 integer number yes -231 to 231 - 1
long 64 large integer number yes -263 to 263 - 1
float 32 single precision decimal number yes + 3.4e38 to + 1.4e-45
double 64 double precision decimal number yes + 1.8e308 to + 4.9e-324

What about strings?

  • There is no primitive data type in Java to represent strings (such as "Java is fun")

  • Several packaged classes provide for string manipulation and will be covered later

What is Unicode?

  • An international (ISO) standard. For detailed information, go to http://www.unicode.org/

  • Provides a unique bit pattern to represent every character or symbol in all the world's character sets

  • Each character has an equivalent, positive integer value corresponding to the bit pattern

Integer data format

Uses two's-complement notation. To determine the bit pattern of an integer value:

  1. If the number is greater than or equal to zero: convert it to binary

  2. If the number is negative: take its absolute value, subtract one, convert the result to binary, and reverse all the bits

The left-most (high-order) bit is the sign bit with 0 indicating positive and 1 indicating negative.

Here are some examples using a 16 bit (short) data format:

Action Base 10 Binary Hexadecimal
Integer to be represented -5
1. Absolute value 5
2. Minus 1 4 0000 0000 0000 0100 0004
3. Reverse bits 1111 1111 1111 1011 FFFB
Integer to be represented 67 0000 0000 0100 0011 0043
Integer to be represented -44
1. Absolute value 44
2. Minus 1 43 0000 0000 0010 1011 002B
3. Reverse bits 1111 1111 1101 0100 FFD4

For simplicity, hexadecimal (base 16) notation is used to represent binary numbers. Each hexadecimal digit represents four binary digits as follows:

Hexadecimal Digit Binary Equivalent
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

Floating-point (decimal) types

  • Conform to the IEEE 754 specification for representing real numbers in a binary format. For the certification exam, you will not need to know the internal representation or the range of maximum and minimum values.

  • double allows up for twice as many significant digits as float and should be used when extremely large numbers or greater precision are required

More information

Java ranch has two great tutorials relating to topics covered in this lesson. Be sure to read the following "campfire stories":

Review questions

  1. Which of the following are valid Java identifiers? (choose three)

  1. _$amount_due_2002

  2. 2002_sales

  3. class

  4. String

  5. FINANCE_CHARGE

  1. Which primitive data type may be used to store the integer value -473 ? (choose three)

  1. char

  2. byte

  3. short

  4. int

  5. long

  1. Which of the following hexadecimal values might represent the contents of a negative, int variable? (choose two)

  1. 1AF21110

  2. C8253B7C

  3. F63A

  4. FFFFFFFFFFFFFFFF

  5. 90001C58

  1. In hexadecimal notation, what is the value of -100 as a byte?

  1. FF9C

  2. 9C

  3. FF9B

  4. 9B

  5. a byte is too small an area to hold the value


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy