KEMBAR78
Computer Programming Java Data Types.pptx
JAVA DATA TYPES
DATA TYPES ARE DIVIDED INTO
TWO GROUPS:
•Primitive data types – includes
byte, short, int, long, float,
double, Boolean and char
•Non-primitive data types – such
as String, Arrays and Classes
PRIMITIVE DATA TYPES
•A primitive data type specifies
the type of a variable and the
kind of values it can hold.
PRIMITIVE DATA TYPES
• byte - Stores whole numbers from -128 to 127
• short - Stores whole numbers from -32,768 to
32,767
• int - Stores whole numbers from -2,147,483,648 to
2,147,483,647
• long - Stores whole numbers from -
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
PRIMITIVE DATA TYPES
• float - Stores fractional numbers. Sufficient
for storing 6 to 7 decimal digits
• double - Stores fractional numbers. Sufficient
for storing 15 to 16 decimal digits
• boolean - Stores true or false values
• char - Stores a single character/letter or
ASCII values
JAVA NUMBERS
Primitive number types are divided into two groups:
• Integer types stores whole numbers, positive or
negative (such as 123 or -456), without decimals. Valid
types are byte, short, int and long.
• Floating point types represents numbers with a
fractional part, containing one or more decimals.
There are two types: float and double.
INTEGER TYPES
Byte
• The byte data type can store whole numbers from -128 to
127. This can be used instead of int or other integer types
to save memory when you are certain that the value will be
within -128 and 127:
INTEGER TYPES
Short
• The short data type can store whole
numbers from -32768 to 32767:
INTEGER TYPES
Int
• The int data type can store whole numbers from -
2147483648 to 2147483647. In general,
the int data type is the preferred data type when
we create variables with a numeric value.
INTEGER TYPES
Long
• The long data type can store whole numbers
from -9223372036854775808 to
9223372036854775807. This is used when int is
not large enough to store the value. Note that
you should end the value with an "L":
INTEGER TYPES
Floating Point Types
• You should use a floating point type whenever
you need a number with a decimal, such as 9.99
or 3.14515.
• The float and double data types can store
fractional numbers. Note that you should end
the value with an "f" for floats and "d" for
doubles:
INTEGER TYPES
INTEGER TYPES
Use float or double?
• The precision of a floating point value
indicates how many digits the value can have
after the decimal point. The precision
of float is only six or seven decimal digits,
while double variables have a precision of
about 16 digits. Therefore, it is safer to
use double for most calculations.
INTEGER TYPES
Scientific Numbers
• A floating point number can also be a
scientific number with an "e" to indicate
the power of 10:
JAVA BOOLEAN DATA TYPES
Boolean Types
• Very often in programming, you will need a
data type that can only have one of two
values, like:
• YES / NO
• ON / OFF
• TRUE / FALSE
JAVA BOOLEAN DATA TYPES
Boolean values are mostly used for conditional testing.
JAVA CHARACTERS
Characters
• The char data type is used to store
a single character.
• The character must be surrounded by single
quotes, like 'A' or 'c':
JAVA CHARACTERS
Alternatively, if you are familiar with ASCII values, you
can use those to display certain characters:
ASCII PRINTABLE CHARACTERS
ASCII PRINTABLE CHARACTERS
ASCII PRINTABLE CHARACTERS
ASCII PRINTABLE CHARACTERS
JAVA CHARACTERS
Strings
• The String data type is used to store a
sequence of characters (text). String values
must be surrounded by double quotes:
JAVA CHARACTERS
• The String type is so much used and
integrated in Java, that some call it "the
special ninth type".
• A String in Java is actually a non-
primitive data type, because it refers to an
object. The String object has methods that
are used to perform certain operations on
strings.
REAL-LIFE EXAMPLE
NON-PRIMITIVE DATA TYPES
• Non-primitive data types are
called reference types because they refer to
objects.
The main differences between primitive
and non-primitive data types are:
•Primitive types in Java are predefined and
built into the language, while non-primitive
types are created by the programmer (except
for String).
NON-PRIMITIVE DATA TYPES
•Non-primitive types can be used to call methods
to perform certain operations, whereas primitive
types cannot.
•Primitive types start with a lowercase letter
(like int), while non-primitive types typically
starts with an uppercase letter (like String).
•Primitive types always hold a value, whereas
non-primitive types can be null.
Examples of non-primitive types
are Strings, Arrays, Classes etc.
1. What is an int in Java?
A. A data type representing integers.
B. A data type representing strings.
C. A data type representing decimals.
2. Add the correct data type for
the following variables:
3. byte, short, int, long, float, double,
boolean and char are called:
4. Integer types are used to
store:
A. Floating point numbers
B. Decimals
C. Whole numbers
5.Which is NOT a valid integer type?
A. byte
B. size
C. short
D. double
E. int
6. True or False:
A float variable has greater
precision than double.
7. What is the correct letter
suffix to use for float values?
A. a
B. b
C. c
D. d
E. f
8. What is the correct syntax for
declaring a boolean variable in Java?
A. boolean isJavaFun = ‘true’;
B. boolean isJavaFun = true;
C. bool isJavaFun = true;
D. boolean isJavaFun = 1;
9. The boolean data type can
take two values, which are:
A. true and false
B. truth and falsy
C. 1 and 0
10. What is the output of the following
code?
boolean isFishTasty = false;
System.out.println(isFishTasty);
A.true
B.false
C.1
D.0
11. Which data type is used to
store a single character?
A. char
B. String
C. int
D. single
12. The character inside
a char variable must be
surrounded by:
A. Single quotes
B. Double quotes
C. Question marks
13. What is the output of the following
code?
char myLetter = 'B';
System.out.println(myLetter);
A.‘B’
B. B
C. myLetter
D. b
14. To store more than one
character (a text), you must
use:
A. The string data type.
B. The String data type.
C. The text data type.
D. The char data type.
15. What is the output of the following
code?
String name = "John Doe";
System.out.println(name);
A. John Doe
B. name
Computer Programming Java Data Types.pptx

