Data Types Keywords Identifiers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

DATA TYPES,

KEYWORDS,
IDENTIFIERS
Java Keywords
Below are Java keywords. They have
specific usage in Java and should not be
used as identifiers. abstract default if private this
boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
continue goto package synchronized
Java Keywords
All of these keywords are in small
letters or lowercase. Remember that
Java is case-sensitive. Capitalizing
any letter of a keyword will convert it
to an identifier.
Identifiers
Identifiers are names supplied by programmers for classes, methods,
constants and variables.
Rules to follow:
• An identifier's first character can be a Unicode letter, a dollar sign
($), or an underscore (_). The rest can be underscores and
alphanumeric characters. Special characters are not allowed.
• There is no restriction on the length of identifiers. To take
advantage of this, make your identifiers as descriptive as possible
of their purpose.
• Capitalize the beginning of each succeeding word of the identifier
if it is made up of several words. There should be no spaces in an
identifier.
• Do not use Java keywords as identifiers.
Identifiers

Examples of valid identifiers:


PhoneModel
TelephoneNumber
$money
_exchange
Examples of invalid identifiers:
switch
Class Room
1st_place
Author's_name
Data Types
Data types in Java can either be primitive or reference (non-
primitive). Objects and arrays are reference data types and variables
of this type have an initial value of null. Another example is String
which is an array made up of characters. In Java, it is a class in the
java.lang package.
Here are primitive data types and their corresponding default
values when a class member variable is initialized. Local variables do
not have automatic initialization with these default values. There is
also a column for additional examples.
Data Types

Data Type Default Value Example


boolean false false, true
char '\u0000' 'L', 'e', '\t', '7'
float 0.0F 22.3F
double 0.0D 22.3
byte 0 23
short 0 1023
int 0 102376
long 0L 19761023
Data Types
boolean is a data type with two logical values: false or true. Java
does not use the boolean values in C/C++ which are 0 (false) or 1
(true).
char is a single character or a 16-bit Unicode character data type.
Notice that data of this type are enclosed in single quotes.
double and float are floating point data types. These are numbers
that may have functional parts.
long, int, short and byte are integer data types. An integer is a
whole number which is either positive or negative.
Data Type Length Range
byte 8 bits -27 to 2 7 -1
short 16 bits -2 15 to 2 15 -1
int 32 bits -2 31 to 2 31 -1
long 64 bits -2 63 to 2 63 -1
Variables
Variables are identifiers that can have changeable values. There
is a need to specify the datat type in declaring a variable. The syntax
is:
<data_type> <identifier>;
Examples:
boolean Healthy;
char Gender;
byte Age;
short Number_Of_Friends;
int bankAccountNumber;
long propertyIdentificationNumber;
float bodyWeight;
double atomicWeight; string schoolName;
Literals
Literals are values that belong to a particular type. There are
literals that cannot be used as identifiers such null, true and false.
The last two are boolean literals. You can also place an initial value
(literal) when declaring a variable and the syntax is:
<data_type> <identifier> = <literal>;
Examples:
boolean Healthy=true;
char Gender='F';
byte Age=32; string schoolName="University of Lesley";
short Number_Of_Friends=255;
int bankAccountNumber=178999;
long propertyIdentificationNumber=6787000001;
float bodyWeight=86.97; double atomicWeight=0.231076;
Literals
It is possible to have several variables declared in one statement
for a specific data type by using a comma (,) to separate each
identifier.
Examples:
boolean Switch, Healthy=true;
char Gender='F', Civil_Status;
byteday, Age=32; string schoolName="University of Lesley";
short Number_Of_Friends=255, ToyCount;
int bankAccountNumber=178999;
long propertyIdentificationNumber=6787000001;
float bodyWeight=86.97; double atomicWeight=0.231076;
Constants
There are also identifiers whose values will never change once
defined. These are called constants. Unlike other programming
languages that use the keyword const, Java uses static final or final
to declare a constant. The syntaxes are:
static final <type> <identifier> = <literal>;
or
final <type> <identifier> = <literal>;

Example:
static final long SpeedOfLight=299792458;
Activity
Fill in the blanks:
Below is a program that prints your name and birthday on the
screen. Fill-in the missing portions.
/* Name of program: myName.java
Made by: _________________
____
package ________________;
public class ______________{
public _____________ main (String___ args)
{
System.out.println("_____________");
___________________("___________");
}
}

You might also like