Array 1 ม ต java int char doble string

A primitive data type specifies the size and type of variable values, and it has no additional methods.

There are eight primitive data types in Java:

Data Type Size Description`byte`1 byte Stores whole numbers from -128 to 127`short`2 bytes Stores whole numbers from -32,768 to 32,767`int`4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647`long`8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807`float`4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits`double`8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits`boolean`1 bit Stores true or false values`char`2 bytes Stores a single character/letter or ASCII values

In this article, we’ll look at how to convert a string to an array of characters in Java. I'll also briefly explain to you what strings, characters, and arrays are.

What is a Character in Java?

Characters are primitive datatypes. A character is a single character enclosed inside single quotation marks. It can be a letter, a digit, a punctuation mark, a space or something similar. For example: char firstVowel = 'a';

What is a String in Java?

Strings are objects (reference type). A string is made up of a string of characters. It's anything inside double quotation marks. For example: String vowels = "aeiou";

What is an Array in Java?

Arrays are fundamental data structures which can store fixed number of elements of the same data type in Java. For example, let's declare an array of characters: char[] vowelArray = {'a', 'e', 'i', 'o', 'u'};

Now, we have a basic understanding about what strings, characters, and arrays are.

Let's Convert a String to a Character Array

1. Use toCharArray() Instance Method

toCharArray() is an instance method of the String class. It returns a new character array based on the current string object.

Let's check out an example: // define a string String vowels = "aeiou"; // create an array of characters char[] vowelArray = vowels.toCharArray(); // print vowelArray System.out.println(Arrays.toString(vowelArray));

Output: [a, e, i, o, u]

When we convert a string to an array of characters, the length remains the same. Let's check the length of both String vowels = "aeiou";

0 and String vowels = "aeiou";

1 : System.out.println("Length of \'vowels\' is " + vowels.length()); System.out.println("Length of \'vowelArray\' is " + vowelArray.length);

Output: Length of 'vowels' is 5 Length of 'vowelArray' is 5

We can use various ways to print an array. I used the String vowels = "aeiou";

2 static method from the String vowels = "aeiou";

3 utility class.

You can read more about the toCharArray() instance method in the .

2. Use charAt() Instance Method

String vowels = "aeiou";

5 is an instance method of the String class. It returns a character at the specified index of the current string.

NOTE: a string is zero index based, similar to an array.

Let's see how we can convert a string to an array of characters using String vowels = "aeiou";

5 : // define a string String vowels = "aeiou"; // create an array of characters. Length is vowels' length char[] vowelArray = new char[vowels.length()]; // loop to iterate each characters in the 'vowels' string for (int i = 0; i < vowels.length(); i++) { // add each character to the character array vowelArray[i] = vowels.charAt(i); } // print the array System.out.println(Arrays.toString(vowelArray));

Output: [a, e, i, o, u]

You can read more about the String vowels = "aeiou";

5 instance method in the .

I just showed you another way of converting a string to a character array, but we can use the toCharArray() method to easily convert instead of creating loops and iterating them.

Please feel free to let me know if you have any suggestions or questions.

Photo by Alex Alvarez on Unsplash.

Please support freeCodeCamp in their Data Science Curriculum Pledge Drive.

Connect with me on Medium.

Thank you 😇

Happy Coding ❤️

More on Programming in Java

  1. Object-Oriented Programming Principles in Java: OOP Concepts for Beginners
  2. Java Array Methods – How to Print an Array in Java
  3. Java String to Int – How to Convert a String to an Integer
  4. Java Random Number Generator – How to Generate Integers With Math Random

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Toplist

โพสต์ล่าสุด

แท็ก

แปลภาษาไทย ไทยแปลอังกฤษ แปลภาษาอังกฤษเป็นไทย pantip โปรแกรม-แปล-ภาษา-อังกฤษ พร้อม-คำ-อ่าน อาจารย์ ตจต ศัพท์ทหาร ภาษาอังกฤษ pdf lmyour แปลภาษา ชขภใ ห่อหมกฮวกไปฝากป้าmv กรมพัฒนาฝีมือแรงงาน อบรมฟรี 2566 ขขขขบบบยข ่ส ศัพท์ทางทหาร military words หนังสือราชการ ตัวอย่าง หยน แปลบาลีเป็นไทย ไทยแปลอังกฤษ ประโยค การไฟฟ้านครหลวง การไฟฟ้าส่วนภูมิภาค ข้อสอบโอเน็ต ม.3 ออกเรื่องอะไรบ้าง พจนานุกรมศัพท์ทหาร เมอร์ซี่ อาร์สยาม ล่าสุด แปลภาษามลายู ยาวี Bahasa Thailand กรมพัฒนาฝีมือแรงงาน อบรมออนไลน์ การ์ดจอมือสอง ข้อสอบคณิตศาสตร์ พร้อมเฉลย คะแนน o-net โรงเรียน ค้นหา ประวัติ นามสกุล บทที่ 1 ที่มาและความสําคัญของปัญหา ร. ต จ แบบฝึกหัดเคมี ม.5 พร้อมเฉลย แปลภาษาอาหรับ-ไทย ใบรับรอง กรมพัฒนาฝีมือแรงงาน PEA Life login Terjemahan บบบย มือปราบผีพันธุ์ซาตาน ภาค2 สรุปการบริหารทรัพยากรมนุษย์ pdf สอบโอเน็ต ม.3 จําเป็นไหม เช็คยอดค่าไฟฟ้า แจ้งไฟฟ้าดับ แปลภาษา มาเลเซีย ไทย แผนที่ทวีปอเมริกาเหนือ ่้แปลภาษา Google Translate กระบวนการบริหารทรัพยากรมนุษย์ 8 ขั้นตอน ก่อนจะนิ่งก็ต้องกลิ้งมาก่อน เนื้อเพลง ข้อสอบโอเน็ตม.3 มีกี่ข้อ คะแนนโอเน็ต 65 ตม กรุงเทพ มีที่ไหนบ้าง