Found 267 Articles for Java8

Java Unary Operator Examples

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

473 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 Operators Precedence

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

2K+ Views

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3 * 2 and then adds into 7.Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators ... Read More

Java Boolean operators

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

8K+ 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

791 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

113 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

Java overflow and underflow

Arjun Thakur
Updated on 15-Jun-2020 05:47:33

3K+ Views

OverflowOverflow occurs when we assign such a value to a variable which is more than the maximum permissible value.UnderflowUnderflow occurs when we assign such a value to a variable which is less than the minimum permissible value.JVM does not throw any exception in case Overflow or underflow occurs, it simply changes the value. Its programmer responsibility to check the possibility of an overflow/underflow condition and act accordingly. Example (Overflow)Consider the case of int variable, it is of 32 bit and any value which is more than Integer.MAX_VALUE (2147483647) is rolled over. For example, Integer.MAX_VALUE + 1 returns -2147483648 (Integer.MIN_VALUE).As int data ... Read More

Java variable declaration best practices

Fendadis John
Updated on 15-Jun-2020 05:46:54

1K+ Views

Following are the best practices while declaring a variable.Variables names should be short or long enough as per the scope. For example, loop counter variable, i is fine whereas employee as a loop variable.Specific words should not be used as equals, compare, data.Use meaningful names which can explain the purpose of the variable. For example cnt Vs counter.Don't use _ to declare a variable name, Use camel casing. For example, employeeName is better than employee_name.Each organization has its own syntax specific standards. Follow those rules to maintain consistency and readability.

Atomic variables in Java

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

139 Views

Yes, from Java 8 onwards, java.util.concurrent.atomic package contains classes which support atomic operations on single variables preventing race conditions or do not face synchronization issues. All classes in the atomic package have get/set methods. Each set method has a happens-before relationship with any subsequent get() method call on the same variable. import java.util.concurrent.atomic.AtomicInteger; class AtomicCounter { private AtomicInteger counter = new AtomicInteger(0); public void increment() { counter.incrementAndGet(); } public void decrement() { counter.decrementAndGet(); } public int value() { return counter.get(); } }

Global variables in Java

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

720 Views

There is no global variables support in Java. Static variables can be used as an alternate solution for global variables.

Advertisements