Skip to content

Instantly share code, notes, and snippets.

@weakish
Created July 10, 2017 13:42
Show Gist options
  • Save weakish/1357200e7f2014a4d762b9fa343c39ee to your computer and use it in GitHub Desktop.
Save weakish/1357200e7f2014a4d762b9fa343c39ee to your computer and use it in GitHub Desktop.
#playground code on reading effective #java
import java.util.ArrayList;
class DefaultValueInstanceVariable
{
private int m;
private int n; // IntelliJ warns "private field is never assigned".
DefaultValueInstanceVariable(int x)
{
m = x;
}
void print()
{
System.out.println(m);
System.out.println(n);
}
}
class PromoteInt
{
PromoteInt()
{
byte one = 1;
byte two = 2;
int three = one + two;
Byte four = 4;
Byte five = 5;
Integer nine = four + five;
double ten = 10;
}
}
class SideEffect
{
private static int id(int x)
{
System.out.println(x);
return x;
}
static int f(int x)
{
id(0);
return id(x);
}
}
class ThisConstructor
{
int x;
ThisConstructor(int x)
{
this.x = x;
}
ThisConstructor(int x, int y)
{
this(x + y);
}
}
//public class ZeroOrOnePublicClass {}
class RemoveRange
{
public static void removeRange()
{
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
arrayList.add(5);
arrayList.add(6);
arrayList.subList(1, 4).add(10);
System.out.println(arrayList.subList(1, 4));
System.out.println(arrayList);
}
}
enum Singleton {
instance
}
class utility
{
public static void print()
{
System.out.println("hello");
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
DefaultValueInstanceVariable defaultValueInstanceVariable = new DefaultValueInstanceVariable(1);
defaultValueInstanceVariable.print();
SideEffect.f(2);
ThisConstructor thisConstructor = new ThisConstructor(1, 2);
System.out.println(thisConstructor.x);
RemoveRange.removeRange();
NutritionFacts cocaCola = NutritionFacts.build(240, 8).calories(100).sodium(35).carbohydrate(27).build();
System.out.println(Singleton.instance);
utility.print();
}
}
public class NutritionFacts
{
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Internal
{
// Parameters initialized to default values (if any)
private int servingSize; // Required; no default value
private int servings; // " " " "
private int calories = 0;
private int fat = 0;
private int sodium = 0;
private int carbohydrate = 0;
private Internal(int servingSize, int servings)
{
this.servingSize = servingSize;
this.servings = servings;
}
// Setters
public Internal calories(int val)
{
calories = val;
return this;
}
public Internal fat(int val)
{
fat = val;
return this;
}
public Internal sodium(int val)
{
sodium = val;
return this;
}
public Internal carbohydrate(int val)
{
carbohydrate = val;
return this;
}
public NutritionFacts build()
{
return new NutritionFacts(this);
}
}
private NutritionFacts(Internal internal)
{
servingSize = internal.servingSize;
servings = internal.servings;
calories = internal.calories;
fat = internal.fat;
sodium = internal.sodium;
carbohydrate = internal.carbohydrate;
}
public static Internal build(int servingSize, int servings)
{
Internal internal = new Internal(servingSize, servings);
return internal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment