Chp 2: Using Data Flashcards
declaring/using variables
format:
[datatype] [name/identifier] = [initial value]
ex:
String name = “”;
using the boolean Data Type
a logical data type
it’s either true or false
ex:
if ( boolean condition )
floating-point data types
float
double
data types that have decimals
double takes up more memory/space than float, so it stores greater values
char data types
stores one character
single quotation
declaring a constant
makes it possible for the variable to not be changed
changes the variable into a mathematical constant
becomes read only
you use “final”
ex:
final double pi = 3.14159;
performing arithmetic using variables and constants
\+ - / * %
type conversion
you can use either parse or cast
primitive type: the most basic data types available for Java
int
byte, short, int, long
float
double
char
boolean
String
strongly typed language
one of the characteristics of Java
you need to tell the variables in advance for Java to understand
variables/datas in Java have several types
Java has many rules
assignment operator
=
used to assign a (new) value to a variable
ex:
age = 18;
cast operator
to convert a variable into another variable on the fly
( )
ex:
if age is an int,
(double) age
uninitialized variable
something that Java does not have this: a variable with no initial value
All variables must be declared and initialized
ex: the initial value of a String variable cannot be null
garbage value
random data that will not be used in the program, so it takes up memory for no reason
ex: the value we use to initialize variables
block of code
a section of code that’s together
sometimes put within braces { }