Skip to content

Instantly share code, notes, and snippets.

@Scherso
Last active October 17, 2022 17:00
Show Gist options
  • Save Scherso/67f62bcb6857ba05b3c9b2d5e33509fa to your computer and use it in GitHub Desktop.
Save Scherso/67f62bcb6857ba05b3c9b2d5e33509fa to your computer and use it in GitHub Desktop.

Variables

Is that a Variable!?

ex: j, myVar, _inT

Back to _inT, this is a bad variable because it uses a reserved word, and had a leading underscore, which should only be used for device drivers, and lower level system development.

  • Another example of a bad variable is password$, this is because the $ indicates security.

  • @ is not a variable in Java, but may be a variable in other languages.

  • i j, k, m, n, p, q, r, x, y are temporary variables they should NOT be used for important variables.

  • i and j are incrementors and decrimentors.

Recap:

  • _ before a variable, ex: _var should be reserved for low level development.
  • $ before a variable, ex: $var should be reserved for security development.

Variable Declarations

Primative types in Java

Type Size in Bytes Range
byte 1 byte 128-127
short 2 bytes -32,768 to 32,767
char 2 bytes 0 to 65,536
int 4 bytes -2,147,483,648 to 2,147,483,647
float 4 bytes ±3.40282347E+38F
long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
double 8 byte ±1.79769313486231570E+308
boolean not defined true or false

Explicit value assignment

  • null A quantity whose specific value is undefined. the reserved word null is case sensitive.

  • String Is always denoted using double quotations (""). Two double quotations used without any intervening characters denotes null

  • Decimal (int) values - Is always denoted using double quotations (""). Two double quotations used without any intervening characters denotes null

  • double any unqualified value that contains a decimal point. 1234.56 is a rational number treated as double.

  • short instantiating a short must be done with respect to how the compiler treats the size of constant values.

  • float a decimal number that is immediately proceeded by the character lowercase 'f'. Is a rational number treated as floating-point.

  • char - a single symbol bounded by single quotation marks (''). Use of the numeric value zero as an initialization or an assignment to a char is convention that the character is null, even though it is not the null value. Note that two or more symbols in sequence consitutes a string.


Complex types in Java (defined by the comments //)

public class IntroToProg {
     int deen; // Declaration or instantiation.
     int deen = 0; // Declaration and initializatoin. 
     public static void main(String[] args) {
          deen = 0; // Assigning a instanted variable.
     }
}
  • Declaration Specifying a data type and a name without giving it a starting value.

  • Initialization A declaration using a starting value.

  • Assignment When a value is given to a variable after its declaration or initialization. DOES NOT HAVE A TYPE

    • Lvalue = Rvalue
  • Instantiate To create a variable or process and not initialize it.


teacher being annoying with number bases.

double example1 = 010.01; // This is equal to 10.01 in base 10.
double example2 = 0x12; // This is equal to 18 in base 10.

double example = 123f; // The jvm finna cast this.
  • final Declaring that an entity can only be assigned a value ones in a scope.

  • God Statement Declaring: mod type name value.

char digit = '0';
int x = digit - 48; // this int equals 0, you're subtracting by the ascii numeric representation of zero.

Conditions and Conditional Statements

Conditional Statements

  • if Changing Branching conditional, changes the program flow.

  • while Looping conditional, only interates while a condition is met.

  • do Statement behaves similarly to the while() loop in that it iterates until the Boolean condition is false, except that the contents of the do-while() are guaranteed to execute at least once.

  • switch Comparison of constant values.

  • ternary (cond) ? T : F ; This is the only condition that uses assignment.

  • for Iterative; repetition to to resolve a sequential outcome.

  • else Only executes if the condition isn't met, continues program flow.


Constructing an if statement

// if (conditional)
// for exmple `!(Conditional)` is, the negation of the condition l -> r
// `&&` 2 operands l -> r
// || (x && y ) || (z || p)

boolean condition = false; 

if (!condition) {
    return;
}
Level Operator Description Associativity
16 [] . () array element, object memebers, parentheses left to right
15 ++ -- unary post-increment, unary post-decrement not associative
14 ++ -- + - ! ~ unary pre-increment, unary pre-decrement, unary plus, unary minus, unary logical NOT, unary bitwise NOT right to left
13 () new cast, object creation righ to left
12 * / % multiplicative right to left
11 + - + additive, string concatentation left to right
10 << >> >>> shift left to right
9 < <= > >= instanceof relational not associative
8 == != equality left to right
7 & bitwise AND left to right
6 ^ bitwise XOR left to right
5 | bitwise OR left to right
4 && logical AND left to right
3 || logical OR left to right
2 ?: ternary right to left
1 = += -= *= /= %= &= ^= |= <<= >>= >>>= assignment right to left

Other stuff

int x = 4;
int p = 2

int y = x+++p--;

// y = 6 because (4 + 1) + (2-1) = 6 

// another example

int y = x+++p;

// the x operator has a higher level of associativity, and will result in y equating to 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment