Chapter 2 Flashcards
Data Type
The name for group of values. Everything that need to be manipulated in Java has its ‘type’. Java supports two different kinds of data: primitive data and objects.
Primitive Types
There are 8 Primitive data types in Java: boolean, byte, char, double, float, int, long and shot. Four of them are fundamental: boolean, char, double and int.
Expression
set of operations that produces a value or a simple value.
type ‘char’
A character literal has type char.
Ex.
‘a’ ‘m’ ‘x’ ‘!’ ‘3’ ‘\’ ‘'
’/’ division
19 / 5 is 3.8 on a calculator, so 19 / 5 evaluates to 3 in Java programming.
4.8 / 2.0 is 2.4
numerator / denominator
4 / 8
numerator -> 4
denominator -> 8
Mixing Types and Casting
If one of the operands is type double the result is double
double 5 / 1.5
Java can convert values with ‘cast’.
The cast is applied to whatever comes right after if .
For ex.
(int) 5.2 / 1.5 -> 2.0 has not the same result
as
(int)(5.2 / 1.5) -> 2
Variable
a memory location with name and type that
stores a value.
The name must start with a letter and then can be followed by any combination of letters and digits.
ex.
int number = 4;
double countSum = 0.0;
Before variables can be used in a Java program, they must be declared. -> It’s called a variable ‘declaration’.
Declaration - a request to set aside a new variable with a given name and type.
ex. double height; or double height = 5.2 ; -> (declaration and assignment) or double height, weight; or double height = 5.2, weight = 105
Assignment Statement
The assignment operator evaluates from fight to left,
which allows to write statements such these:
int x, y, z;
x = y = z = 2 * 5 + 4;
equivalent to:
x = (y = (z = 2 * 5 + 4));
Scope
The part of a program where a particular declaration is valid.
Variable Scope / Local Variables / Localizing Variables
The scope of a variable is from its declaration to the closings curly brace.
Any variables that is declared inside of a method can be accessed only inside of that method. This variable called a Local Variable.
For example, a variable delayed inside a for loop cannot be accessed from outside of that loop.
Localizing Variables - declaring variables in the most local scope possible.
Class Constants
A named value that cannot be changed. A class constant can be accessed anywhere in the class (its scope).
A general syntax:
public static final =
The keyword “final” indicated that the value cannot be changed.
In case a method to be able to access constants , the constants must be static.
Ex.
public static final int HEIGHT = 10;