Basics Flashcards
How many characters can a long store?
long largest =
19 characters
Can a comparison in Java have more than 2 sides? e.g.
while(!dice1 == dice2 ==dice3)
// all 3 dice match
?
No. Only 2 sides. So:
!(dice1 ==2 && dice2 == dice3)
while not
dice 1 is the same as dice 2
and
dice 2 is the same as dice 3
Define a variable
a called message (with some rules) that has been stored under a variable name
Variable Name rules?
1) starts with a lower case letter
2) must define the type (String, Boolean, Double, Int)
Variable Name rules?
1) starts with a lower case letter
2) must define the type (String, Boolean, Double, Int, Char)
Variable Name declaration rules?
1) starts with a lower case letter
2) must define the type (String, Boolean, Double, Int, Char)
3) camel case - mutiple words strung together, first word must be lower case followed by upper case on first letter of each subsequent word e.g. glasgowIsgreat = true;
4) cannot start with numbers
5) cannot start with ‘ or “
6) stick to standard text characters
7) make it mean something and simple (as long as you like)
String message = “Hello!”;
double?
stores decimals, -ve numbers
1,1, 2.1345, -1.4e10
char
stores individual characters
a, b, 1
char
stores individual characters
(a, b, 1)
char d = ‘f’;
Not single ‘’
boolean
stores T or F
boolean GlasgowIsgreat = true;
What is camel case?
mutiple words strung together, first word must be lower case followed by upper case on first letter of each subsequent word e.g. glasgowIsgreat = true;
What is the difference between doing maths using double and int?
Integer division - the remainders are left out!
int a = 3;
int b = 2;
a/b;
= 1 [not 1.5!]
For integer division, how does Java get the remainder (how much is left)?
3 % 2 = 1 // can remove one 2 from 3, and we are left with 1
5 % 6 = 5 // all 5 are remaining
10 % 4 = 2
What is the Order of Operations in Java?
1) not necessarily Left to Right
2) Order is:
*
/
+
-
* and / are done BEFORE + and -
3) If multiple / and *, they are done Left to Right
When using brackets to specify the order of operations, which nested values are evaluated first?
the innermost
e.g.
(a(b + c))/(ac + b);
If you want to divide two int variables and then do a standard double division i.e. change from int division to answer being a double?
e.g. double c = a/b
double c = 1.0*a/b;
1.0* converts a into a double and then the division is done as a double division
What is casting?
Casting is converting one type to another and storing it in another variable e.g. convert d (a double variable) into an int and storing the result in c
Can you cast from a double to a string?
NO
What is a literal?
when we put actual values into the programme e.g.
String message = “Hola Chica!” –> is the literal
It is a hard coded value
What is a method?
a function
Can you put two variables inside the same scope, even if it's a different type? e.g. int a = 5; a = 6; or int a = 5; int a = 6;
NEVER
int a = 5;
a = 6;
is correct
Can you put two variables inside the same scope, even if it's a different type? e.g. int a = 5; a = 6; or int a = 5; int a = 6;
NEVER
int a = 5;
a = 6;
is correct
Can you put two variables inside the same scope, even if it's a different type? e.g. int a = 5; a = 6; or int a = 5; int a = 6;
NEVER
int a = 5;
a = 6;
is correct
How do you stop a programme from running from inside a programme?
What is the convention?
System.exit(int n);
Calling this method terminates your programme. The value n is passed back to whatever started your programme and the normal convention is:
N = 0 //sucessfully ran
N = 1 //unsuccessful
What is overloading?
same name for two different methods but with different arguments
e.g. to calculate the area of a rectangle
public static double rectangleArea (double width){ return rectangleArea(width, width); }
What is the signature of a method?
its name and argument list
computeArea(double myRadius)
What are the 5 conditions in Java?
If
Else
Else if
==,
not
How do you write comments?
//
/* */
In conditions, what does == mean?
equals
In conditions, what does != mean?
not equals
> =
greater than or equal to
<=
less than or equal to
How do you string operations together?
&&
(both conditions have to be true - and)
||
(either condition has to be true - or)
When conducting string equality, how should we compare two strings?
if (a.equals(b)) {…
}
DO NOT DO:
if(a==b){…}