Skip to content

Instantly share code, notes, and snippets.

class Parent {
public void angry() {
System.out.println("こら");
}
}
class Child extends Parent{
public void sorry() {
System.out.println("ごめん");
}
class Parent {
public void angry() {
System.out.println("こら");
}
}
class Child extends Parent{
public void sorry() {
System.out.println("ごめん");
}
class Parent {
public void angry() {
System.out.println("こら");
}
}
class Child extends Parent{
public void sorry() {
System.out.println("ごめん");
}
public static void main(String[] a) { // エントリーポイント
}
public static void Main(String[] args){ // MainはNG,mainであること
}
public static void main(int[] args){ // 単なるオーバーロードなのでコンパイルは通るがエントリーポイントではない
}
public class Main {
static int i;
static boolean flag;
static short s;
static float f;
static double d;
static long l;
static char c;
static byte b;
public static void main(String[] args) {
public class Main {
static char i = 65535; // これはOK
// static char j = 65536; // これはコンパイルエラーなのでコメントする
static int k = 100000; // int型
public static void main(String[] args) {
int x = i; // char型からint型に暗黙変換できる
System.out.println(x); // 65535となる
System.out.println(k); // 100000となる
char xx = (char)k; // int型からchar型に変換する場合は明示的にキャストする
System.out.println(xx); // 蚠となる
public class Main {
static float f;
static double d;
public static void main(String[] args) {
f = 3.0f; // fかFが必須、省くとコンパイルエラー
d = 4.0; // dかDは必須ではない
}
}
public class Employee {
private int age;
private String name;
public Employee(int age, String name) {
this.age = age;
this.name = name;
}
public boolean equals(Object obj) {
if(obj == null) return false; // パラメータがnullの場合はfalseを返すのがequalsメソッドの条件
return this.name == ((Employee)obj).name; // nameだけ同値ならtrueとしている
public class Main {
public static void main(String[] args) {
Employee emp1 = new Employee(10, "takahashi");
Employee emp2 = new Employee(20, "takahashi");
System.out.println(emp1.equals(emp2)); // nameだけで同値なのでtrueとなる
}
}
public class Main {
public static void main(String[] args) {
String str1 = "aa";
String str2 = "aa"; // 使いまわすため同一インスタンスとなる
System.out.println(str1 == str2); // 同一のため、true
System.out.println(str1.equals(str2)); // 同値のため、true
}
}