0% found this document useful (0 votes)
16 views2 pages

Week 2

The document discusses data types, variables, literals, declaration, assignment, initialization and printing values of variables. It defines what variables are, how they are declared and different data types like integers, characters, strings and booleans that should be compatible with variable types. It also explains how values are assigned and initialized to variables.

Uploaded by

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

Week 2

The document discusses data types, variables, literals, declaration, assignment, initialization and printing values of variables. It defines what variables are, how they are declared and different data types like integers, characters, strings and booleans that should be compatible with variable types. It also explains how values are assigned and initialized to variables.

Uploaded by

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

DATA TYPES, VARIABLES & LITERALS

VARIABLES
Used to store values in our computer’s memory.
 Our computer has a memory.
 To store values in this memory we need to reserve some space.
o Use a variable
 Each variable has a specific type.
 It is called a variable because the value inside it can change.

DECLARATION
Allocating space inside our memory.
 To allocate space in our memory we declared a variable.
o TYPE NAME;
 The type of the variable should be compatible with the data inside it.
o To store a string inside a variable, the variables type should be string.
 String myName;
o Declared a variable called myName and can store a String.
 To declare multiple variables of the same type.
o TYPE NAME1, NAME2, NAME3;
 String myName, myJob;
o myName and my job are two variables that can store a String.
 A variable must be declared before it can be used.

DATATYPES
A type that should be compatible with the data inside it.
 Integers – Numbers without a decimal part.
Ex: 1, 2, 100, -4, -9, 0
 Real numbers / floating point numbers – Numbers with decimal part.
Ex: 1.5, 2.25, -4.7, -9.9, 1.0, 0.0
 Characters – All characters on the keyboard surrounded with ‘single quotes’.
Ex: ‘a’, ‘5’, ‘-‘, ‘*’, ‘?’, ‘$’, ‘;’, ‘,’
 Strings – Group of characters.
Ex: “a”, “abc123”, “534”, “hello”
 Booleans – Represents true or False.
Ex: true, false
ASSIGNMENT
Used to store/put a value inside a variable.
 We can assign a value to a variable by using the assignment operator (=).
 VariableName = expression
o myJob=”Prgrammer”;
o “Programmer” will be stored inside my job.
 An expression is anything that produces/gives a value
o Examples: (1+3), (4*2).

INITIALIZATION
Assigning a value to a variable when declaring it.
o Examples: String myJob = “Programmer”;
o This equivalent to
- String myJob;
myJob=”Programmer”;

be careful: do not re-defined variables


Consider the following code:

 Each variable has a unique name.


 When assigning a value to a variable do not define it again.

PRINTING THE VALUE OF VARIABLE


Use print()/println()

You might also like