- "Java Is Everywhere"
staticfinalStringFAVORITE_CHANNEL="nelanLoves7652626"- Hosting: Where the data is housed
- Developing: How we make the data
- Database: Theory behind how we store our data
- Logic: How We process the data
- API: how we get our data
- UI: How We Present the data
- ==: compares content and reference
- .equals(): compares just the content
Class Naming is Pascal Case The case name is = to 2526 fav programming language and fav screen color:
classPascalCase{}publicvoidtheBestMethod()
{
logln("2526: 727225, 27736259, 27429, 27375, 746867 ARE THE BEST THINGS EVER and 557 AKA LLP Prog is also my fav!!!");
}- Integer
- Float
- Char
- Pointers
- Arrays
- List
- Linear:
- Stacks
- Queues
- Non-Linear:
- Graphs
- Trees
- Linear:
$javac*.java
$java<MAIN_CLASS>- This method uses ecj instead of javac
$~ java callingProgram.java MethodProgram.javaclassNelanLvsBDAndCSunAndFTN{
intcobolfb = 2626532;
intpascalfb= 72722532;
publicvoidsetVals(intcobolfb, intpascalfb){
/*here is where the this keyword comes to play to tell java that I want to use the parameter of my function aka local variables and not the instance variables(up top) */this.cobolfb = cobolfb;
this.pascalfb = pascalfb;
}
}Raised when you try to call an undeclared variable
publicclassOmar{
publicstaticvoidmain(String [] args)
{
inta = 1;
intb= 2;
intc= 3;
mean = (a+b+c)/2;
System.out.println(mean);
}
}In line 8 we try to print to the console mean we have set the value of mean but we never declared it To solve we do this
publicclassOmar{
publicstaticvoidmain(String [] args)
{
inta = 1;
intb= 2;
intc= 3;
doublemean = (a+b+c)/2;
System.out.println(mean);
}
}Raised when you try to call an undeclared variable
publicclassGreat{
publicstaticvoidmain(String [] args)
{
the_best_method;
}
publicstaticvoidthe_best_method()
{
System.out.println("This is the best method in the world");
}
}In line 4 we are incorrectly calling the_best_method but we forget the parenthesis. To fix this error I must place the open and close parenthesis
publicclassGreat{
publicstaticvoidmain(String [] args)
{
the_best_method();
}
publicstaticvoidthe_best_method()
{
System.out.println("This is the best method in the world");
}
}Raised when you are using the scanner
publicclassGreat{
publicstaticvoidmain(String [] args)
{
ScanneruseInput= newScanner(); // scanner is not importedintl = useInput.nextInt();
}
}In line 4 we are using the scanner but we never imported the library that enables us to use it
importjava.util.Scanner;
publicclassGreat{
publicstaticvoidmain(String [] args)
{
ScanneruseInput= newScanner(); // scanner has no default constructorintl = useInput.nextInt();
}
}publicclassThebest
{
publicstaticvoidmain(String[] args) {
System.out.println("Hello, world!");
}
}SOOO, I save the file and I name it Lemon.java well, it will error because our class is Thebest so that means our file name should be Thebest.java
This error is raised when I try to write code outside of a method which is unintentionally done.
publicclassTest { System.out.println("Hello!");
publicstaticvoidmain(String[] args) {
System.out.println("World!");
}
}To fix I just place the print Statement of hello inside of main
publicclassTest {
publicstaticvoidmain(String[] args) {
System.out.println("Hello!");
System.out.println("World!");
}
}An "illegal start of expression" error occurs when the compiler when we start a expression before closing the previous one.
publicclassTest {
publicstaticvoidmain(String[] args) {
my_method();
publicstaticvoidmy_method() {
System.out.println("Hello, world!");
}
} To fix this piece of code, I simply add a closing curly brace for the main method. To know we are doing the right thing, just look at the lines of code before the error, there may be a missing closing paranthesis or a missing closing curly brace. This would give us what the error is.
publicclassTest
{
publicstaticvoidmain(String[] args)
{
my_method();
}
publicstaticvoidmy_method()
{
System.out.println("Hello, EVERYONEEEE!");
}
} The incompatible types error is raised when we are facing with data type errors. We can overcome this, by converting say a char to an int. We can convert a double to an integer with typecasting. BUt WE CANNOT convert between primitive types and objects. A primitive type is say a: null, undefined, boolean, number, string or char. However objects can be: Arrays, Maps, Sets, Functions, Regular Expression or Date..
publicclassTest
{
publicstaticvoidmain(String[] args)
{
intnum = "Hello, world!";
}
}The above code is an error because we are assigning the string Hello World to the variable num of type int. To fix this error I must put a integer within the quotes and use the ParseInt method. This is not a syntax error but a logical error. Step 1: Change the String value from Hello, world! to 500
publicclassTest
{
publicstaticvoidmain(String[] args)
{
intnum = "500";
}
}Step 2: Use parsing to convert the string to an integer
publicclassTest
{
publicstaticvoidmain(String[] args)
{
intnum = Integer.parseInt("500");
}
}Every method in Java requires that you explicitly state the return type of the method. Even methods that do not return a value must explicitly say void in the method signature, just as the main method does. When a method declaration does not contain a return type, this error will occur:
publicclassTest
{
publicstaticvoidmain(String[] args)
{
intx = getValue();
System.out.println(x);
}
publicstaticgetValue()
{
return10;
}
}
To fix this, simply insert the appropriate return type in the method signature and the error will go away:
publicclassTest
{
publicstaticvoidmain(String[] args)
{
intx = getValue();
System.out.println(x);
}
publicstaticintgetValue()
{
return10;
}
}An ArrayIndexOutOfBoundsException is thrown when an attempt is made to access an index in an array that is not valid. This means that say an array has 8 elements and we know that the number of elements in index is 7. We start counting at 0. So, if I enter a value of 8 or greater to access, this will raise an error.
publicclassTest {
publicstaticvoidmain(String[] args) {
int[] arr = {1, 2, 3};
for (inti = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}
}
}The code above errored due to the for loop iteration settings. The first element is index 0 which is fine however, the function's output of arr.length of our array named arr of type int is 3. However, we are using the comparison operator of <=. This means less than or equal to. If, we change it to < it will not error. The equal means it will try to access index 3 which is the 4th item in the array which we do not have.
publicclassTest {
publicstaticvoidmain(String[] args) {
int[] arr = {1, 2, 3};
for (inti = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}The exception StringIndexOutOfBoundsException is thrown to the console when an attempt is made to access an index in the String that is not valid. The only valid index of the String we can access is from 0 to the (length of the String-1). This means that if the array 8 elements. The biggest number I can access is 7 not 8. If we enter any number greater than 7 for access will throws an outofBoundsException. This is an error in runtime not compile-time. It is accepted by the compiler because it is a logical error
publicclassTest {
publicstaticvoidmain(String[] args)
{
Stringstr = "Hello, world!";
Stringa = str.substring(-1, 3);
Stringb = str.charAt(str.length());
Stringc = str.substring(0, 20);
}
}To fix this I simply change the String a declaration in line 7 from index -1 to 1. Then another error, will raise because str.length output is 13. The last index we can access is 12. Then, we must change the datatype of b because the operation will output a character not a string. The fourth change we must make is change 20. The biggest index we can access is 12. Therefore the bottom code is bug free
publicclassTest
{
publicstaticvoidmain(String[] args)
{
Stringstr = "Hello, world!";
Stringa = str.substring(1, 3);
charb = str.charAt((str.length())-1);
Stringc = str.substring(0, 6);
}
}publicclassOmar { publicstaticvoidmain(String[] args) {
omarMethod(1.0, 2, "Hello!");
}
publicstaticvoidomarMethod(doublea, Stringb, intc) {
System.out.println(a + " " + b + " " + c);
}
}
This errors because I have called the methods with the specified data types in the wrong order. I must call it in the right order
publicclassOmar
{
publicstaticvoidmain(String[] args) {
omarMethod(1.0,"YOLO!", 2);
}
publicstaticvoidomarMethod(doublea, Stringb, intc) {
System.out.println(a + " " + b + " " + c);
}
}publicclassOmar
{
publicstaticvoidmain(String[] args)
{
intx = doubleMyNum(5);
System.out.println(x);
}
publicstaticintdoubleMyNum(intm)
{
intvalue = 2 * m;
}
}The above code errors because I have made the function behave like a void but my 3rd keyword indicates my return type should be of type int. To fix this, after storing the computation in a variable. I use the return keyword to return to the console. The output of the computation performed by the method.
publicclassOmar
{
publicstaticvoidmain(String[] args)
{
intx = doubleMyNum(5);
System.out.println(x);
}
publicstaticintdoubleMyNum(intm)
{
intvalue = 2 * m;
returnvalue;
}
}publicclassOmar
{
publicstaticvoidmain(String[] args)
{
intx = myAwesomeAbsVal(-5);
System.out.println(x);
}
publicstaticintmyAwesomeAbsVal(intm)
{
if(m<0)
{
return -m;
}
if(m>0)
{
returnm;
}
}
}The above lines of code have an error in logic. We should switch the code to this:
publicclassOmar
{
publicstaticvoidmain(String[] args)
{
intx = myAwesomeAbsVal(-5);
System.out.println(x);
}
publicstaticintmyAwesomeAbsVal(intm)
{
if(m<0)
{
return -m;
}
else
{
returnm;
}
}
}publicclassOmar
{
publicstaticvoidmain(String[] args)
{
inttheAwesomePi = 3.14159;
System.out.println("The value of pi is: " + theAwesomePi);
}
}There is an error above being raised being we are store double in an integer. An integer can only store 4 4 bytes in main memory. The value we are storing in it is a double which has a memory size of 8 bytes. The way to solve this issue. We will explictly cast the variable theAwesomePi to an int.
publicclassOmar
{
publicstaticvoidmain(String[] args)
{
inttheAwesomePi = (int)3.14159;
System.out.println("The value of pi is: " + theAwesomePi);
}
}publicclassOmar
{
publicstaticvoidmain(String[] args)
{
myWonderfulMethod();
}
publicstaticvoidmyWonderfulMethod()
{
System.out.println("How Awesome do you think my Method is?");
}There is an error above being raised being we are not properly closing our class. To solve this issue we add a closing curly brace. After, the closing curly brace of my method.
publicclassOmar
{
publicstaticvoidmain(String[] args)
{
myWonderfulMethod();
}
publicstaticvoidmyWonderfulMethod()
{
System.out.println("How Awesome do you think my Method is?");
}
}An "unreachable statement" error takes place when the compiler sees that it is impossible to reacha a certain statement. This is caused by the following code.
publicclassOmar
{
publicstaticvoidmain(String[] args)
{
inttheAwesomeNum = doubleMe(5);
System.out.println(theAwesomeNum);
}
publicstaticintdoubleMe(inta)
{
intdoubleMe = 2 * a;
returndoubleMe;
System.out.println("Returning " + doubleMe);
}
}The compiler will generate a number of errors. The first one to be listed is that it is unable to reach the print statement. This is because whenever we create a method and use the keyword return the compiler says you are done with the method therefore, we can exit out of the method and execute the next line of code. To fix this error I simply reverse the order of the print statement and the return statement.
publicclassOmar
{
publicstaticvoidmain(String[] args)
{
inttheAwesomeNum = doubleMe(5);
System.out.println(theAwesomeNum);
}
publicstaticintdoubleMe(inta)
{
intdoubleMe = 2 * a;
System.out.println("Returning " + doubleMe);
returndoubleMe;
}
}An variable might not have been initialized error is triggered when we declare a variable and specify its type but never give it an initial value;
publicclassOmar
{
publicstaticvoidmain(String[] args) {
intmyNum = 16;
intmyNum2;
System.out.println(myNum + myNum2);
}
} The compiler will generate the error variable myNum2 might not have been initialized because we declared it with the specified data type but never gave it an initial value. To solve this, I simply give it an initial value.
publicclassOmar
{
publicstaticvoidmain(String[] args)
{
intmyNum = 16;
intmyNum2=3;
System.out.println(myNum + myNum2);
}
} Invoke the super method in your subclass constructor. super()
publicclassOmar
{
publicvoidlogLn(objecto){
System.out.println(o);
}
publicstaticvoidmain(String[] args)
{
intmyNum = 16;
intmyNum2=3;
logLn(myNum + myNum2);
}
}I am getting this error because logLn should me a static method
publicclassOmar
{
publicstaticvoidlogLn(objecto){
System.out.println(o);
}
publicstaticvoidmain(String[] args)
{
intmyNum = 16;
intmyNum2=3;
logLn(myNum + myNum2);
}
}- Boolean: boolean ..... 1 bit
- Byte: byte ..... 1 byte = 8bits
- Character: char ..... 2 bytes = 16bits
- Short: short ..... 2 bytes = 16bits
- Integer: int ..... 4 bytes = 32bits
- Float: float ..... 4 bytes = 32bits
- Double: double ..... 8 bytes = 32bits
- Long: long ..... 8 bytes = 32bits
- Thanks to annotations I shorten commented out code to the next code
/*private int luckyNum = 3532;public int getLuckyNum() { return luckyNum;} public void setLuckyNum(int luckyNum) { this.luckyNum = luckyNum;}*/@Getter@SetterprivateintluckyNum = 3532;@ToString(exclude="f")
publicclassExample@EqualsAndHashCode(
exclude={"id1", "id2"})
publicclassExample {
}