JAVA from Codecacemy - my quick notes
L2: Variables 2
Variables – primitives (no built-in behaviour) 2
ints 2
doubles 2
booleans (which store true and false) 3
char 3
Variables – objects (have built-in behaviour) 4
String 4
Static Typing – static checking 4
Naming 5
Review 6
L3: Manipulating Variables 7
Addition and Subtraction (+ and -) 7
Multiplication and Division (* and /) 8
Modulo (%- the rest of full batches) 9
Greater Than and Less Than (< , >) 10
Equals and Not Equals (== and !=) 11
Greater/Less Than or Equal To (>= , =<) 12
.equals() 12
String Concatenation 12
Review 13
Introduction to Classes 14
Classes: Syntax 15
Classes: Constructors 16
Classes: Instance Fields 17
Classes: Constructor Parameters 18
Classes: Assigning Values to Instance Fields 19
Classes: Multiple Fields 20
Classes: Review 21
Example: 22
Functions: 23
L2: Variables
Variables – primitives (no built-in behaviour)
ints
The first type of data we will store is the whole number. Whole numbers are very common in programming. You often see them used to store ages, or maximum sizes, or the number of times some code has been run, among many other uses.
In Java, whole numbers are stored in the int primitive data type.
ints hold positive numbers, negative numbers, and zero. They do not store fractions or numbers with decimals in them.
The int data type allows values between -2,147,483,648 and 2,147,483,647, inclusive.
To declare a variable of type int, we use the int keyword before the variable name:
// int variable declaration
int yearJavaWasCreated;
// assignment
yearJavaWasCreated = 1996;
// declaration and assignment
int numberOfPrimitiveTypes = 8;
doubles
Whole numbers don’t accomplish what we need for every program. What if we wanted to store the price of something? We need a decimal point. What if we wanted to store the world’s population? That number would be larger than the int type can hold.
The double primitive data type can help. double can hold decimals as well as very large and very small numbers. The maximum value is 1.797,693,134,862,315,7 E+308, which is approximately 17 followed by 307 zeros. The minimum value is 4.9 E-324, which is 324 decimal places!
To declare a variable of type double, we use the double keyword in the declaration:
// doubles can have decimal places:
double price = 8.99;
// doubles can have values bigger than what an int could hold:
double gdp = 12237700000;
booleans (which store true and false)
Often our programs face questions that can only be answered with yes or no.
Is the oven on? Is the light green? Did I eat breakfast?
These questions are answered with a boolean, a type that references one of two values: true or false.
We declare boolean variables by using the keyword boolean before the variable name.
boolean javaIsACompiledLanguage = true;
boolean javaIsACupOfCoffee = false;
In future lessons, we’ll see how boolean values help navigate decisions in our programs.
char
How do we answer questions like: What grade did you get on the test? What letter does your name start with?
The char data type can hold any character, like a letter, space, or punctuation mark.
...
gerweb