Found 9326 Articles for Object Oriented Programming

Java Program to create Character Array from String Objects

Samual Sam
Updated on 26-Jun-2020 16:56:31

219 Views

Use the toCharArray() method in Java to create character arrays from string objects. The following is our string.String str = "This is it!";Now, let us create character array from the above string.char[] strArr = str.toCharArray();Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "This is it!";       char[] strArr = str.toCharArray();       for(char res: strArr) {          System.out.println(res);       }    } }OutputT h i s i s i t !Let us see another example in which we are creating ... 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

235 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

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

729 Views

The valueOf() method is used in Java to convert byte to string.Let’s say we have the following byte value.byte val = 40;Converting the above byte to string.String.valueOf(val);Example Live Demopublic class Demo {    public static void main(String[] args) {       byte val = 40;       // converting       String str = String.valueOf(val);       System.out.println("String: "+str);    } }OutputString: 40

Java Program to convert int to Octal String

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

568 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

4K+ 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

karthikeya Boyini
Updated on 26-Jun-2020 16:34:42

12K+ Views

The Integer.toHexString() method in Java converts Integer to hex string.Let’s say the following are our integer values.int val1 = 5; int val2 = 7; int val3 = 13;Convert the above int values to hex string.Integer.toHexString(val1); Integer.toHexString(val2); Integer.toHexString(val3);Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 5;       int val2 = 7;       int val3 = 13;       int val4 = 23;       int val5 = 30;       System.out.println("Converting integer "+val1+" to Hex String: "+Integer.toHexString(val1));       System.out.println("Converting integer "+val2+" to ... Read More

Convert a String to a byte number in Java

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

158 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

8K+ 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

185 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

Java Program to convert a string into a numeric primitive type using Integer.valueOf()

karthikeya Boyini
Updated on 26-Jun-2020 15:33:54

448 Views

The Integer.valueOf() method is used in Java to convert a string into a numeric primitive type.Let’s say the following is our string.String str = "989";To convert it into Integer primitive type, use Integer.valueOf()Integer.valueOf(str)Let us see the complete example to convert a string to Integer primitive type.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "989";        System.out.println(Integer.valueOf(str));     } }Output989

Advertisements