
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
What's the Kotlin equivalent of Java's String[]?
String is a collection which is implemented using String class. As per the Kotlin documentation, a string can be defined as follows −
Class String : Comparable<String>, CharSequence
In Kotlin, a string is a collection of characters. Strings are immutable in nature which means they are read-only. The length and elements of a string can be modified once declared.
In Java, we have an option to create an empty String array by defining it like String[]. In this article, we will see how we can achieve the same using Kotlin library function.
Example: Using arrayOf()
Kotlin library provides a function to create an array of different types of Strings. In this example, we will create an array of Strings using arrayOf().
Example
fun main(args: Array<String>) { var myEmptyStringArray = arrayOf<String>() println(myEmptyStringArray) }
Output
It will generate the following output −
[Ljava.lang.String;@4aa298b7
In the above piece of code, we have declared an empty array of Strings and named it as "myEmptyStringArray" and we have printed its contents. It generates the hash code of the memory location.
Example: Using arrayOfNulls()
arrayOfNulls() is another function which creates an array of empty Strings. In the following example, we will modify our previous example and we will create an array of empty Strings.
Example
fun main(args: Array<String>) { var myEmptyStringArray: Array<String?> = arrayOfNulls(3) println(myEmptyStringArray) }
Output
It will generate the following output −
[Ljava.lang.String;@4aa298b7
In the above piece of code, we have declared an empty array of Strings and named it as "myEmptyStringArray" and we have printed its contents. It generates the hash code of the memory location.
Example: Using emptyArray()
We can also use emptyArray() to create an empty array of Strings in Kotlin. In the following example, we will create an empty array of Strings using emptyArray().
Example
fun main(args: Array<String>) { var myEmptyStringArray: Array<String> = emptyArray() println(myEmptyStringArray) }
Output
It will generate the following output −
[Ljava.lang.String;@4aa298b7
- Related Articles
- Kotlin equivalent of Java's equalsIgnoreCase
- What is the Java equivalent to MySQL's smallint?
- What is the C# equivalent to Java's isInstance()?
- C# equivalent to Java's Thread.setDaemon?
- What is the PHP equivalent of MySQL's UNHEX()?
- What is the equivalent of Java static methods in Kotlin?
- C# Equivalent to Java's Double Brace Initialization?
- What is the equivalent of Java static final fields in Kotlin?
- What's the difference between "!!" and "?" in Kotlin?
- JavaScript equivalent of Python's zip function
- How to use Java's bitwise operators in Kotlin?
- Equivalent of Ruby's each cons in JavaScript
- What is the equivalent of Matlab's surf(x,y,z,c) in Matplotlib?
- Equivalent to matlab's imagesc in Matplotlib
- Sort an arrays of 0’s, 1’s and 2’s using Java
