Found 346 Articles for Java Programming

Java Variable Widening Example

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

696 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

105 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

120 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

679 Views

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

Static variables in Java

Paul Richard
Updated on 04-Oct-2023 12:40:34

23K+ Views

Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it. Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final, and static. Constant variables never change from ... Read More

Instance variables in Java

Rishi Raj
Updated on 24-Feb-2020 05:08:46

19K+ Views

Instance variables are declared in a class, but outside a method, constructor or any block.When space is allocated for an object in the heap, a slot for each instance variable value is created.Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.Instance variables can be declared at the class level before or after use.Access modifiers can be given ... Read More

Member variables vs Local variables in Java

Vikyath Ram
Updated on 24-Feb-2020 05:31:57

1K+ Views

Local VariableLocal variables are declared in methods, constructors, or blocks.Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.Access modifiers cannot be used for local variables.Local variables are visible only within the declared method, constructor, or block.Local variables are implemented at stack level internally.There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.Instance/Member VariableInstance variables are declared in a class, but outside a method, constructor or any block.When a ... Read More

Advertisements