Found 9150 Articles for Object Oriented Programming

Integer.signum() method in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:39:47

146 Views

The method returns the signum function of the specified int value. Let us now see the syntax.int signum(int i)Here is the parameter.i − This is the int valueThe return value is -1 if the specified value is negative, 0 if the specified value is zero and 1 if the specified value is positive.Let us now see an example.Example Live Demopublic class Demo {    public static void main(String []args) {       System.out.println("Returns = "+Integer.signum(0));       System.out.println("Returns = "+Integer.signum(-99));       System.out.println("Returns = "+Integer.signum(678));    } }OutputReturns = 0 Returns = -1 Returns = 1

Integer.rotateLeft() method in Java

Samual Sam
Updated on 26-Jun-2020 10:40:18

118 Views

The Integer.rotateLeft() method returns the value obtained by rotating the two's complement binary representation of the specified int value i left by the specified number of bits. The following is the syntax.int rotateLeft(int i, int distance)Here are the parameters.i − This is the int value.distance − This is the rotation distance.Example Live Demopublic class Demo {    public static void main(String []args) {       int val = 1;       for (int i = 0; i < 4; i++) {          val = Integer.rotateLeft(val, 1);          System.out.println(val);       }    } }Output2 4 8 16

Boolean Literals in Java

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

789 Views

The Boolean literals have two values i.e. True and False.The following is an example to display Boolean Literals.Example Live Demopublic 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.

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

691 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

148 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

142 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

Revathi Satya Kondra
Updated on 18-Dec-2024 22:42:09

7K+ Views

To create an Integer object in Java is quite easy. Let us learn about the following two ways for this purpose. Before moving to the coding section, let's briefly discuss the Integer class. The Integer class is a wrapper class in Java, which can be used to encapsulate a primitive int value in an object. Using an Integer Constructor You can create an Integer object using the Integer constructor by passing a primitive int value. In Java, a constructor is a special method whose name is exactly the same as the class name. Example The following example creates an Integer ... Read More

Java Program to display Bit manipulation in Integer

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

238 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

992 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

Advertisements