-** switch statement **
The data types that can be used in the case statement
・ Byte ・ Char ・ Short ・ Int ・ Enum ・ String ・ Character ・ Byte ・ Short ・ Integer
The above basic data types and their wrapper classes. However, it seems that only enum, String, char, and int are actually used.
-** Extended Literal **
Decimal, octal, hexadecimal, binary, and underscore (_) can be used.
public class Sample {
public static void main(String[] args) {
int v1 = 10; //Decimal number
int v2 = 072; //8 base
int v3 = 0x1a; //Hexadecimal
int v4 = 0b1111; //Binary number
int v5 = 100_000; //To improve readability
}
}
Keep in mind that underscores cannot be used for literals ** beginning, ending, or before or after a symbol **.
-** Access modifiers and encapsulation **
Java has the following modifiers for defining the access range, so-called scope.
Modifier | Applicable place | Description |
---|---|---|
public | Classes, methods, variables, constructors | Can be used from any class |
protected | Methods, variables, constructors | Only the same package and inherited classes can be used |
Default (not specified) | Classes, methods, variables, constructors | Can only be used from the same package |
private | Methods, variables, constructors | Can only be used within the same class |
** About public **
Only one public class can be created in one file as shown below.
Sample.java
public class Sample { //This is OK
public static void main(String[] args) {
System.out.println("Sample");
}
}
public class Sample2 { //This will result in a compilation error
public static void main(String[] args) {
System.out.println("Sample2");
}
}
** About protected and private **
Due to the limitation that protected can only be used from the same package and subclasses It seems that it is customarily attached to "methods that you want to override".
It seems that private is often used for encapsulation. The purpose of the encapsulation seems to be to prevent the fields from being changed indiscriminately. After that, I think it would be nice to be able to easily understand where the value is set and where it is being acquired.
Is it like this when used as a sauce?
Sample.java
public class Sample {
public static void main(String[] args) {
Cat zakiyama = new Cat();
zakiyama.setName("Zakiyama");
System.out.println(zakiyama.getName());
}
}
abstract class Animal {
private String Name;
protected abstract String getName();
protected abstract void setName(String newName);
}
class Cat extends Animal {
private String Name;
@Override
protected String getName() {
return this.Name;
}
@Override
protected void setName(String newName) {
this.Name = newName;
}
}
-** final and static modifiers **
The final modifier can be applied to variables, methods and classes. When applied to each, the movement is as follows.
Applicable place | Description |
---|---|
class | そのclassを継承できなくなる |
Method | そのMethodをオーバーライドできなくなる |
variable | そのvariableの中身を変更できなくなる(定数化) |
** ・ About final ** One of the standard java classes, the String class is the final class. (See JavaDoc) When to add final, add it when inheritance is unnecessary. It is great to prevent the design from collapsing.
It's not really a class that inherits and extends If it is inherited, the role that it should have is meaningless.
The article here describes the merits of adding final, so it is quite helpful.
** ・ About static ** Variables and methods with static have different memory areas from instances. You can use the variables and methods without instantiating them.
Like this.
Sample.java
public class Sample {
public static void main(String[] args) {
Animal zakiyama1 = new Animal();
Animal zakiyama2 = new Animal();
zakiyama1.SetName("It's Zakiyama 1");
System.out.println(zakiyama1.getName());
System.out.println(zakiyama2.getName());
zakiyama2.SetName("It's Zakiyama 2");
System.out.println(zakiyama1.getName());
System.out.println(zakiyama2.getName());
}
}
class Animal {
private static String Name;
public String getName() {
return Animal.Name;
}
public void SetName(String newName) {
Animal.Name = newName;
}
}
Execution result
It's Zakiyama 1
It's Zakiyama 1
It's Zakiyama 2
It's Zakiyama 2
Because static variables are for classes It does not have the contents for each instance variable.
Therefore, if you change the variable in zakiyama1, the contents are changed when you call it in zakiyama2. If the contents of the variable are retained for each instance, the variable is not affected by multiple instances.
In the case of instance variables, it looks like this.
Sample.java
public class Sample {
public static void main(String[] args) {
Animal zakiyama1 = new Animal();
Animal zakiyama2 = new Animal();
zakiyama1.SetName("It's Zakiyama 1");
System.out.println(zakiyama1.getName());
System.out.println(zakiyama2.getName());
zakiyama2.SetName("It's Zakiyama 2");
System.out.println(zakiyama1.getName());
System.out.println(zakiyama2.getName());
}
}
class Animal {
private String Name;
public String getName() {
return this.Name;
}
public void SetName(String newName) {
this.Name = newName;
}
}
result
It's Zakiyama 1
null
It's Zakiyama 1
It's Zakiyama 2
More on that.
Recommended Posts