Found 9326 Articles for Object Oriented Programming

Java Program to convert String to short primitive

Samual Sam
Updated on 26-Jun-2020 10:21:58

223 Views

Use the parseShort() method to convert String to short primitive. It parses the string argument as a signed short.Firstly, we have set a string.String str = "99";Now, we have taken a short primitive and included the string value.short val = Short.parseShort(str);The following is an example to convert String to short primitive.Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "99";       short val = Short.parseShort(str);       System.out.println(val);    } }Output99

Convert short primitive type to Short object in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:22:19

431 Views

Let us first create a short primitive type and assign a value.short val = 30;Now, create a Short object and set the above short type to it.Short myShort = new Short(val);The following is the complete example to convert short primitive type to Short object using Short constructor.Example Live Demopublic class Demo {    public static void main(String []args) {       short val = 30;       Short myShort = new Short(val);       System.out.println(myShort);    } }Output30

Convert Short into String in Java

Samual Sam
Updated on 26-Jun-2020 10:22:46

141 Views

Using toString() method to convert Short to string. Firstly, let us take a short primitive datatype and initialize a short value.short myShort = 55;Now let us include this value to the following Short object.Short s = new Short(myShort);Convert the above given Short to string using the toString() method as shown in the complete example below.Example Live Demopublic class Demo {    public static void main(String []args) {       short myShort = 55;       Short s = new Short(myShort);       System.out.println("Short: "+s);       String myStr = s.toString();       System.out.println("String: "+myStr);    } }OutputShort: 55 String: 55

Convert Java String to Short in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:23:09

5K+ Views

Use valueOf() method to convert a String in Java to Short.Let us take a string.The following is an example −String myStr = "5";Now take Short object and use the valueOf() method. The argument should be the string we declared above.The following is an example.Short myShort = Short.valueOf(myStr);Let us now see the entire example to learn how to convert a string in Java to Short.Example Live Demopublic class Demo {    public static void main(String []args) {       String myStr = "5";       System.out.println("String: "+myStr);       Short myShort = Short.valueOf(myStr);       System.out.println("Short: "+myShort);    } }OutputString: 5 Short: 5

Java Program to compare Two Java short Arrays

Samual Sam
Updated on 26-Jun-2020 10:23:36

121 Views

To compare two Java short arrays, use the Arrays.equals() method.Let us first declare and initialize some short arrays.short[] arr1 = new short[] { 20, 15, 35, 55, 69 }; short[] arr2 = new short[] { 20, 15, 35, 55, 69 }; short[] arr3 = new short[] { 22, 19, 30, 45, 78 };Now let us compare any two of the above arrays.Arrays.equals(arr1, arr2));In the same way, work it for other arrays and compare themExample Live Demoimport java.util.*; public class Demo {    public static void main(String []args) {       short[] arr1 = new short[] { 20, 15, 35, 55, ... Read More

Convert Byte to numeric primitive data types in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:24:07

309 Views

To convert Byte to numeric primitive data types, use the following methods −byteValue() shortValue() intValue() longValue() floatValue()Firstly, let us declare a Byte.Byte byteVal = new Byte("35");Now, let us see how to convert it to long type, for a simple example.long longVal = byteVal.longValue(); System.out.println(longVal);In the same way, you can convert it to other primitive data types as shown in the complete example below −Example Live Demopublic class Demo {    public static void main(String args[]) {       // byte       Byte byteVal = new Byte("35");       byte b = byteVal.byteValue();       System.out.println(b);   ... Read More

Java Program to convert String to byte array

Samual Sam
Updated on 26-Jun-2020 10:24:35

307 Views

Here is our string.String str = "Asia is a continent!";Now let us use a byte array and the getBytes() method to fulfill our purpose.byte[] byteVal = str.getBytes();Now, if we will get the length of the array, it would return the length as shown in the complete example below −Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = "Asia is a continent!";       System.out.println(str);       // converted to byte array       byte[] byteVal = str.getBytes();       // getting the length       System.out.println(byteVal.length);    } }OutputAsia is a continent! 20

Java Program to convert byte[] array to String

karthikeya Boyini
Updated on 26-Jun-2020 10:25:04

783 Views

To convert byte[] to String, firstly let us declare and initialize a byte array.// byte array byte[] arr = new byte[] {78, 79, 33};Now take a String and include the array in it.String str = new String(arr);Let us see the complete example to convert byte array to String.Example Live Demopublic class Demo {    public static void main(String args[]) {       // byte array       byte[] arr = new byte[] {78, 79, 33};       String str = new String(arr);       // string       System.out.println("String = "+str);    } }OutputString = NO!

Convert Hex String to byte Array in Java

Samual Sam
Updated on 26-Jun-2020 10:25:38

11K+ Views

To convert hex string to byte array, you need to first get the length of the given string and include it while creating a new byte array.byte[] val = new byte[str.length() / 2];Now, take a for loop until the length of the byte array.for (int i = 0; i < val.length; i++) {    int index = i * 2;    int j = Integer.parseInt(str.substring(index, index + 2), 16);    val[i] = (byte) j; }Let us see the complete example.Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = "p";     ... Read More

Check whether the entered value is a digit or not in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:26:16

3K+ Views

To check whether the entered value is a digit or not in Java, use the Character.isDigit() method.We have a character to be checked.char val = '5';Now let us use the Character.isDigit() method.if (Character.isDigit(val)) {    System.out.println("Character is a digit!"); } else {    System.out.println("Character is not a digit!"); }Let us see the complete example now to check for Uppercase in Java.Example Live Demopublic class Demo {    public static void main(String []args) {       System.out.println("Checking for digit...");       char val = '5';       System.out.println("Value: "+val);       if (Character.isDigit(val)) {         ... Read More

Advertisements