Java Tutorial 4 - Variables, Numeric Primitive Data Types 1-2

preview_player
Показать описание
Variables

- to store data

1
1 byte
0 0 0 0 0 0 0 1

10
1 byte
0 0 0 0 1 0 1 0

Declaring Variable

int x; // declare x to be an integer variable
double radius; // declare radius to be a double variable
char a; // declare a to be a character variable

Naming Conventions

- meaningful and descriptive names e.g. area
- letters, digits, underscores, dollar signs
- use lowercase e.g. age,
- begin with lowercase, and capitalize the next word e.g. computeArea

Numeric primitive data types:

byte 1 byte -128 to 127 (-2^7 to 2^7-1).
short 2 bytes -32,768 to 32,767.
int 4 bytes -2,147,483,648 to 2,147,483,647.
long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80.
float 4 bytes 7 decimal digits.
double 8 bytes 16 decimal digits.

Assignment Statements

x = 1; // assign 1 to x

radius = 1.0; // assign 1.0 to radius

a = 'A'; // assign 'A' to a

Numeric Operators

+ Addition 34 + 1 35
- subtraction 34.0 - 0.1 33.9
* Multiplication 20 * 2 40
/ Division 1.0 / 2.0 0.5
% Remainder 20 % 3 2
Рекомендации по теме