Skip to content

Instantly share code, notes, and snippets.

@Kev-in123
Last active December 27, 2021 00:16
Show Gist options
  • Save Kev-in123/df5706608062df3fdb9bee9a83a73edc to your computer and use it in GitHub Desktop.
Save Kev-in123/df5706608062df3fdb9bee9a83a73edc to your computer and use it in GitHub Desktop.
Quiz: Java Basics
Non-numerical math values in java:
n/0.0 = Infinity where n is a positive number
n/0.0 = -Infinity where n is a negative number
0/0.0 = NaN (can also do 0.0/0.0)
note the 0.0 is a decimal and not an integer
Operators:
+ adds the numbers
- subtracts the numbers
* multiplies the numbers
/ divides the numbers (if both numbers used are both integers, drop the decimal once done dividing)
% divides the numbers and returns the remainder
BEDMAS:
You should know these but here they are
B - brackets
E - exponents
D - division
M - multiplication
A - addition
S - subtraction
division and multiplication can be switched around
addition and subtraction can be switched around
Math Class:
double Math.E() returns Euler's number (e)
double Math.PI() returns PI
t Math.abs(n) returns the absolute value of n with the same type specified in the paramater (int/long/float/double)
double Math.pow(n1, n2) returns n1 raised to the power of n2
double Math.random() returns a random number between 0 and 1 (0 inclided 1 is not)
double Math.sqrt(n) returns the square root of n
double Math.exp(n) returns e to the power n
double Math.log(n) returns the logarithm of n to base e
double Math.log10(n) returns the logarithm of n to base 10
double Math.sin(n) returns the sine of n
double Math.cos(n) returns the cosine of n
double Math.tan(n) returns the tangent of n
double Math.toRadians(n) returns n (angle in degrees) converted to radians
double Math.toDegrees(n) returns n (angle in radians) converted to degrees
t Math.max(n, m) returns the larger value of n and m with the same type specified in the paramater (int/long/float/double)
t Math.min(n, m) returns the smaller value of n and m with the same type specified in the paramater (int/long/float/double)
double Math.floor(n) Returns the closest integer-valued double which is equal to or less than n
double Math.ceil(n) Returns the closest integer-valued double which is equal to or greater than n
double Math.rint(n) Returns the closest integer-valued double to n
long Math.round(n) Returns the long which is closest in value to the double n
int Math.round(n) Returns the int which is closest in value to the float n
Naming conventions:
1) under 256 characters
2) only letters, digits and underscores (_)
3) can’t start with a digit
4) no spaces
5) no reserved keywords
Most of these are based off memory so there might be some slight inaccuracy, others were found through googling
Note that most of these you won't need to know their use, just need to know the keywords
Reserved keywords:
Data types:
boolean - stores true or false
byte - stores whole numbers from -128 to 127
char - stores a single character/ASCII values
int - stores whole numbers from -2,147,483,648 to 2,147,483,647
long - stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
short - stores whole numbers from -32,768 to 32,767
float - stores fractional numbers, sufficient for storing 6 to 7 decimal digits
double - stores fractional numbers, sufficient for storing 15 decimal digits
Access Control:
private - can only be accessed in the class
protected - can only be accessed in the class or a subclass
public - can be accessed anywhere
Modifiers:
abstract - a non-access modifier, used for classes and methods
final - a non-access modifier used for classes, attributes and methods (not changeable)
finally - executes no matter if the try-except failed or passed
native - specifies that a method is not implemented in the same java source file
private - can only be accessed in the class
protected - can only be accessed in the class or a subclass
public - can be accessed anywhere
static - is a non-access modifier used for methods and attributes
transient - specifies that an attribute is not part of an object's persistent state
synchronized - specifies that methods can only be accessed by one thread at a time
volatile - attribute is not cached thread-locally
strictfp - restrict the precision and rounding of floating-point calculations
Catch-Exception:
try - tries to execute some code
catch - catches the exception raised in try (if applicable)
finally - executes no matter if the try-except failed or passed
throw - throw an error
throws - what error may be thrown
Loops or decision-makes:
break - exits the loop
case - used in a switch-case statement to specify a case
continue - stops the current iteration of the loop and moves on to the next
default - the default block of code in a switch statement, if it's the last statement, it doesn't need a break
do - used together with while to create a do-while loop (please don't do this, just make a while loop normally)
while - similar to if, however, continually executes the code until the boolean is false
for - can be used to perform a task a specific number of times or be used to loop through an array (in python we say list instead of list)
switch - the expression is compared with the values of each case, if none match, then the default is used, if there isn't a default, nothing happens
if - used to check a condition, if true execute the code once
else - if the condition in the if statement is false, the code is executed
Class functions:
abstract - a non-access modifier, used for classes and methods
assert - for debugging
class - used to create a class
extends - extends a class (indicates that a class is inherited from another class)
enum - declares an enumerated type
implements - is used to implement an interface
import - used to import a package, class or interface
instanceof - checks whether an object is an instance of a specific class or an interface
new - creates new objects
package - creates a package
return - what a function returns
interface - used to declare a special type of class that only contains abstract methods
this - the way you reference the class object
throws - what error may be thrown
void - specifies that a method should not have a return value
super - refers to superclass (parent) objects
Assigned values:
true - self-explanatory
false - self-explanatory
null - self-explanatory
Outdated keywords:
const - use final instead
goto - not in use, and has no function
Casting:
Automatic type conversion can happen if both types are compatible and the target type is larger than the source type
Example:
byte i = 50;
// No casting needed for below conversion
short j = i;
int k = j;
long l = k;
float m = l;
double n = m;
It can also happen if a decimal value is added/subtracted/multiplied/divided to an integer value
When you are assigning a larger type to a smaller type, then explicit casting is required
Example:
double d = 75.0;
// Explicit casting is needed for below conversion
float f = (float) d;
long l = (long) f;
int i = (int) l;
short s = (short) i;
byte b = (byte) s;
Generating random numbers:
(int) (Math.random() * ((max/increment) + 1)) * increment + min
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment