I started Java Gold (Chapter 1-1)

Chapter 1 Java Class Design

-** 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

I started Java Gold (Chapter 1-1)
I took Java SE8 Gold.
What I learned with Java Gold
Effective Java Chapter 2
I started Ruby
I started Angular
Effective Java Chapter 6 34-35
Effective Java Chapter 4 15-22
Effective Java Chapter 3
Java Performance Chapter 1 Introduction
I started programming diary
I first touched Java ②
I first touched Java ③
I first touched Java ④
Java Gold Countermeasures: Format
I first touched Java
Java Performance Chapter 3 Java Performance Toolbox
Java8 Gold exam memorandum
Java Gold Countermeasures: Localization
Getting Started with Java Collection
What I researched about Java 6
[Java improvement case] I started programming after I was 30 years old.
On passing Java Gold SE 8
I made roulette in Java.
What I researched about Java 9
I investigated Java primitive types
Java Performance Chapter 2 Performance Testing Approach
I tried Drools (Java, InputStream)
I tried Rails beginner [Chapter 1]
What I researched about Java 7
I tried using Java REPL
Getting Started with Java Basics
[Java] I implemented the combination.
I tried Rails beginner [Chapter 2]
Java concurrency I don't understand
I studied the constructor (java)
I tried metaprogramming in Java
What I researched about Java 5
What I thought about when I started migrating from Java to Kotlin
I sent an email in Java
I compared PHP and Java constructors
I created a PDF in Java.
I made a shopify app @java
Oracle Certified Java Programmer, Gold SE 8
Story of passing Java Gold SE8
I checked Java Flight Recorder (JFR)
I fell into Java Silver (crying)
I tried to interact with Java
I tried UDP communication with Java
Effective Java 3rd Edition Chapter 5 Generics
I wrote Goldbach's theorem in java
I tried the Java framework "Quarkus"
I tried using Java8 Stream API
Effective Java 3rd Edition Chapter 8 Methods
I made an annotation in Java.
I tried using JWT in Java
I tried to summarize Java learning (1)
Getting started with Java lambda expressions
What I learned with Java Silver
What I researched about Java learning
I tried to summarize Java 8 now