0% found this document useful (0 votes)
43 views

Unit 03 - Data Types and Variables

This document discusses basic data types and variables in Java. It defines key terms like data, information, literals, and identifiers. It then lists the 8 primitive data types in Java - byte, short, int, long, float, double, boolean, and char - along with their sizes and uses. Examples of declaring variables of each type are provided. Rules for naming variables are outlined. The syntax for declaring a variable is defined as Type variable name; with an example given.

Uploaded by

Mik Reyes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Unit 03 - Data Types and Variables

This document discusses basic data types and variables in Java. It defines key terms like data, information, literals, and identifiers. It then lists the 8 primitive data types in Java - byte, short, int, long, float, double, boolean, and char - along with their sizes and uses. Examples of declaring variables of each type are provided. Rules for naming variables are outlined. The syntax for declaring a variable is defined as Type variable name; with an example given.

Uploaded by

Mik Reyes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

UNIT 03 – BASIC ELEMENTS OF A JAVA PROGRAM

LESSON # 3.2
TITLE: Data Types and Variables

Terminologies
Data – Raw facts and figures.
Information – manipulating or transforming data types.
Literals – is a notation or representation for a fixed value in the source code.
Primitive Data type – is a type of data that can have only one value at a time.
Character Escape Sequence – is a string of characters preceded by an escape character (\) that
represents a command to a printer or display device.
Identifiers – are names given to data, methods, and classes.

Discussion

Java Primitive Data Types

Data Type Size Description


byte 1 byte Stores whole numbers from -128 to 127.
short 2 bytes Stores whole numbers from -32,768 to 32,767.
int 4 bytes Stores whole numbers from -2,147,483,648 to
2,147,483,647.
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807.
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7
decimal digits.
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal
digits.
boolean 1 bit Stores true or false values.
char 2 bytes Stores a single character/letter or ASCII values.

Examples:
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99f; // Floating point number
char myLetter = 'D'; // Character
boolean myBool = true; // Boolean
String myText = "Hello"; // String

Sample of Literals:
3 4.5 ‘a’ “this is a string”

Character Escape Sequence


Escape Sequence Description
\’ Single Quote
\” Double Quote
\\ Backslash
\r Carriage Return
\n New Line
\t Tab
Creating Variables

Rules for naming variables:


• All variable names must begin with a letter of the alphabet, an underscore, or (_), or a dollar sign
($). The convention is to always use a letter of the alphabet. The dollar sign and the underscore are
discouraged.

• After the first initial letter, variable names may also contain letters and the digits 0 to 9. No spaces
or special characters are allowed.

• The name can be of any length, but don't get carried away. Remember that you will have to type
this name.

• Uppercase characters are distinct from lowercase characters. Using ALL uppercase letters are
primarily used to identify constant variables. Remember that variable names are case-sensitive.

• You cannot use a java keyword (reserved word) for a variable name.

Syntax in declaring a variable

Syntax
Type variable name;

Example:
int num1; //each declaration should be terminated by a semicolon
double sum, product; // declaration of two variables of the same type
char code = ‘a’ // declaration and initialization of the variable code

You might also like