1/21
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Variables
Act as containers to store data that can be referenced and manipulated in a program. Each has a specific type, determining the kind of data it can hold.
Declaring a variable
int age;
double salary;
String name;
char middleName;It specify the data type, followed by the variable name.
Variable Initialization
int age = 19;
double salary = 20000.9876;
String name = "Grrrr";
char middleName = 'G';Variables are declared or later in the code
Statically-typed Language
Java is a _____________, meaning each variable must have a type defined at compile time.
Primitive Data Types
Reference Data Types
Two primary data types of Java.
byte
short
int
long
float
double
char
boolean
Primitive Data Types
byte
1 byte which are used for small numbers that ranges from -128 to 127.
short
2 bytes which is suitable for slightly larger numbers that ranges from -32,768 to 32,767.
int
4 bytes which commonly used integer type that ranges from -231 to 231-1.
long
long bigNumber = 12345678L;8 bytes which uses for a larger integer type that uses L suffix for literals.
float
float pi=3.14F;4 bytes which is a single-precision floating point that uses F suffix.
double
8 bytes which is a double-precision floating point and default for decimal values.
char
2 bytes which stores a single Unicode character.
boolean
1 byte which holds true or false.
Reference Data Types
These include classes, interfaces, and arrays, such as String, arrays (int[]), and custom classes. Unlike primitives, they store references to objects rather than the object itself.
Scope
The section of the code where a variable can be accessed.
Local Variables
Declared within a method and accessible only within that method.
Instance Variables (Fields)
Declared within a class but outside any method. They belong to the instance of the class.
Class Variables (Static Fields)
Declared with the static keyword and shared across all instances of the class.
Lifetime
The period during which a variable occupies memory. Local variables are deleted after method execution, while instance and class variables persist as long as the object or class exists
Deleted
Lifetime is when a variable occupies memory. Local variables are ______ after method execution, while instance and class variables persist as long as the object or class exists
Persist
Lifetime is when a variable occupies memory. Local variables are deleted after method execution, while instance and class variables _______ as long as the object or class exists.