AP COMP SCI
int num = 7;
Is stored in 4 bytes
“int“ creates an integer variable
“num” is the placement for the name of the variable.
EX: numPets, inStorage
“7“ is the value of the variable
“=” is an assignment operator
Assignment operators take whats on the right-hand side and puts that value in the variable.
double costBurger = 3.95;
Is stored in 8 bytes
String student = "Vicky Viking";
char mcChoice = 'a';
Only need 2 parts to create a variable
By creating a variable, the space is allocated in the stack even if the value is not yet defined.
(+) Operator does 2 things
3 + 2 adds to 5
String + number → acts like a concatenation operator
(adding)
(subtract)
/ (divide)
* (multiple)
% (modulus)
int num = 5 + 7;
//num = 12
int num = 7 - 5;
// num = 2
int num = 10 / 5;
// num = 2
int num = 2 * 5;
//num = 10
int num = 15 % 6;
// num = 3
+= (Plus Equals)
int num = 5;
num += 10;
//num is now 15
-= (Minus Equals)
int num = 5;
num -= 3;
//num is now 2
*= (Multiply Equals)
int num = 5;
num *= 2;
//num is now 10
/= (Slash Equals)
int num = 10;
num /= 2;
//num is now 5
%= (Mod Equals)
int num = 5;
num %= 2;
//num is now 1
++
int num = 5;
num ++;
//num is now 6
--
int num = 5;
num --;
//num is now 4
int num = 7;
int newNum = 15;
int div = newNum/num;
// The value stored in div will be 2
double div = newNum/num;
// The value stored in div will be 2.0
double div = (double)newNum/num;
// The value stored in div will be 2.14285...
// By casting newNum as a double, we can prevent integer division and allow it to divide properly.
Casting does not change the variable type permanently.
Java uses Classes
Classes describe objects
Running the code creates objects
“instantiating an object”
“create an instance of an object”
→ have stuff = variables
→ do stuff = methods
→ (Store in) file named “Student.java“
In our main file
→ create Student objects
Variables | |
---|---|
name | Bob |
gpa | 2.0 |
Methods | |
---|---|
study | Bob.study() |
takeTest | Bob.takeTest |
→ returns nothing
public String toString()
// public = privacy spec
// Returns a String variable
// toString = name of method
//--------------------------------------------------------
Booleans are variables that can be set to true or false
//Example
boolean isHungry = true;
System.out.println("Are you hungry?" + isHungry);
//Prints out Are you hungry? true
int num = 7;
Is stored in 4 bytes
“int“ creates an integer variable
“num” is the placement for the name of the variable.
EX: numPets, inStorage
“7“ is the value of the variable
“=” is an assignment operator
Assignment operators take whats on the right-hand side and puts that value in the variable.
double costBurger = 3.95;
Is stored in 8 bytes
String student = "Vicky Viking";
char mcChoice = 'a';
Only need 2 parts to create a variable
By creating a variable, the space is allocated in the stack even if the value is not yet defined.
(+) Operator does 2 things
3 + 2 adds to 5
String + number → acts like a concatenation operator
(adding)
(subtract)
/ (divide)
* (multiple)
% (modulus)
int num = 5 + 7;
//num = 12
int num = 7 - 5;
// num = 2
int num = 10 / 5;
// num = 2
int num = 2 * 5;
//num = 10
int num = 15 % 6;
// num = 3
+= (Plus Equals)
int num = 5;
num += 10;
//num is now 15
-= (Minus Equals)
int num = 5;
num -= 3;
//num is now 2
*= (Multiply Equals)
int num = 5;
num *= 2;
//num is now 10
/= (Slash Equals)
int num = 10;
num /= 2;
//num is now 5
%= (Mod Equals)
int num = 5;
num %= 2;
//num is now 1
++
int num = 5;
num ++;
//num is now 6
--
int num = 5;
num --;
//num is now 4
int num = 7;
int newNum = 15;
int div = newNum/num;
// The value stored in div will be 2
double div = newNum/num;
// The value stored in div will be 2.0
double div = (double)newNum/num;
// The value stored in div will be 2.14285...
// By casting newNum as a double, we can prevent integer division and allow it to divide properly.
Casting does not change the variable type permanently.
Java uses Classes
Classes describe objects
Running the code creates objects
“instantiating an object”
“create an instance of an object”
→ have stuff = variables
→ do stuff = methods
→ (Store in) file named “Student.java“
In our main file
→ create Student objects
Variables | |
---|---|
name | Bob |
gpa | 2.0 |
Methods | |
---|---|
study | Bob.study() |
takeTest | Bob.takeTest |
→ returns nothing
public String toString()
// public = privacy spec
// Returns a String variable
// toString = name of method
//--------------------------------------------------------
Booleans are variables that can be set to true or false
//Example
boolean isHungry = true;
System.out.println("Are you hungry?" + isHungry);
//Prints out Are you hungry? true