Java Extra Notes
Java Extra Notes
edu/java/11cheatsheet/
https://www.jetbrains.com/idea/
Integers, Doubles, Casting (Ch. 1):
● Assignment Operator: “=”
● The largest acceptable integer is 231 – 1, that is, 2147483647
● binary ex:
○ 123 in binary = 1111011 = (1 × 26) + (1 × 25) + (1 × 24) + (1 × 23) + (0 × 22) + (1 × 21) + (1
× 2 0)
○ 4 in binary = 100 = (1 * 22) + (0 * 21) + (0 * 20)
● hexadeciamal digits = 0x, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f
○ a = 10, b = 11, c = 12, d = 13, e = 14, f = 15
● To perform an operation and specify the data type of the resulting value: you would put “(data
type)” in front of the operation// this is known as casting
○ ex: (int) 3.8 + 5 // this will result in 8
● You cannot declare a variable inside a method parameter/ inside a method
● IF you have an INTEGER method and it RETURNS a NEGATIVE NUMBER, you have exceeded
the data cap of the number, so you get an INTEGER ARITHMETIC OVERFLOW
●
Arithmetic Expressions, Pitfalls and Surprises , Declaring Variable Values,
Shortcuts (Chapter 2):
● You could say that you use PEMDAS to evaluate arithmetic expressions
● if a positive value / 0.0, it will equal Infinity
● if a negative value / 0.0, it will equal -Infinity
● if 0.0 / 0.0, it will equal Nan (Not a number)
● if a double value OR double cast (double) is present, the output will be a double
● Integer.MIN_VALUE = -2147483648
● Integer.MAX_VALUE = 2147483647
● evaluating or putting a cast on a float value, you will have a “loss of precision”
● IF you have 2 int’s in the the operation, they WILL ALWAYS RESOLVE to INT; no matter if you
divide it, multiply, etc. (SAME THING GOES FOR OTHER PRIMITIVE TYPES)
Strings, Converting Between Strings and Numbers (Chapter 3):
● If you want to check for a word in a String, use (String Name).contains
● When comparing strings, you use “.equals()” not “==”
● use “.compareTo()” to compare Strings
○ if a.compareTo(b):
■ it will give NEGATIVE if a comes before b in lexographical order
■ it will give NEUTRAL/0 if a and b are the same String
■ it will give POSITIVE if a comes after b
● Lexographical order includes Uppercase letters, a few punctuation marks, then lowercase letters
● “&&” uses lazy evaluation because if the first condition is false, the compiler won’t consider the
second condition
○ Don’t get tricked thinking that “||” uses that too because if the first condition is false, the
compiler has to look for a true in the second condition
● De Morgan’s Laws summarize the functions of “&&” and “||”
○ !(a && b) == (!a || !b) is true
○ !(a || b) == (!a && !b) is true
● you use “.IndexOf(“...”)” to find the first index that the specified substring is found in the String
● ex: m = p.substring(p.indexOf("V"),p.length());
○ It will find the index of the first instance of “V” in the String, find the substring from that
index to the end of the String, and make that substring the value of m.
○ You can use p.length in this situation because it is inclusive and will refer to all index that
comes before that point.
Booleans (Chapter 4):
● There are 6 different relational operators: <=, <, >=, >, !=, ==
● If you reference a specific index in a substring, you will also reference the rest of
the substring after that index.
○ String c = b.substring(1); // b = “Fred”
// c = “red”;
● A loop will only begin iterations when the deciding condition is true. Even if the
condition is met, if its false then it will not start
Arrays (Chapter 5):
● If an array is having its elements use the “.remove()”, watch for how the indexes
are moved over, taking up new positions
● You can't use "{"","",""}" to re-initialize a String array. You have to change each index at a time.
● For 2 dimensional array, syntax is (arrayname)[x][y];
x goes (1 left) from here, and
to the right
[1][0] [1][1]
● ex: If h is a 2-dimensional array, what expression evaluates to the value of the
element that is in the last column of the last row?
○ h[ h.length - 1 ][ h[ 0 ].length - 1 ]
● The syntax [x][y] REALLY MATTERS
● In:
String[] p = { "A", "B", "C", "D", "E" };
String[] q = { p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ], p[ 4 ] };
String[] r = p;
p[ 3 ] = "Z";
//p[3] = “Z”, q[3] = “D”, r[3] = “Z”;
● Your experimentation in Exercise 49 should have made you suspect the following:
When an array referenced by one variable is assigned to another variable, then both
variables reference the same array. In the code above, once the assignment y = x
has occurred, the situation may be depicted as follows:
Such a suspicion is well-founded. Under such circumstances, any changes made to
the elements of the array are "seen by both variables". In fact, x and y are said to be aliases
— they are two different labels for one and the same thing.
● If a change happens in y, and “y=x”, then the change will also happen x.
● To find the length of an array: (array name).length
○ To find the length of an ArrayList: (arraylist name).size
● To find the length of a String: (String name).length()
● ex: String[] b;
b = { "a", "b", "c" }; // illegal
b = new String[] { "a", "b", "c" }; // legal
int[] z = new int[] { 1, 2, 3, 4, 5 }; // legal
int[] z = { 1, 2, 3, 4, 5 }; // legal
● You can only use {...} to initialize data type arrays, not arrays of Strings and Objects
● For 2-dimen[sional arrays, “(array name).length” only finds the number of rows, not columns.
● When initializing a 2-dimensional array: int[ ] [ ] t = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
// you need 2 brackets on each side
● Different ways to make array:
○ int [] x = new int[10];
○ int[] x;
x = new int[10]
○ int[] x = {1,2,3,4,5};
○ int x [];//it works, but you can’t really do much with it.
● If you try to access an uninitialized index in an array or arraylist, it will print out “null” since it is the
default String value
○ Default array values: int arrays give 0, double arrays give 0.0, boolean arrays give false,
String arrays give “null”
● If you try to access an index that is outside the range of the array, you will get an
ArrayIndexOutOfBounds Error
● Giving a variable a value, and then changing that value later is called “overwriting”
● Vectors: one dimensional array(normal array); Matrix: 2-dimensional array
● ex: int[] b = { 5, 4, 3, 2, 1 };
int t = 3;
b[ t ] = b[ t - 1 ];
b[ t + 1 ] = b[ t ] - 1;
int y = b[ 4 ];
int x = b[ y - 1 ] + 1;// x = 5
● enchanced loop - loop created with “:” operator
○ The specified operation will apply to all the index in the array