Sie sind auf Seite 1von 24

Java has two sets of data types: Primitive and Reference (or

Non-primitive). Below is a table of primitive data types with a few


examples of data that they can handle.
Data Type Example/s
string “hello world”
boolean true, false
char ‘A’, ‘z’, ‘\n’, ‘6’
byte 1
short 11
int 167
long 11167L
boolean is a logical data type. In Java, it has only two possible
value: true or false. But in C/C++ (another OOP language), it can
be represented by 0 (false) or 1 (true).

char is a data type used for single characters. When assigning


literals to identifiers declared as char, you need to enclose the
literal in single quotes (‘ ‘). Literals are values assigned to
variables or constants.
NOTE:

The next-line character (‘\n’) represents a line break. It is


composed of the backslash character (\) and the letter “n” but is
interpreted by Java as a single character.
byte, short, int, and long hold integer values. They differ in size
and range. Here is a table of integer values with their sizes and
ranges.

Data Type Length Range


byte 8 bits −27 𝑡𝑜 27
short 16 bits −215 𝑡𝑜 215
int 32 bits −231 𝑡𝑜 231
long 64 bits −263 𝑡𝑜 263
float and double are floating point literals – numbers with
possible decimal parts, float is 32 bits in length and double is 64
bits in length.

NOTE:

Floating point literals like 1.63 are considered double unless


you explicitly declare them as float by adding “F” at the end. So
1.63 is double and 1.63F is float.
Data types not included among the primitive data types are
called reference data type and they consist of objects and
arrays. String is a reference data type often used in many
applications. It is actually a class that lets us store a sequence
of characters.
VARIABLES
Variables are identifiers whose values can be changed.
The general syntax for declaring variable is:
<data_type> <identifier>;
VARIABLES
boolean Passed=true, Switch;
char EquivalentGrade=‘F’,ch1, ch2;
short SH, Classes=19;
int Faculty_No=6781, Num1;
long Student_No=76667, Employee_No, Long1;
float Average=96.89F, Salary;
double Logarithm=o.8795564564, Tax, SSS;
String LastName=“Bonifacio”, FirstName=“Andres”, MiddleName;
CONSTANTS
In constant to variables, constants are identifiers whose
values never change once declared. The general syntax
for declaring constants is :
final <type> <identifier> = <literal>;
CONSTANTS
In constant to variables, constants are identifiers whose
values never change once declared. The general syntax
for declaring constants is :
final <type> <identifier> = <literal>;
CONSTANTS
Examples:
final String Student_Bday = “11-30-1863”;
final char gender = ‘m’;
final double Student_Allowance = 999.99;
CASTING
Casting is the process of assigning a value or variable of
a specific type to a variable of another type. Java
performs type conversion automatically when value of
smaller size is assigned to variable with a larger size.
CASTING
For example, assigning an integer value to a double
variable is automatically done (variables declared as int
occupy 32 bits while a double occupies 64 bits).
CASTING
However, when there is a possible loss of data such as
when a double value is assigned to an integer variable,
this should be explicitly stated. Here is an example of
how to explicitly tell the compiler to cast.
CASTING
CASTING

Line 9 declares an integer x and initializes it to 10. At the


same time, average is declared as an int with an initial
value of 0.
CASTING

Line 10 declares Quiz_1 and Quiz_2 of type double with


initial values of 10 and 9 respectively.
CASTING

Line 11 declares a character variable c and initializes it


to the letter ‘a’. Remember character literals are
enclosed with in single quotes (‘ ‘).
CASTING

Line 12 explicitly cast the Quiz_1 and Quiz_2 from double to int.
This is done manually by the programmer noticing that integer is
lower than double which means there is a loss of data. This what
they call downward cast (casting from value occupying more
memory to value occupying less memory).
CASTING

Line 13 implicitly cast the character c into an integer. This is done


automatically by the compiler due to the reason that integer is
higher compared to character in terms of memory occupation. The
numeric 97 is the actual equivalent of ‘a’ in ASCII.

Das könnte Ihnen auch gefallen