Found 9326 Articles for Object Oriented Programming

Display default initial values of DataTypes in Java

Samual Sam
Updated on 26-Jun-2020 10:27:05

2K+ Views

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.Example Live Demopublic 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 More

Display the limits of DataTypes in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:27:50

513 Views

Every data type in Java has a minimum as well as maximum range, for example, for Integer.Minimum = -2147483648 Maximum = 2147483647Let’s say for Integer, if the value extends the maximum range display above, it leads to Overflow. However, if the value is less than the minimum range displayed above, it leads to Underflow.The following program displays the limits on datatypes in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Limits of primitive DataTypes");       System.out.println("Byte Datatype values...");       System.out.println("Min = " + Byte.MIN_VALUE);       System.out.println("Max = ... Read More

Integer.numberOfTrailingZeros() method in Java

Samual Sam
Updated on 26-Jun-2020 10:28:15

91 Views

The Integer.numberOfTrailingZeros() method returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value.We have the following decimal as an example.int dec = 199;Calculated binary using Integer.toBinaryString() as shown below −Integer.toBinaryString(dec);Now let us see the implementation of Integer.numberOfTrailingZeros() method.Example Live Demopublic class Demo {    public static void main(String []args) {       int dec = 199;       System.out.println("Binary = " + Integer.toBinaryString(dec));       System.out.println("Count of one bits = " + Integer.bitCount(dec));       System.out.println("Number of trailing zeros : " + Integer.numberOfTrailingZeros(dec));    } ... Read More

Integer.numberOfLeadingZeros() method in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:28:42

69 Views

This Integer.numberOfLeadingZeros() method in Java returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified int value.We have the following decimal as an example.int dec = 294;Calculated binary using Integer.toBinaryString() as shown below −Integer.toBinaryString(dec);Now let us see the implementation of Integer.numberOfLeadingZeros() method.Example Live Demopublic class Demo {    public static void main(String []args) {       int dec = 294;       System.out.println("Decimal = " + dec);       System.out.println("Binary = " + Integer.toBinaryString(dec));       System.out.println("Count of one bits = " + Integer.bitCount(dec));       ... Read More

Create an Integer object in Java

Samual Sam
Updated on 26-Jun-2020 10:29:11

5K+ Views

To create an Integer object in Java is quite easy. Let us learn about the following two ways for this purpose.Method1Example Live Demopublic class Demo {    public static void main(String []args) {       Integer obj = new Integer("199");       System.out.println(obj);    } }Output199Method2 −Now let us see the second wayExamplePublic class Demo {    public static void main(String []args) {       Integer obj = new Integer(99);       System.out.println(obj);    } }Output99

Java Program to display Bit manipulation in Integer

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

182 Views

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.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));    } }OutputCount of one bits = 4

Convert byte primitive type to Byte object in Java

Samual Sam
Updated on 26-Jun-2020 10:33:57

741 Views

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.Example Live Demopublic class Demo {    public static void main(String[] args) {       byte b = 100;       Byte res = new Byte(b);       System.out.println(res);    } }Output100

Java Program to convert string to byte

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

792 Views

Use the valueOf() method to convert string to byte. Firstly, let us take a string.String str = "65";Now, use the valueOf() method to convert string to byte.byte res = Byte.valueOf(str);Let us see the complete example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "65";       System.out.println("String: "+str);       byte res = Byte.valueOf(str);       System.out.println("Byte: "+res);    } }OutputString: 65 Byte: 65

Java Program to sort Short array

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

2K+ Views

To sort Short array, use the Arrays.sort() method.Let us first declare and initialize an unsorted Short array.short[] arr = new short[] { 35, 25, 18, 45, 77, 21, 3 };Now, let us short the array.Arrays.sort(arr);The following is the complete example that shows how to sort Short array in Java.Example Live Demoimport java.util.*; public class Demo {    public static void main(String []args) {       short[] arr = new short[] { 35, 25, 18, 45, 77, 21, 3 };       System.out.println("Unsorted:");       for (short s : arr) {          System.out.println(s);       }       System.out.println("Sorted:");       Arrays.sort(arr);       for (short s : arr) {          System.out.println(s);       }       System.out.println();    } }OutputUnsorted: 35 25 18 45 77 21 3 Sorted: 3 18 21 25 35 45 77

Convert Short to numeric primitive data types in Java

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

249 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

Advertisements