Data types, keywords, identifiers
in java
•What are identifiers in java.
•What are keywords how & why they are used?
•What are data types in java.
4/10/2019 1Jamsher Bhanbhro(F16CS11)
Identifiers
• Names required for java classes, variables, methods
are called identifiers.
• Can be A-Z or a-z or in mixture(Alphabets).
• Currency character($) can be used as identifier.
• _ underscore can be used.
• Keywords cannot be used as identifier.
• Eg: HelloWorld, Jamsher (may be class names).
• A=3; a=5; z=c; x=y, can be the variables.
• Sum, Sub, Div can be methods.
4/10/2019 Jamsher Bhanbhro(F16CS11) 2
Java Keywords
• Keywords are reserved names for java.
• These can’t be used as identifiers, variables name
etc.
• Abstract: used for class
• Boolean: 0 or 1 true or false
• Break: use to break the statement or the condition
• Char: for data type
• Import, class, switch, case, if else, try, this, throw,
void, return, package etc are the java keywords.
4/10/2019 Jamsher Bhanbhro(F16CS11) 3
Data types in java
• When a variable is assigned a memory it is
assigned by its data types.
• A variable store which type of data is known
as data type.
• There are basically two data types available in
java
1. Primitive data type
2. Non Primitive data type/ Reference data type
4/10/2019 Jamsher Bhanbhro(F16CS11) 4
Primitive data type
• Those data types which are already defined in
the java library is known as Primitive data
type.
• These are built-in or predefined data types.
• Java basically supports 8 predefined data
types.
• Which are byte, short, long, float, double,
Boolean, char, int.
4/10/2019 Jamsher Bhanbhro(F16CS11) 5
1. Integer Data type
• Integer is denoted by int and stores the integer values.
• Int data type is a 32-bit signed two's complement
integer
• Minimum value is - 2,147,483,648 (-2^31)
• Maximum value is 2,147,483,647(inclusive) (2^31 -1)
• Integer is generally used as the default data type for
integral values unless there is a concern about memory.
• The default value is 0
• Example: int a = 11, int b = 41
4/10/2019 Jamsher Bhanbhro(F16CS11) 6
2. Byte
• Byte data type is an 8-bit signed two's
complement integer
• Minimum value is -128 (-2^7).
• Maximum value is 127 (inclusive)(2^7 -1)
• Default value is 0.
• Byte data type is used to save space in large
arrays, mainly in place of integers, since a byte is
four times smaller than an integer
• For Example: byte a=11, b=41;
4/10/2019 Jamsher Bhanbhro(F16CS11) 7
3. Float
• Float data-type is a single-precision 32-bit
IEEE 754 floating point.
• Float is mainly used to save memory in large
arrays of floating point numbers.
• Default value is 0.0f.
• Float data-type is never used for precise
values such as currency.
• Example: float f1 = 234.5f(f stands for float)
4/10/2019 Jamsher Bhanbhro(F16CS11) 8
4. Short
• Short datatype is a 16-bit signed two's complement
integer
• Minimum value is -32,768 (-2^15)
• Maximum value is 32,767 (inclusive) (2^15 -1)
• Short datatype can also be used to save memory as byte
data type. A short is 2 times smaller than an integer
• Default value is 0
4/10/2019 Jamsher Bhanbhro(F16CS11) 9
5. Long
• Long datatype is a 64-bit signed two's
complement integer
• Minimum value is 9,223,372,036,854,775,808 (-
2^63)
• Maximum value is9,223,372,036,854,775,807
(inclusive) (2^63 -1)
• This type is used when a wider range than int is
needed.
• Default value is 0L(L is used for Long)
• Example: long a = 100000L, long b = -200000L
4/10/2019 Jamsher Bhanbhro(F16CS11) 10
6. Char
• char data-type is a single 16-bit Unicode
character.
• Minimum value is 'u0000' (or 0) .
• Maximum value is 'uffff' (or 65,535 inclusive)
.
• Char data-type is used to store any character
• Example: char letterA =‘J‘;
4/10/2019 Jamsher Bhanbhro(F16CS11) 11
7. Boolean
• Boolean data-type represents one bit of
information.
• There are only two possible values: true and
false.
• This data-type is used for simple flags that
track true/false conditions.
• Default value is false .
• Example: boolean one = true, 0=false;
4/10/2019 Jamsher Bhanbhro(F16CS11) 12
8. Double
• double data-type is a double-precision 64-bit
IEEE 754 floating point.
• This data-type is generally used as the default
data type for decimal values, generally the
default choice.
• Double data-type should never be used for
precise values such as currency.
• Default value is 0.0d(d for double)
4/10/2019 Jamsher Bhanbhro(F16CS11) 13
Reference data-types
• These are not predefined data-types.
• These are created using defined constructors
of the classes.
• They are used to access the objects
• These are the reference variables
• eg: Jamsher ob=new Jamsher();
4/10/2019 Jamsher Bhanbhro(F16CS11) 14

