9cubed
ブログ | Tailwind | Vite | Python | Node.js | Linux | PowerShell | その他 | 将棋ウォーズ | 歌の練習
< 前の記事

Kotlin - IntRange(..ドット2つ)

次の記事 >

Kotlin - for、forEach

Kotlin

Kotlin - Null合体演算子(エルビス演算子)

公開日:2019-11-06
更新日:2019-11-06

1. 概要

Null合体演算子( ?: )を使うと、変数が null だった時のデフォルト値を指定できます。


2. Null合体演算子( ?: )

var s:String? = null
println(s ?: "Default") // Default

s = "test"
println(s ?: "Default") // test

null の際に例外をスローすることもできます。
var s1: String? = null
println(s1 ?: throw NullPointerException())

null の際に無名関数を実行することもできます。
var n = 10
var s1:String? = null
var s2 = s1 ?: {
    val s = (1..n).joinToString(",")
    s  // 戻り値(return不要)
}()
println(s2) // 1,2,3,4,5,6,7,8,9,10

< 前の記事

Kotlin - IntRange(..ドット2つ)

次の記事 >

Kotlin - for、forEach

YouTube X

新着一覧

  • SCSS のインストールVite
  • Tailwind CSS のプロジェクトの作成Tailwind
  • TypeScriptのプロジェクトの作成Vite
  • Flask のインストールと動作確認Python
  • 簡易Webサーバーの作成Python
  • pipeline で文章の生成Python
  • pipeline で文章の要約Python
  • 音声から文字起こしPython
  • Node.js のインストールNode.js
  • .ps1(PowerShellスクリプト)を実行可能にするPowerShell

アーカイブ

  • 2025/12
  • 2025/11
  • 2025/10
  • 2025/09
  • 2025/08

以前のカテゴリー一覧

  • CakePHP3
  • CentOS7
  • HTML・CSS・JavaScript
  • Haskell
  • JavaScript
  • Kotlin
  • Laravel5
  • PHP
  • Python
  • Ruby
  • RubyOnRails5
  • TypeScript
  • Vue.js
  • Webサーバ講座
  • Webプログラミング講座
  • jQuery
  • linux
  • パソコン講座
  • ブログ
  • プログラミング講座
  • メモ帳作成講座
  • 数学

Copyright © 9cubed. All Rights Reserved.

プライバシーポリシー 利用規約
▲