Java
Java - Formatter
깔끔한 코드 작성에 String.format()의 필요를 느껴 이와 관련된 내용을 공부하고자 한다. String Docs https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html String (Java SE 17 & JDK 17) All Implemented Interfaces: Serializable, CharSequence, Comparable , Constable, ConstantDesc The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instan..
Java - Google Java Style Guide 정리
보다 깔끔한 코드를 위해 Google Java Style Guide를 정리하고자 한다. Google Java Style Guide는 Google의 Java 코딩 표준을 정리한 문서이다. https://google.github.io/styleguide/javaguide.html Google Java Style Guide 1 Introduction This document serves as the complete definition of Google's coding standards for source code in the Java™ Programming Language. A Java source file is described as being in Google Style if and only if it a..
Java - .of .asList .emptyList
List를 초기화하는 다양한 방법이 익숙하지 않아서 이에 대해 정리하고자 한다. List.of() List 인터페이스의 of 메서드를 활용해 List를 정의하는 방법으로 Java 9부터 활용할 수 있다. 문서를 확인해 보니 아래와 같이 여러 개로 오버로딩 되어있다. 무엇이 다른 것인지 코드를 직접 확인해 보니 인자의 개수에 따라 동작하는 코드가 달랐다. - 인자가 없는 경우 - 인자가 1개 or 2개인 경우 - 인자가 3개 이상인 경우 - 가변인자 코드 코드가 서로 다르지만 동일하게 ImmutableCollections 클래스를 활용한다. Immutable가 붙은 것을 보아 변경할 수 없다고을 유추할 수 있는데, 실제로 Immutablecollections 클래스를 찾아보니 List가 아니라 처음 보는 ..
Java - 생성자, 초기화 블록
생성자와 초기화 블록에 대해 알아보자. 생성자 생성자는 인스턴스가 생성될 때 호출되는 인스턴스 초기화 메서드이다. 인스턴스가 생성될 때 필요한 동작을 위해 활용한다. 생성자 구조 생성자는 클래스 이름과 같은 이름을 활용하며 반환값이 없다. 반환값이 없으므로 void만 올 수 있고 이를 생략한다. 아래 코드에서 Car() {...}가 생성자이다. public class ConstructorPractice { public static void main(String[] args) { Car car = new Car(); } } class Car { Car() { System.out.println("Hello world"); } } 이를 실행해 보면 Hello world가 출력된다. 인스턴스가 생성되고 생성자가..
Java - Java 변수, 메서드
Java의 변수와 메서드에 대해 알아보자. Java 변수 Java의 변수는 클래스변수, 인스턴스변수, 지역변수로 구분된다. 각 변수의 차이는 선언된 위치이다. 클래스변수 클래스 영역 클래스가 메모리에 올라갈 때 인스턴스변수 인스턴스가 생성될 때 지역변수 클래스 영역 이외의 영역 변수 선언문이 수행되었을 때 글로 보면 어려움이 있을 수 있는데 아래 코드로 보면 확실히 알 수 있다. public class VariablePractice { static int classVariable = 1; // 클래스변수 int instanceVariable = 2; // 인스턴스변수 public void method() { int localVariable = 3; // 지역변수 } } 각 변수를 하나씩 살펴보자. 클래스..
Java - enum
Java의 enum을 알면 효율적인 프로그래밍이 가능해 보여 이에 대해 알아보고자 한다. 열거형 선언 JDK 1.5부터 사용 가능한 기능이다. 상수를 편리하게 선언하기 위한 것으로 여러 상수를 정의할 때 유용하다. 정의는 아래와 같이 한다. 끝에 세미콜론(;)이 없는 것에 주의해야 한다. enum 열거형이름 {상수명1, 상수명2, ... } Enum 클래스 enum에 대해 자세히 알아보기 전에 모든 열거형의 조상인 Enum 클래스를 먼저 알아보자. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Enum.html Enum (Java SE 11 & JDK 11 ) docs.oracle.com Enum는 Serializable, ..
Java - Integer
Integer 클래스에 알고리듬 문제 풀기에 좋은 기능이 많은 것 같아 이에 대해 알아보고자 한다. Integer 클래스 Integer 클래스는 원시 타입 int를 클래스로 다룰 수 있도록 감싸주는 Wrapper 클래스 중 하나이다. Wrapper 클래스에 대해선 추후에 다루기로 하고 지금은 Integer 클래스가 가지고 있는 여러 기능을 알아보자. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html Integer (Java SE 11 & JDK 11 ) Returns the value obtained by rotating the two's complement binary representation of t..
Java - Collections unmodifiable
Java를 활용해 이것저것 만들다가 우연히 Collections 클래스의 unmodifiable 메서드를 알게 되었다. 자주 활용할 것 같아 간단히 정리하고자 한다. https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html Collections (Java Platform SE 8 ) Rotates the elements in the specified list by the specified distance. After calling this method, the element at index i will be the element previously at index (i - distance) mod list.size(), for all va..