Skip to content

Instantly share code, notes, and snippets.

@bbrother92
Last active July 8, 2024 03:21
Show Gist options
  • Save bbrother92/b845ae059bb0c05fd70c21bb3c6b6ede to your computer and use it in GitHub Desktop.
Save bbrother92/b845ae059bb0c05fd70c21bb3c6b6ede to your computer and use it in GitHub Desktop.
#cheatsheet #Java

Basic numeric types

char \u0000 16B (65535)
byte (-128 127) 
short 2B   (-32768 до 32767)
int 4B (-2147483648 до 2147483647)
long 64B (+- 9*10^18)
0154 octal
0b01101100 binary
3_000_000_000L long
float 4B
double 8B   double b = 0xFp //60.0 ; 
1e-6
0/0 0*Double.NEGATIVE_INFINITY = Nan 

Integer pool

Integer a = 128;
Integer b = 128;
System.out.println(a == b); //false
System.out.println(a.equals(b));//true
Integer a = 127;
Integer b = 127;
System.out.println(a == b); //true bcs VALUES ARE CACHED IN POOL [-128 and 127] 
Integer to string
int one = 11;
String st = String.valueOf(one);
String st2 = Integer.toString(one);
String to integer
int a = Integer.parseInt(string);
int b = Integer.valueOf(string);
Integer to int
Integer b = 3;
b.intValue();

Strings

System.identityHashCode(str) // одни в хипе другие в пуле

StringBuilder ( non sync, faster than StringBuffer )

String s = new StringBuffer().append("Это").append(" одна ").append("строка");
//----
StringBuilder sb = new StringBuilder("foobar");
System.out.println(sb.append("test"));
System.out.println(sb.delete(1,3)); //fbartest
sb.insert(0, "I");//Ifoobar
sb.deleteCharAt(sb.length()-1);//fooba
sb.reverse().reverse();//foobar
sb.indexOf("bar");//3
sb.charAt(3);//b

Casting

Downcasting
Animal animal = new Animal();
Dog notADog = (Dog) animal; // runtime check will fail ClassCastException
-----

Arrays

String[] st0 = new String[4];
String[] st = new String[]{"one", "two"};
//Arrays
int[]	c = Arrays.copyOf(a,7);
c = Arrays.copyOfRange(a,4,6); // like substring
Arrays.toString(intArray);
Arrays.asList(stringArray).contains("a");
System.arraycopy()
Arrays.binarySearch(a,4);
.Arrays.fill(tt , 17)
.deepToString()

int[] fibo = new int[] {1,1,2,3,5};
int[] fiboCopy = Arrays.copyOf(fibo,fibo.length);
ArrayList<Integer> fibo = new ArrayList<>();
fibo.add(1);
ArrayList<Integer> fiboCopy = new ArrayList<>(fibo);
fibo.set(0, 123);

//Converting array to ArrayList
Integer[] fibo = new Integer[] {1,1,2,3,5};
ArrayList<Integer> fiboCopy = new ArrayList<>(Arrays.asList(fibo));
//Converting ArrayList to array
ArrayList<Integer> fibo = new ArrayList<>();
Integer[] fiboCopy = fibo.toArray(new Integer[0]);
//populate ArrayList
Collections.nCopies(100,0);
Collections.emptyList();

Enums

enum Sexy {
	TOO("eloquent"), MUCH("relentless");
	private String feature;
	
	Sexy(String feature) {
		this.feature = feature;
	}
	
	public String invert() {
		return String.format("not %s",this.feature);
	}
}

Sexy too = Sexy.valueOf("MUCH");//String to Enum
String name = too.name(); //MUCH
int number = too.ordinal(); //1

Arrays.toString(Sexy.values()) //[TOO, MUCH]
System.out.println(too.invert()); //not eloquent
	
	
	
mtd((short) 2); // void mtd (short a) {}
short d = (short)(a+b)  ; // a, b - short



Arrays.deepToString();
Integer.toBinaryString(num)
Math.round(0.49999999999999999)
Random rn      = new Random();
System.out.println(rn.nextInt());

Scanner scn = new Scanner(System.in);
scn.nextLine();
scn.next();
scn.nextDouble();

double num = 0xFp3; // num is equal to 120.0 (15x2³)
double num = 1e2; // num is equal to 100.0 (1x10²)
double num = Double.parseDouble(str);
System.out.println(Long.MAX_VALUE);
String formatString = "We are printing double variable (%f), string ('%s') and integer variable (%d).";
System.out.println(String.format(formatString, 2.3, "habr", 10));









//Fun
you can use null as key or value in hashmap, or in ArrayList
((Main) null).funk();
System.out.println(' '+0); //32+0
Integer.toHexString((int)'Ё');
System.getProperty("java.version") // in java 9: not 1.9.0

new Box() {
@Override
void print(String s) {
super.print(s+"inside");
}
}.print("iam");

int c() [][] { return new int[0][]; }

Inheritance

class Parent {
}

class Child extends Parent implements MyInterface {
}

class OtherClass {
}
pr instanceof Child;// false
cd instanceof Parent;// true
cd instanceof MyInterface;// true
OtherClass.class.isAssignableFrom(Child.class) //checks if  OtherClass is superclass of child class/interface 
cd instanceof OtherClass;//compile error
null instanceof OtherClass;// false


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