Found 9150 Articles for Object Oriented Programming

Java Program to convert string to byte

Shriansh Kumar
Updated on 01-Aug-2024 12:10:46

1K+ Views

Suppose you are given a String named "str". Now, your task is to write a Java program to convert the given string to Byte. String is a class in Java which stores sequence of characters within double quotes and Byte is a wrapper class of java.lang package which wraps value of byte datatype. Example Scenario: Input: String str = "65"; Output: res = 65 Using Byte.valueOf() Method The valueOf() method of Java Byte class is used to convert given string into its corresponding Byte object. It accepts a single numeric string as a parameter value and returns it as ... Read More

Java program to sort short array

Samual Sam
Updated on 06-Aug-2024 22:46:38

2K+ Views

In this article, we will learn to sort a short array in Java using the Arrays.sort() method. Begin by declaring and initializing an unsorted short array, then sort it and display the sorted array. Arrays.sort() method : This method sorts the elements of array in ascending order which have specified range and specified order. Syntax public static void sort(Object[] a, int fromIndex, int toIndex) Steps to sort short array Following are the steps to sort short array in Java − Declare and initialize an unsorted short array. ... Read More

Convert Short to numeric primitive data types in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:35:31

343 Views

To convert Short to numeric primitive data types, use the following methods −byteValue() shortValue() intValue() longValue() floatValue()Firstly, let us declare a Short.Short shortObj = new Short("40");Now, let us see how to convert it to long type, for a simple example.long val5 = shortObj.longValue(); System.out.println("Long: "+val5);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) {       Short shortObj = new Short("40");       byte val1 = shortObj.byteValue();       System.out.println("Byte: "+val1);       int val2 ... Read More

Java Program to convert String to short primitive

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

297 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

592 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

214 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

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

211 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

420 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

454 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

Advertisements