Computer Programming Java Data Types.pptx

  • 1.
  • 2.
    DATA TYPES AREDIVIDED INTO TWO GROUPS: •Primitive data types – includes byte, short, int, long, float, double, Boolean and char •Non-primitive data types – such as String, Arrays and Classes
  • 3.
    PRIMITIVE DATA TYPES •Aprimitive data type specifies the type of a variable and the kind of values it can hold.
  • 4.
    PRIMITIVE DATA TYPES •byte - Stores whole numbers from -128 to 127 • short - Stores whole numbers from -32,768 to 32,767 • int - Stores whole numbers from -2,147,483,648 to 2,147,483,647 • long - Stores whole numbers from - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • 5.
    PRIMITIVE DATA TYPES •float - Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits • double - Stores fractional numbers. Sufficient for storing 15 to 16 decimal digits • boolean - Stores true or false values • char - Stores a single character/letter or ASCII values
  • 6.
    JAVA NUMBERS Primitive numbertypes are divided into two groups: • Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. • Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types: float and double.
  • 7.
    INTEGER TYPES Byte • Thebyte data type can store whole numbers from -128 to 127. This can be used instead of int or other integer types to save memory when you are certain that the value will be within -128 and 127:
  • 8.
    INTEGER TYPES Short • Theshort data type can store whole numbers from -32768 to 32767:
  • 9.
    INTEGER TYPES Int • Theint data type can store whole numbers from - 2147483648 to 2147483647. In general, the int data type is the preferred data type when we create variables with a numeric value.
  • 10.
    INTEGER TYPES Long • Thelong data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L":
  • 11.
    INTEGER TYPES Floating PointTypes • You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515. • The float and double data types can store fractional numbers. Note that you should end the value with an "f" for floats and "d" for doubles:
  • 12.
  • 13.
    INTEGER TYPES Use floator double? • The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 16 digits. Therefore, it is safer to use double for most calculations.
  • 14.
    INTEGER TYPES Scientific Numbers •A floating point number can also be a scientific number with an "e" to indicate the power of 10:
  • 15.
    JAVA BOOLEAN DATATYPES Boolean Types • Very often in programming, you will need a data type that can only have one of two values, like: • YES / NO • ON / OFF • TRUE / FALSE
  • 16.
    JAVA BOOLEAN DATATYPES Boolean values are mostly used for conditional testing.
  • 17.
    JAVA CHARACTERS Characters • Thechar data type is used to store a single character. • The character must be surrounded by single quotes, like 'A' or 'c':
  • 18.
    JAVA CHARACTERS Alternatively, ifyou are familiar with ASCII values, you can use those to display certain characters:
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
    JAVA CHARACTERS Strings • TheString data type is used to store a sequence of characters (text). String values must be surrounded by double quotes:
  • 24.
    JAVA CHARACTERS • TheString type is so much used and integrated in Java, that some call it "the special ninth type". • A String in Java is actually a non- primitive data type, because it refers to an object. The String object has methods that are used to perform certain operations on strings.
  • 25.
  • 26.
    NON-PRIMITIVE DATA TYPES •Non-primitive data types are called reference types because they refer to objects. The main differences between primitive and non-primitive data types are: •Primitive types in Java are predefined and built into the language, while non-primitive types are created by the programmer (except for String).
  • 27.
    NON-PRIMITIVE DATA TYPES •Non-primitivetypes can be used to call methods to perform certain operations, whereas primitive types cannot. •Primitive types start with a lowercase letter (like int), while non-primitive types typically starts with an uppercase letter (like String). •Primitive types always hold a value, whereas non-primitive types can be null. Examples of non-primitive types are Strings, Arrays, Classes etc.
  • 29.
    1. What isan int in Java? A. A data type representing integers. B. A data type representing strings. C. A data type representing decimals.
  • 30.
    2. Add thecorrect data type for the following variables:
  • 31.
    3. byte, short,int, long, float, double, boolean and char are called:
  • 32.
    4. Integer typesare used to store: A. Floating point numbers B. Decimals C. Whole numbers
  • 33.
    5.Which is NOTa valid integer type? A. byte B. size C. short D. double E. int
  • 34.
    6. True orFalse: A float variable has greater precision than double.
  • 35.
    7. What isthe correct letter suffix to use for float values? A. a B. b C. c D. d E. f
  • 36.
    8. What isthe correct syntax for declaring a boolean variable in Java? A. boolean isJavaFun = ‘true’; B. boolean isJavaFun = true; C. bool isJavaFun = true; D. boolean isJavaFun = 1;
  • 37.
    9. The booleandata type can take two values, which are: A. true and false B. truth and falsy C. 1 and 0
  • 38.
    10. What isthe output of the following code? boolean isFishTasty = false; System.out.println(isFishTasty); A.true B.false C.1 D.0
  • 39.
    11. Which datatype is used to store a single character? A. char B. String C. int D. single
  • 40.
    12. The characterinside a char variable must be surrounded by: A. Single quotes B. Double quotes C. Question marks
  • 41.
    13. What isthe output of the following code? char myLetter = 'B'; System.out.println(myLetter); A.‘B’ B. B C. myLetter D. b
  • 42.
    14. To storemore than one character (a text), you must use: A. The string data type. B. The String data type. C. The text data type. D. The char data type.
  • 43.
    15. What isthe output of the following code? String name = "John Doe"; System.out.println(name); A. John Doe B. name

Editor's Notes

  • #2 As explained in the previous chapter, a variable in Java must be a specified data type: int (integer – whole number), Float (floating point number), char (character), Boolean, String
  • #3 There are eight primitive data types in Java:
  • #6 Even though there are many numeric types in Java, the most used for numbers are int (for whole numbers) and double (for floating point numbers). 
  • #11 float - Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits double - Stores fractional numbers. Sufficient for storing 15 to 16 decimal digits
  • #25 Here's a real-life example of using different data types, to calculate and output the total cost of a number of items:
  • #29 A.
  • #30 Int, float, char, Boolean, String
  • #31 primitive
  • #32 C
  • #33 B
  • #34 False
  • #35 E
  • #36 b
  • #37 A
  • #38 false
  • #39 A
  • #40 A
  • #41 B
  • #42 B
  • #43 A