Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 263 of 589
Convert Hex String to byte Array in Java
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.Examplepublic class Demo { public static void main(String args[]) { String str = "p"; ...
Read MoreJava Program to convert byte[] array to String
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.Examplepublic 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!
Read MoreJava Program to convert String to byte array
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 −Examplepublic 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
Read MoreConvert Short into String in Java
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.Examplepublic 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
Read MoreConvert short primitive type to Short object in Java
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.Examplepublic class Demo { public static void main(String []args) { short val = 30; Short myShort = new Short(val); System.out.println(myShort); } }Output30
Read MoreConvert byte primitive type to Byte object in Java
To convert byte primitive type to Byte object, use the Byte constructor.Here is our byte primitive type.byte b = 100;Now let us convert it to Byte object.Byte res = new Byte(b);Let us see the complete example.Examplepublic class Demo { public static void main(String[] args) { byte b = 100; Byte res = new Byte(b); System.out.println(res); } }Output100
Read MoreJava Program to display Bit manipulation in Integer
Let’s say we have the following decimal number, which is binary 100100110.int dec = 294;To perform bit manipulation, let us count the number of 1 bits in it. We have used bitCount() method for this purpose.Integer.bitCount(dec);The following is an example to display bit manipulation in given Integer.Examplepublic class Demo { public static void main(String []args) { // binary 100100110 int dec = 294; System.out.println("Count of one bits = " + Integer.bitCount(dec)); } }OutputCount of one bits = 4
Read MoreDisplay default initial values of DataTypes in Java
To display default initial values of a datatype, you need to just declare a variable of the same datatype and display it.The following is a Java program to display initial values of DataTypes.Examplepublic class Demo { boolean t; byte b; short s; int i; long l; float f; double d; void Display() { System.out.println("boolean (Initial Value) = " + t); System.out.println("byte (Initial Value) = " + b); System.out.println("short (Initial Value) = " + s); System.out.println("int (Initial Value) = " + ...
Read MoreBoolean Literals in Java
The Boolean literals have two values i.e. True and False.The following is an example to display Boolean Literals.Examplepublic class Demo { public static void main(String[] args) { System.out.println("Boolean Literals"); boolean one = true; System.out.println(one); one = false; System.out.println(one); } }OutputBoolean Literals true falseIn the above program, we have declared a boolean value and assigned the boolean literal “true”.boolean one = true;In the same way, we have added another boolean literal “false”.one = false;Both of the above values are displayed, which are the boolean literals.
Read MoreConvert Java String Object to Boolean Object
A string object can be created in Java using the string literal.String myStr = “Amit”;Another way to create a string object is using the new keyword.String myStr = new String("Hello!");We have used the first method to create a string object.String str = "true";Now, use the valueOf() method to convert String Object to Boolean Object. We have used this method on Boolean object.Boolean bool = Boolean.valueOf(str);Let us now see the complete example to show how to convert String Object to Boolean Object.Examplepublic class Demo { public static void main(String[] args) { String str = "true"; ...
Read More