Lect5

  • 1.
    Data types, keywords,identifiers in java •What are identifiers in java. •What are keywords how & why they are used? •What are data types in java. 4/10/2019 1Jamsher Bhanbhro(F16CS11)
  • 2.
    Identifiers • Names requiredfor java classes, variables, methods are called identifiers. • Can be A-Z or a-z or in mixture(Alphabets). • Currency character($) can be used as identifier. • _ underscore can be used. • Keywords cannot be used as identifier. • Eg: HelloWorld, Jamsher (may be class names). • A=3; a=5; z=c; x=y, can be the variables. • Sum, Sub, Div can be methods. 4/10/2019 Jamsher Bhanbhro(F16CS11) 2
  • 3.
    Java Keywords • Keywordsare reserved names for java. • These can’t be used as identifiers, variables name etc. • Abstract: used for class • Boolean: 0 or 1 true or false • Break: use to break the statement or the condition • Char: for data type • Import, class, switch, case, if else, try, this, throw, void, return, package etc are the java keywords. 4/10/2019 Jamsher Bhanbhro(F16CS11) 3
  • 4.
    Data types injava • When a variable is assigned a memory it is assigned by its data types. • A variable store which type of data is known as data type. • There are basically two data types available in java 1. Primitive data type 2. Non Primitive data type/ Reference data type 4/10/2019 Jamsher Bhanbhro(F16CS11) 4
  • 5.
    Primitive data type •Those data types which are already defined in the java library is known as Primitive data type. • These are built-in or predefined data types. • Java basically supports 8 predefined data types. • Which are byte, short, long, float, double, Boolean, char, int. 4/10/2019 Jamsher Bhanbhro(F16CS11) 5
  • 6.
    1. Integer Datatype • Integer is denoted by int and stores the integer values. • Int data type is a 32-bit signed two's complement integer • Minimum value is - 2,147,483,648 (-2^31) • Maximum value is 2,147,483,647(inclusive) (2^31 -1) • Integer is generally used as the default data type for integral values unless there is a concern about memory. • The default value is 0 • Example: int a = 11, int b = 41 4/10/2019 Jamsher Bhanbhro(F16CS11) 6
  • 7.
    2. Byte • Bytedata type is an 8-bit signed two's complement integer • Minimum value is -128 (-2^7). • Maximum value is 127 (inclusive)(2^7 -1) • Default value is 0. • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer • For Example: byte a=11, b=41; 4/10/2019 Jamsher Bhanbhro(F16CS11) 7
  • 8.
    3. Float • Floatdata-type is a single-precision 32-bit IEEE 754 floating point. • Float is mainly used to save memory in large arrays of floating point numbers. • Default value is 0.0f. • Float data-type is never used for precise values such as currency. • Example: float f1 = 234.5f(f stands for float) 4/10/2019 Jamsher Bhanbhro(F16CS11) 8
  • 9.
    4. Short • Shortdatatype is a 16-bit signed two's complement integer • Minimum value is -32,768 (-2^15) • Maximum value is 32,767 (inclusive) (2^15 -1) • Short datatype can also be used to save memory as byte data type. A short is 2 times smaller than an integer • Default value is 0 4/10/2019 Jamsher Bhanbhro(F16CS11) 9
  • 10.
    5. Long • Longdatatype is a 64-bit signed two's complement integer • Minimum value is 9,223,372,036,854,775,808 (- 2^63) • Maximum value is9,223,372,036,854,775,807 (inclusive) (2^63 -1) • This type is used when a wider range than int is needed. • Default value is 0L(L is used for Long) • Example: long a = 100000L, long b = -200000L 4/10/2019 Jamsher Bhanbhro(F16CS11) 10
  • 11.
    6. Char • chardata-type is a single 16-bit Unicode character. • Minimum value is 'u0000' (or 0) . • Maximum value is 'uffff' (or 65,535 inclusive) . • Char data-type is used to store any character • Example: char letterA =‘J‘; 4/10/2019 Jamsher Bhanbhro(F16CS11) 11
  • 12.
    7. Boolean • Booleandata-type represents one bit of information. • There are only two possible values: true and false. • This data-type is used for simple flags that track true/false conditions. • Default value is false . • Example: boolean one = true, 0=false; 4/10/2019 Jamsher Bhanbhro(F16CS11) 12
  • 13.
    8. Double • doubledata-type is a double-precision 64-bit IEEE 754 floating point. • This data-type is generally used as the default data type for decimal values, generally the default choice. • Double data-type should never be used for precise values such as currency. • Default value is 0.0d(d for double) 4/10/2019 Jamsher Bhanbhro(F16CS11) 13
  • 14.
    Reference data-types • Theseare not predefined data-types. • These are created using defined constructors of the classes. • They are used to access the objects • These are the reference variables • eg: Jamsher ob=new Jamsher(); 4/10/2019 Jamsher Bhanbhro(F16CS11) 14