Skip to content

Instantly share code, notes, and snippets.

@queckezz
Last active January 6, 2016 18:58
Show Gist options
  • Save queckezz/ea6ece1a809e1daa81b0 to your computer and use it in GitHub Desktop.
Save queckezz/ea6ece1a809e1daa81b0 to your computer and use it in GitHub Desktop.
Java Snippets

Data types

Why use wrapper classes?

http://stackoverflow.com/questions/3579035/why-are-there-wrapper-classes-in-java

  • So that a null value is possible
  • To include in a Collection
  • To treat generically / polymorphically as an Object along with other Objects

Quick notes

  • Always use Double over Float
  • Integer Wrapper Class is autoboxed when compared until from -128 to (and with) 127
Integer j1 = 2;
Integer j2 = 2;
System.out.println( j1 == j2 );   // true
Integer k1 = 127;
Integer k2 = 127;
System.out.println( k1 == k2 );   // true
Integer l1 = 128;
Integer l2 = 128;
System.out.println( l1 == l2 );   // false
Integer m1 = 1000;
Integer m2 = 1000;
System.out.println( m1 == m2 );   // false
  • convert primitive type to wrapper class, use WrapperClass.valueOf()
// implicit conversion
Integer x = 100;

// explicit conversion
Integer x = Integer.valueOf(100);
  • Convert wrapper class to primitive type
Integer x = Integer.valueOf(10);
// implicit conversion
int y = x;
// explicit conversion
int y = x.intValue();
  • Integer.parseInt(String) returns a primitive int
  • Integer.valueOf(String) return a Integer wrapper class
array.toArray(new Type[array.size()]);
// if Type = int
array.stream().mapToInt(i -> i).toArray();
private ArrayList<Type> type = new ArrayList<>();
Collections.sort(SortList, new Comparator<User>(){
public int compare(User p1, User p2) {
int flag = user0.getAge().compareTo(user1.getAge());
if (flag == 0) {
return user0.getName().compareTo(user1.getName());
} else {
return flag;
}
}
});
public static void main(String[] args) {
// TODO code application logic here
}
File file = new File("c:/temp/data.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
if(line.contains("password")){
System.out.println(line);
}
}
br.close();
fr.close();
Thread.sleep(1000);
text.split("\\W+");
Desktop.getDesktop().browse(new URI(url));
// If your string contains exactly one character the simplest way to convert it to a character is probably to call the charAt method:
char c = s.charAt(0);
public class VendingMachine {
int[] coins;
public VendingMachine(int[] coins) {
this.coins = coins;
}
public int[] getCoinsForExchange(int exchange) {
ArrayList<Integer> finalCoins = new ArrayList<>();
for(int i = 0; i < coins.length; i++) {
int coin = coins[i];
int diff = exchange / coin;
System.out.format("%s, %s\n", i, diff);
if (diff <= 1) {
for(int j = 0; j < diff; j++) {
finalCoins.add(coin);
exchange = exchange - coin;
}
}
}
return finalCoins.stream().mapToInt(i -> i).toArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment