Skip to content

Instantly share code, notes, and snippets.

@liang799
Last active August 14, 2022 10:16
Show Gist options
  • Save liang799/e296e8aa0304b0fc2265ac273e04eb2c to your computer and use it in GitHub Desktop.
Save liang799/e296e8aa0304b0fc2265ac273e04eb2c to your computer and use it in GitHub Desktop.
Java tips

Tips

Doesn't work

test += 1 + 1;

Java Doesn't throw integer exceptions

Integer.MAX_VALUE + 1;

Null pointer exception

String[] s = new String[3];
System.out.println(s[0].toString());

Infinity or error?

int result = 12 / 0; //ArithmeticException
// anything else will not throw an error, but have varying results such as NaN & Infinity

Local variables can't be static

Can't declare static var in main

public static void main(String args[]) { 
  static double f = 3; //error
} 

Singletons

  • Private Constructors
  • Enums
  • getInstance()
  • Should be thread safe

Anonymous Class

  • Inner Class without a name
  • Single object is created
  • Can be created from an interface

Abstract Classes

  • Can have variables

Overriding

  • Check class access modifier table
  • Cannot assign weaker access privileges
  • For this one, both protected and public works fine if have same method signature
class Test {
  double test(double x) {
    return x;
  }
}

class Testes extends Test {
  //will have compilation error, unless you change access modifier to public or protected
  @Override
  private double test(double x) { 
    x *= 10;
    return x;
  }
}

Void type

The only value we can assign to a Void variable is null. Hence, this is allowed

void useMe() {
  return null;
}

However, useMe() ** does not return a value**

TextArea vs Textfield

  • TextArea:
    • Multiple lines
    • Not resizable
    • Not scrollable
  • Textfield
    • Single line

Remember JavaFX button listeners

// this is an anonymous class
button.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        label.setText("Accepted");
    }
});

equal

    String s = "abc";
    if (s == "abc") {
      System.out.println("== works"); //bad practise but works
    }
    if (s.equals("abc")) {
      System.out.println("method works");
    }  

javafx set size

stage.setScene(scene2);
stage.setHeight(1000);
stage.setWidth(1000);

Difference between parseInt and valueOf in java

The API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int.

Unboxing

Boxable/Unboxable

Primitive type  Wrapper class
boolean         Boolean 
byte            Byte 
char            Character 
float           Float 
int             Integer 
long            Long 
short           Short 
double          Double

Border Layout

2022-08-14 18_15_23-A Visual Guide to Layout Managers (The Java™ Tutorials _ Creating a GUI With Swi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment