Atomic Variables in Java

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

449 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(); } }

Iterate Over Dictionaries Using For Loops in Python

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

372 Views

Even though dictionary itself is not an iterable object, the items(), keys() and values methods return iterable view objects which can be used to iterate through dictionary. The items() method returns a list of tuples, each tuple being key and value pair. >>> d1={'name': 'Ravi', 'age': 23, 'marks': 56} >>> for t in d1.items(): print (t) ('name', 'Ravi') ('age', 23) ('marks', 56) Key and value out of each pair can be separately stored in two variables and traversed like this − >>> d1={'name': 'Ravi', 'age': 23, 'marks': 56} >>> for k, v in d1.items(): print (k, ... Read More

Get Natural Logarithm of 10 in JavaScript

V Jyothi
Updated on 30-Jul-2019 22:30:21

565 Views

To get the Natural logarithm of 10, use the Math.LN10 property in JavaScript. It returns the natural logarithm of 10, which is approximately 2.302.You can try to run the following code to get Natural logarithm of 10:Example Live Demo           JavaScript Math LN10 Property                        var property_value = Math.LN10;          document.write("Property Value: " + property_value);          

Function Overloading in JavaScript

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

2K+ Views

JavaScript does not support Function Overloading. The following shows function overloading − function funcONE(x,y) { return x*y; } function funcONE(z) { return z; } The above will not show an error, but you won't get desired results. On calling, // prints 5 funcONE(5); // prints 5, not 30 funcONE(5,6); JavaScript does not support function overloading natively. If we will add functions with the same name and different arguments, it considers the last defined function.

Use SELECT Statement as Argument of MySQL IF Function

Sai Subramanyam
Updated on 30-Jul-2019 22:30:21

554 Views

It is quite possible to use a SELECT statement as the first argument of MySQL IF() function. To make it understand, consider the data as follows from table ‘Students’. mysql> Select * from Students; +----+-----------+-----------+----------+----------------+ | id | Name | Country | Language | Course | +----+-----------+-----------+----------+----------------+ | 1 | Francis | UK | English | Literature | | 2 | Rick | ... Read More

What is JavaScript Garbage Collection

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:21

341 Views

JavaScript automatically allocates memory, while a variable is declared. Garbage collection finds memory no longer used by the application and releases it since it is of no use. Garbage collector uses algorithms like Mark-and-sweep algorithm, to find the memory no longer used.This algorithm is used to free memory when an object is unreachable. The garbage collector identifies the objects, which are reachable or unreachable. These unreachable objects get the treatment from the automatic garbage collector.The Reference-Counting Garbage Collection is also used for garbage collection in JavaScript. The object will get automatically garbage collected if there are no references to it. ... Read More

Access Python Dictionary in Simple Way

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

176 Views

There are two ways available to access value associated with a key in a dictionary collection object. The dictionary class method get() takes key as argument and returns value. >>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56} >>> d1.get('age') 23 Another way is to use key inside square brackets in front of dictionary object >>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56} >>> d1['age'] 23

When to Use Inline Scripts vs External JavaScript Files

Sai Subramanyam
Updated on 30-Jul-2019 22:30:21

1K+ Views

To enhance performance, try to keep JavaScript external. The separate code makes it easier for web browsers to cache. However, use inline scripts only when you’re making single page websites. Still, it's better to use external code i.e. external JavaScript. To place JavaScript in external file create an external JavaScript file with the extension .js. After creating, add it to the HTML file in the script tag. The src attribute is used to include that external JavaScript file. If you have more than one external JavaScript file, then add it to the same web page to increase the performance of ... Read More

Why Java is Both Compiled and Interpreted Language

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

809 Views

Yes, a java program is first compiled into bytecode which JRE can understand. ByteCode is then interpreted by the JVM making it as interpreted language.

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); } }

Advertisements