Found 7442 Articles for Java

Copy characters from string into char Array in Java

karthikeya Boyini
Updated on 26-Jun-2020 16:50:47

1K+ Views

Let’s say we have the following string.String str = "Demo Text!";To copy some part of the above string, use the getChars() method.// copy characters from string into chArray =str.getChars( 0, 5, chArray, 0 );The following is an example to copy characters from string into char Array in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Demo Text!";       char chArray[] = new char[ 5 ];       System.out.println("String: "+str);       // copy characters from string into chArray       str.getChars( 0, 5, chArray, 0 ... Read More

Java Program to create Character Array from String Objects

Revathi Satya Kondra
Updated on 14-Jan-2025 03:34:28

351 Views

To create a character array from a string object, we convert each character of the string into elements of an array of type char. This is useful when we need to manipulate or access individual characters within the string. String Object: A string object is an instance of the String class representing a sequence of characters. It is immutable, which means once it is created, its value cannot be changed. Character Array: A character array is a data structure in Java that stores a sequence of characters. Creating Character Arrays from ... Read More

Java Program to get the Characters in a String as an Array of Bytes

Samual Sam
Updated on 26-Jun-2020 16:30:00

360 Views

To get the characters in a string as an array of bytes, use the getBytes() method.Let’s say we have the following string.String str = "Demo Text!";Convert it to array of bytes.byte[] arr = str.getBytes();Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Demo Text!";       // getting as an array of bytes       byte[] arr = str.getBytes();       // displaying each character as a Byte value       for(byte res: arr) {          System.out.println(res);       }    } }Output68 101 109 111 32 84 101 120 116 33

Convert byte to String in Java

Revathi Satya Kondra
Updated on 15-Jan-2025 19:00:11

1K+ Views

In Java, converting a byte array to a string means transforming the raw binary data (byte array) into a human-readable text format (String). To convert a byte array to a string, we can use the String constructor and a specified character encoding, such as UTF-8. Byte Array: A byte array is a sequence of bytes, each byte being an 8-bit value. String: A string is a sequence of characters, representing text. Converting a Byte array to a String in Java is quite easy. Let us learn the following methods: ... Read More

Java Program to convert int to Octal String

karthikeya Boyini
Updated on 26-Jun-2020 16:32:50

710 Views

The Integer.toOctalString() method in Java converts int to octal string.Let’s say the following are our integer values.int val1 = 9; int val2 = 20; int val3 = 2;Convert the above int values to octal string.Integer.toOctalString(val1); Integer.toOctalString(val2); Integer.toOctalString(val3);Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 9;       int val2 = 20;       int val3 = 30;       int val4 = 78;       int val5 = 2;       System.out.println("Converting integer "+val1+" to Octal String:       "+Integer.toOctalString(val1));       System.out.println("Converting ... Read More

Java Program to convert int to binary string

Samual Sam
Updated on 26-Jun-2020 16:33:49

5K+ Views

The Integer.toBinaryString() method in Java converts int to binary string.Let’s say the following are our integer values.int val1 = 9; int val2 = 20; int val3 = 2;Convert the above int values to binary string.Integer.toBinaryString(val1); Integer.toBinaryString(val2); Integer.toBinaryString(val3);Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 9;       int val2 = 20;       int val3 = 30;       int val4 = 78;       int val5 = 2;       System.out.println("Converting integer "+val1+" to Binary String: "+Integer.toBinaryString(val1));       System.out.println("Converting integer "+val2+" to ... Read More

Convert Integer to Hex String in Java

Revathi Satya Kondra
Updated on 14-Dec-2024 17:03:48

15K+ Views

In Java, converting an integer value to a hexadecimal (hex) string means transforming the number from its base-10 (decimal) format to base-16 format. This conversion uses digits 0-9 and letters A to F to represent the values. Integer: An integer is a whole number without having a fractional part, such as -2, -1, 0, 1, 2, etc. Hex String: A hexadecimal (hex) string represents a number in base-16, using digits 0-9 and letters A-F. We represent the A-F in numbers as 10 to 15. Example Let’s say the following are our integer values. int val1 = 5; int val2 ... Read More

Convert a String to a byte number in Java

Samual Sam
Updated on 26-Jun-2020 16:27:46

236 Views

Use the parseByte() method in Java to convert a String to a byte.Let’s say the following is our string −String str = “76”;Now, the parseByte() method converts the string to byte −byte val = Byte.parseByte(str);The following is an example −Example Live Demopublic class Demo {     public static void main(String[] args) throws Exception {        String str = "76";        byte val = Byte.parseByte(str);        System.out.println(val);     } }Output76

Convert string of time to time object in Java

karthikeya Boyini
Updated on 26-Jun-2020 15:31:06

10K+ Views

Here is our string.String strTime = "20:15:40";Now, use the DateFormat to set the format for date.DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");Parse the string of time to time object.Date d = dateFormat.parse(strTime);The following is the complete example.Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.text.SimpleDateFormat; public class Demo {     public static void main(String[] args) throws Exception {        String strTime = "20:15:40";        DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");        Date d = dateFormat.parse(strTime);        System.out.println("Resultant Date and Time = " + d);     } }OutputResultant Date and Time = Thu Jan 01 20:15:40 UTC 1970

Java Program to convert String to Integer using Integer.parseInt()

Samual Sam
Updated on 26-Jun-2020 15:31:54

267 Views

The Integer.parseInt() method in Java converts a string to an integer.Let’s say the following is our string −String str = "456";Converting it into integer −Integer.parseInt(str));The following is the complete example −Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "456";        System.out.println(Integer.parseInt(str));     } }Output456

Advertisements