Found 9105 Articles for Object Oriented Programming

Infinite while loop in Java

Syed Javed
Updated on 27-Feb-2020 05:25:01

562 Views

Yes. Following while loop is a valid statement and causes an infinite loop.while(true);

Java labelled for loop

Vikyath Ram
Updated on 15-Jun-2020 09:14:32

2K+ Views

Following program is using labeled for loops.ExampleLive Demopublic class Tester {    public static void main(String args[]) {             first:          for (int i = 0; i < 3; i++) {             for (int j = 0; j< 3; j++){                if(i == 1){                   continue first;                }                      System.out.print(" [i = " + i + ", ... Read More

Java labelled statement

Kumar Varma
Updated on 15-Jun-2020 09:06:36

4K+ Views

Yes. Java supports labeled statements. You can put a label before a for statement and use the break/continue controls to jump to that label. ExampleSee the example below.Live Demopublic class Tester {    public static void main(String args[]) {       first:          for (int i = 0; i < 3; i++) {             for (int j = 0; j < 3; j++){                if(i == 1){                   continue first;               ... Read More

Differences between | and || operators in Java

Vikyath Ram
Updated on 30-Jul-2019 22:30:21

5K+ Views

| is a bitwise operator and compares each operands bitwise.It is a binary OR Operator and copies a bit to the result it exists in either operands.Assume integer variable A holds 60 and variable B holds 13 then  (A | B) will give 61 which is 0011 1101.Whereas || is a logical OR operator and operates on boolean operands. If both the operands are false, then the condition becomes false otherwise it is true. Assume boolean variable A holds true and variable B holds false then (A && B) is true.| is to be used during bitwise operations and || is useful during logical operations.

Differences between & and && operators in Java.

Kumar Varma
Updated on 30-Jul-2019 22:30:21

8K+ Views

& is a bitwise operator and compares each operand bitwise.It is a binary AND Operator and copies a bit to the result if it exists in both operands.Assume integer variable A holds 60 and variable B holds 13 then  (A & B) will give 12 which is 0000 1100.Whereas && is a logical AND operator and operates on boolean operands. If both the operands are true, then the condition becomes true otherwise it is false. Assume boolean variable A holds true and variable B holds false then (A && B) is false.& is to be used during bitwise operations and && is useful during logical operations.

Java Unary Operator Examples

Ayyan
Updated on 30-Jul-2019 22:30:21

639 Views

The unary operator works on a single operand. Following are the examples of unary operators supported in java. Assume A = 60 and B = 20.OperatorDescriptionExample~ (bitwise compliment)Binary One's Complement Operator is unary and has the effect of 'flipping' bits.(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.++ (Increment)Increases the value of operand by 1.B++ gives 21-- (Decrement)Decreases the value of operand by 1.B-- gives 19

Java Boolean operators

Fendadis John
Updated on 30-Jul-2019 22:30:21

10K+ Views

There are following boolean operators supported by Java language.Assume variable A holds 10 and variable B holds 20, then −OperatorDescriptionExample== (equal to)Checks if the values of two operands are equal or not, if yes then condition becomes true.(A == B) is not true.!= (not equal to)Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.(A != B) is true.> (greater than)Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.< (less than)Checks if the ... Read More

Java Variable Widening Example

Jai Janardhan
Updated on 15-Jun-2020 05:56:52

1K+ Views

Widening refers to passing a lower size data type like int to a higher size data type like long. No casting is required in such a case.public class MyFirstJavaProgram {    public static void main(String []args) {       int a = 300;       long b = a;       System.out.println(b);    } }

Java Variable Narrowing Example

George John
Updated on 30-Jul-2019 22:30:21

2K+ Views

Narrowing refers to passing a higher size data type like int to a lower size data type like short. It may lead to data loss. Casting is required for narrowing conversion. Following program output will be 44. public class MyFirstJavaProgram { public static void main(String []args) { int a = 300; byte b = (byte)a; // narrowing System.out.println(b); } }

Java Conversions and Promotions

Paul Richard
Updated on 15-Jun-2020 05:55:04

158 Views

We can convert one data types into another data type using casting. Narrowing ConversionNarrowing refers to passing a higher size data type like int to a lower size data type like short. It may lead to data loss. Following program output will be 44.public class MyFirstJavaProgram {    public static void main(String []args) {       int a = 300;       byte b = (byte)a; // narrowing       System.out.println(b);    } }Widening/Promotion ConversionWidening refers to passing a lower size data type like int to a higher size data type like long. public class MyFirstJavaProgram {    public ... Read More

Advertisements