Convert Byte to String in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:21:30

557 Views

In Java, there are different ways to convert byte to string.Using toString() method, you can easily convert byte to string as shown below −Example Live Demopublic class Demo {    public static void main(String[] args) {       byte res = 87;       // byte to string       System.out.println(Byte.toString(res));    } }Output87In the above example, we have taken a byte value.byte res = 87;With that, to convert to string, the method toString() is used as shown below −Byte.toString(res);Let us see another example to convert byte to string.Example Live Demopublic class Demo {    public static void main(String[] ... Read More

Compare Two Byte Arrays in Java

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

303 Views

To compare two Byte Arrays, use the Arrays.equals() method. Here we have declared and initialized a total of 4 arrays.byte[] arr1 = new byte[] { 11, 13, 30, 45, 77, 89 }; byte[] arr2 = new byte[] { 12, 13, 34, 87, 99, 33}; byte[] arr3 = new byte[] { 11, 13, 30, 45, 77, 89 }; byte[] arr4 = new byte[] { 13, 16, 56, 78, 98, 99 };Now, using the Arrays.equals() method, we will be compare two arrays.Arrays.equals(arr1, arr2);In the same way, other arrays would be compared one by one.Let us see the complete example to compare two ... Read More

Add Auto Increment to Column in MySQL Database using phpMyAdmin

karthikeya Boyini
Updated on 26-Jun-2020 10:21:02

10K+ Views

You can add auto_increment to a column in MySQL database with the help of ALTER command.The syntax is as follows −ALTER TABLE yourTableName MODIFY yourColumnName INT NOT NULL AUTO_INCREMENT;To open PhpMyAdmin on localhost, you need to type the following on localhost and press enter −localhost/phpmyadminThe screenshot is as follows −Above, we already have a table “AutoIncrementDemo”. In that, we have a column “UserId” set as Primary key. Let’s say we need to add auto_increment to the same column.For auto_increment, check the A.I as shown above. The same is marked below as well −After that press the Save button.Let us also ... Read More

Convert Unsigned Byte to Java Type

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

297 Views

Firstly, let us declare byte values.byte val1 = 127; byte val2 = -128;To convert the above given unsigned byte, you can use the following. Here, we are first implementing it for the variable “val1”.(int) val1 & 0xFFNow for the second variable “val2”.(int) val2 & 0xFFLet us see the complete example to convert an UNSIGNED bye to a JAVA type.Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       byte val1 = 127;       byte val2 = -128;       System.out.println(val1);       System.out.println((int) val1 & 0xFF);       System.out.println(val2);       System.out.println((int) val2 & 0xFF);    } }Output127 127 -128 128

Convert Byte to Hexadecimal Equivalent in Java

Samual Sam
Updated on 26-Jun-2020 10:16:08

1K+ Views

To convert a byte to hexadecimal equivalent, use the toHexString() method in Java.Firstly, let us take a byte value.byte val1 = (byte)90;Before using the method, let us do some more manipulations. Mask the byte value now:int res = val1 & 0xFF;Let us now see the complete example and use the toHexString() method to convert a byte to hexadecimal equivalent.Example Live Demopublic class Demo {    public static void main(String[] args) {       byte val1 = (byte)90;       System.out.println("Byte = "+val1);       int res = val1 & 0xFF;       System.out.println("Hexadecimal = "+Integer.toHexString(res));    } }OutputByte = 90 Hexadecimal = 5a

Integer LowestOneBit Method in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:15:40

114 Views

The method Integer.lowestOneBit() returns an int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified int value.Here we have a decimal value 294, whose binary is −100100110The lowest one bit is calculated using the lowestOneBit() method in Java.Example Live Demopublic class Demo {    public static void main(String []args) {       // binary 100100110       int dec = 294;       System.out.println("Count of one bits = " + Integer.bitCount(dec));       System.out.println("Lowest one bit: " + Integer.lowestOneBit(dec));    } }OutputCount of one bits = 4 Lowest one bit: 2

Multiply Integers and Check for Overflow in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:14:54

956 Views

To check for Integer overflow, we need to check the Integer.MAX_VALUE with the multiplied integers result, Here, Integer.MAX_VALUE is the maximum value of an integer in Java.Let us see an example wherein integers are multiplied and if the result is more than the Integer.MAX_VALUE, then an exception is thrown.The following is an example showing how to check for Integer overflow.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 9898;       int val2 = 6784;       System.out.println("Value1: "+val1);       System.out.println("Value2: "+val2);       long mul ... Read More

Check Whether a Character is Lowercase in Java

Samual Sam
Updated on 26-Jun-2020 10:13:14

11K+ Views

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

Check If a Table Is Empty in MySQL Using EXISTS

karthikeya Boyini
Updated on 26-Jun-2020 10:13:06

3K+ Views

The following is the syntax to check whether a table is empty or not using MySQL EXISTS −SELECT EXISTS(SELECT 1 FROM yourTableName);ExampleFirst, let us create a table. The query to create a table is as follows −mysql> create table ReturnDemo    -> (    -> Id int,    -> Name varchar(10)    -> ); Query OK, 0 rows affected (0.79 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into ReturnDemo values(100, 'Larry'); Query OK, 1 row affected (0.18 sec) mysql> insert into ReturnDemo values(101, 'Bob'); Query OK, 1 row affected (0.28 sec) ... Read More

Convert Integer to Boolean in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:12:38

11K+ Views

To convert integer to boolean, firstly let us initialize an integer.int val = 100;Now we will declare a variable with primitive boolean. While declaration, we will initialize it with the val value comparing it with an integer using the == operator. If the value matches, the value “True” is returned, else “False” is returned.boolean bool = (val == 100);Let us now see the complete example displaying how to convert integer to boolean.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val = 100;       System.out.println("Integer: "+val);       boolean bool ... Read More

Advertisements