byte, short, int, long, float, double, boolean and char
3
New cards
Non-primitive data types
String, Arrays and Classes
4
New cards
main method
public static void main(String [] args) -A built-in main method present in every java program -code inside this method gets executed
5
New cards
Syntax for printing
System.out.print(); System.out.println();
6
New cards
Write the shortest system print method input that would output: one "two" three four"!
System.out.print("one \"two\"\nthree\nfour\"!");
7
New cards
Declaring a variable
Data Type + Variable Name int age;
8
New cards
lowerCamelCase
A naming convention where the first letter is lower case, and each subsequent start of a word is upper case. Variables must be written in localCamelCase.
9
New cards
Initializing a variable
Data Type + Variable Name + Value String name = "Naoko"; or name = "Naoko"; if variable has already been declared.
10
New cards
What does the keyword final do?
It prevents variables from being altered.
11
New cards
int number = ((2.0 * 4) / 2) + (3.0 / 2); number = ?
number = 5;
12
New cards
double number = (2 * 3) / 4; number = ?
number = 1.0;
13
New cards
double number = (5 % 3) * (5 / 3); number = ?
number = 2.0;
14
New cards
Add or subtract a variable by 1?
variableName++; variableName--;
15
New cards
Modify the current value of a variable by another value without calling the variable again.
x += y; x -= y; x *= y; x /= y; x %= y;
16
New cards
What will the program output? int num = 2; int dividend = 5; dividend /= num; System.out.println(dividend);
The output will be 2 because when two integers are divided in Java, the decimal portion is always truncated.
17
New cards
Import Scanner
import java.util.Scanner;
18
New cards
Initialize Scanner
Scanner objectName = new Scanner(System.in);
19
New cards
System.in
Value that typically corresponds to keyboard input. including this allows us to input keyboard data into our programs.
20
New cards
Gathering information from user input.
input.nextLine(); input.nextInt(); input.nextDouble(); The data type of the declared variable MUST match the data type of input.next
21
New cards
What happens when nextInt() or nextDouble() comes before nextLine().
nextInt() only reads a portion of the line the user inputs meaning nextLine() will read from the same line as nextInt().
22
New cards
Use nextLine(); after a partial line method.
Add input.nextLine(); after the partial line method. int number = input.nextInt(); input.nextLine(); String name = input.nextLine();
23
New cards
Casting
Turning one variable type into another.
24
New cards
What happens when we cast a double to an int in the program.
Program will throw an error.
25
New cards
What happens when we cast an int to a double in the program.
Java automatically casts the value correctly.
26
New cards
Fix the code: int dollars = 100; int numPeople = 40; double dollarPerPerson = dollars / numPeople;
int dollars = 100; int numPeople = 40; double dollarPerPerson = (double)dollars / numPeople;
27
New cards
Rounding positive and negitive double values with casting.
int positive = (int) (x + 0.5); int negative = (int) (x - 0.5)
28
New cards
What is the total number of values that int can compute and store.
2^32 different values
29
New cards
What is the range of the total number of values that in can compute and store.