Sie sind auf Seite 1von 3

Notes - Literals and Data Types Constant values are values that do not change.

Sometimes they are called literals. Integer (int) constants are comprised of whole numbers. They may be positive or negative. Examples include: 123, 1, 9001, 0, -12, -200, 22 Decimal (double) constants are decimal numbers. They may be positive or negative. Examples include: 1.0, 22.33, -9.0, 0.0, 123.45, -89.0 Character (char) constants are comprised of only one letter, number, or symbol which is enclosed in single quotes. Examples include: a, b, Z, 1, $, !, -, <, + Boolean (boolean) constants are either true or false. String (String) constants are comprised of a series of characters and/or spaces enclosed it double quotes. Examples include: Hello, My name is Ms. Montiero, 1234, #!$@!, asdfASDF

Data Types allow Java to know what kind of information it will be handling. Java has 8 data types built in these are called primitive data types. These include: int, double, char, boolean, short, long, byte, and float. The following data types store whole numbers:
type Size name bytes bits Minimum byte 1 8 -128 short 2 16 -32,768 Range maximum +127 +32,767 +2,147,483,647


4 8

32 64


-9,223,372,036,854,775,808 +9,223,372,036,854,775,807

In this class, we will always use an int to store whole numbers. The following data types store decimal numbers:
type Size Range Precision name bytes bits approximate in decimal digits float 4 32 +/- 3.4 * 1038 6-7 double 8 64 +/- 1.8 * 10308 15

In this class, we will always use a double to store decimal numbers. The data type used to store a character literal is char. The data type used to store a boolean literal is boolean.

The data type used to store a String literal is String. String, however, is not a built-in, primitive data type. It is reference data type. One of the main differences between a primitive data type and a reference data type is the way that they are stored in memory. Primitive data types are stored directly in memory, whereas reference data types are stored as pointers to a location in memory where the data is actually stored. The reference types are associated with a class which defines their behavior. For example, there is a Java class called String which defines what it means for something to be classified as a String. Strings have many, many operations and special features because they are not built in. We will cover these special features later. Defining your own reference type - It is possible for you to define your own reference type. To do this, you need to write a class which describes this type, i.e. the kind of properties a value of this type has the kind of operations values of this type can be involved in We will learn how to create our own reference data types later on. Wrappers are reference data types that wrap around primitive data types. Each of the primitive data types has a wrapper class: Integer for int, Double for double, Boolean for boolean, and Character for char. We will learn more about these later. But, for now it is important to know that these wrapper classes exist. Be very careful when typing in these data types because Java is case sensitive, and Double is very different than double, etc. Comprehension Questions: 1. 2. 3. 4. 5. 6. 7. What is a constant? Give an example of each of the five literal types. What is the difference between a character constant and a String constant? How many primitive data types does Java have? What are they? What is the correlation between bits and bytes? How many bits is an int comprised of? What about a double? Using deductive reasoning, do you think you can store an int in a double? What about storing a double in an int? 8. What are the possible values for a boolean? 9. What is a difference between a primitive data type and a reference data type? 10. What is one reference data type that we are now being introduced to?

Name ___________________ Period ______ Worksheet Understanding Literals and Data Types Properly identify the following constants or indicate that they are invalid: 1. ______________ 2. ______________ 3. ______________ 4. ______________ 5. ______________ 6. ______________ 7. ______________ 8. ______________ 9. ______________ 10. ______________ 2536 bob W true -32.2 -45 5265453 1 5.0005 m

Indicate which data type should be used for the following values: 11. ______________ 12. ______________ 13. ______________ 14. ______________ 15. ______________ -2147483000 Flower ! 0.01280128 false

16. How many bits are in 4 bytes? __________ 17. What is the largest number an int can hold? ______________________ 18. What data types are used to store decimal numbers? ___________________________ 19. What is another word for constant value? __________________ 20. What are built in data types called? ____________________