Nested Classes in Java

AmitDiwan
Updated on 20-Sep-2019 11:09:52

1K+ Views

Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. Nested classes are divided into two types −Non-static nested classes (Inner Classes) − These are the non-static members of a class.Static nested classes − These are the static members of a class.Following are the types of Nested classes in Java −Non-static nested classes (Inner Classes)Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a ... Read More

Java Integer compareUnsigned Method

AmitDiwan
Updated on 20-Sep-2019 11:06:25

221 Views

The compareUnsigned() method compares two integer objects numerically considering the values as unsigned.The return value if 0 if both values are equal; -1 if the val1is less than val2. The return value is 1, if the val1 is more than val2.At first, set two Integer objects −int val1 = 50; int val2 = -10;Now, compare them considering the values as unsigned −System.out.println(Integer.compareUnsigned(val1, val2));Following is an example to implement the compareUnsigned() method in Java −Examplepublic class Main {    public static void main(String[] args) {       int val1 = 50;       int val2 = -10;     ... Read More

Java Integer compareTo Method

AmitDiwan
Updated on 20-Sep-2019 11:05:01

8K+ Views

The java.lang.Integer.compareTo() method compares two Integer objects numerically. This method returns the value 0 if this Integer is equal to the argument Integer, a value less than 0 if this Integer is numerically less than the argument Integer and a value greater than 0 if this Integer is numerically greater than the argument Integer.At first, set two Integer objects −Integer obj1 = new Integer("100"); Integer obj2 = new Integer("200");Now, compare those object −int res = obj1.compareTo(obj2);Following is an example to implement the compareTo() method in Java −Examplepublic class Main {    public static void main(String[] args) {       Integer obj1 ... Read More

Java Integer byteValue Method

AmitDiwan
Updated on 20-Sep-2019 11:01:11

218 Views

The byteValue() method returns the value of this Integer as a byte.Following is an example to implement the byteValue() method in Java −Examplepublic class Main {    public static void main(String[] args) {       Integer val = new Integer(10);       byte res = val.byteValue();       System.out.println("Value = " + res);    } }OutputValue = 10Let us see another example −Exampleimport java.util.*; public class Main {    public static void main(String[] args) {       Byte b = new Byte("10");       byte res = b.byteValue();       System.out.println("Byte = " + b );       System.out.println("Primitive byte = "+ res);    } }OutputByte = 80 Primitive byte = 80

Java Integer BitCount Method

AmitDiwan
Updated on 20-Sep-2019 10:58:54

203 Views

The java.lang.Integer.bitCount() method returns the number of one-bits in the two's complement binary representation of the specified int value.At first, set an int value −int val = 210;Now, find the number of one-bits −Integer.bitCount(val)Following is an example to implement the bitCount() method in Java −Examplepublic class Main {    public static void main(String[] args) {       int val = 210;       System.out.println("Number = " + val);       System.out.println("Binary = " + Integer.toBinaryString(val));       // returns the number of one-bits       System.out.println("Number of one bits = " + Integer.bitCount(val));    } }OutputNumber = 210 Binary = 11010010 Number of one bits = 4

Check If a Value Is Present in an Array in Java

AmitDiwan
Updated on 20-Sep-2019 10:57:19

830 Views

At first sort the array −int intArr[] = {55, 20, 10, 60, 12, 90, 59}; // sorting array Arrays.sort(intArr);Now, set the value to be searched in an int variable −int searchVal = 12;Check for the presence of a value in an array −int retVal = Arrays.binarySearch(intArr, searchVal); boolean res = retVal > 0 ? true : false;Following is an example to check if a value is present in an array −Exampleimport java.util.Arrays; public class Main {    public static void main(String[] args) {       // initializing unsorted int array       int intArr[] = {55, 20, 10, ... Read More

Check If a String Starts With Any Given Prefixes in Java

AmitDiwan
Updated on 20-Sep-2019 10:54:48

522 Views

Let’s say the string is −String str = "Malyalam";Let’s say the prefixes are in an array −String[] prefixArr = { "Ga", "Ma", "yalam" };Now, to check whether the string starts with any of the abive prefix, use the startsWith() −if (Stream.of(prefixArr)    .anyMatch(str::startsWith))    System.out.println("TRUE"); else    System.out.println("FALSE");Following is an example to check is a string starts with any of the given prefixes −Exampleimport java.util.stream.Stream; class Main {    public static void main(String[] args) {       String str = "Malyalam";       String[] prefixArr = { "Ga", "Ma", "yalam" };       if (Stream.of(prefixArr)     ... Read More

HTML DOM TableHeader abbr Property

AmitDiwan
Updated on 20-Sep-2019 10:43:20

60 Views

The HTML DOM TableHeader abbr property returns and modify the value of abbr attribute of a table in an HTML document.SyntaxFollowing is the syntax −1. Returning abbrobject.abbr2. Adding abbrobject.abbr = “text”Let us see an example of abbr property:Example Live Demo    body {       color: #000;       background: lightblue;       height: 100vh;       text-align: center;    }    table {       margin: 2rem auto;       width: 400px;    }    .btn {       background: #db133a;       border: none;       height: 2rem; ... Read More

HTML DOM TableRow Object

AmitDiwan
Updated on 20-Sep-2019 10:39:14

112 Views

The HTML DOM TableRow Object represent the element of an HTML document.Create TableRow objectSyntaxFollowing is the syntax −document.createElement(“TR”);Properties of TableRow objectPropertyExplanationrowIndexIt returns the position of a row in the rows collection of a table.sectionRowIndexIt returns the position of a row in the rows collection of a thead, tbody, or tfoot.Methods of TableRow objectMethodExplanationdeleteCell()It removes a cell from the current table row.insertCell()It adds a cell into the current table row.Let us see an example of TableRow object:Example Live Demo    body {       color: #000;       background: lightblue;       height: 100vh;     ... Read More

HTML DOM TableHeader Object

AmitDiwan
Updated on 20-Sep-2019 10:32:21

100 Views

The HTML DOM TableHeader Object represent the element of an HTML document.Create TableHeader objectSyntaxFollowing is the syntax −document.createElement(“TH”);Properties of TableHeader objectPropertyExplanationcellIndexIt returns the position of a cell in the cell collection of a table row.colSpanIt returns and alter the value of colspan attribute of a table.headersIt returns and alter the value of headers attribute of a table.abbrIt returns and alter the value of abbr attribute of a table.rowSpanIt returns and alter the value of rowspan attribute of a tableLet us see an example of TableHeader object:Example Live Demo    body {       color: #000;     ... Read More

Advertisements