Found 272 Articles for Java8

Reference to a constructor using method references in Java8

Maruthi Krishna
Updated on 08-Apr-2020 14:09:28

551 Views

Lambda expressions In Java allows you to pass functionality as an argument to a method. You can also call an existing method using lambda expressions.list.forEach(n -> System.out.println(n));Method references are simple, easy-to-read lambda expressions to call/refer and the existing method by name in a lambda expression. In addition to the instance and static methods, you can also refer a constructor by using the new keyword.SyntaxFollowing is the syntax to reference a constructor in Java.ClassName::newExampleinterface myInterface{    Test greet(String data); } class Test{    Test(String data){          System.out.println(data);    } } public class MethodReferences {    public static void ... Read More

Java DatabaseMetaData getSQLKeywords() method with example

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

138 Views

This method retrieves the list of all the SQL keywords of the underlying database and returns in the form of a String variable which holds all the keywords separated by commas.To get the list of keywords in the database −Make sure your database is up and running.Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.Get the connection object using the getConnection() method of the DriverManager class. Pass the URL the database and, user name, password of a user in the database, as String variables.Get the DatabaseMetaData ... Read More

How to run JavaTuples program in Eclipse?

Amit Diwan
Updated on 22-Aug-2023 12:15:43

525 Views

Tuples in Java are an ordered collection of objects of different types. To run Tuple in Java, you need to upload an external jar file. Here, we will be using Eclipse IDE to create a new Java Project and upload the JavaTuples external jar file.The JavaTuples jar file is to be downloaded. Let us first download the jar file, create an Eclipse project and import the downloaded jar file. Here are the steps −Step 1 − Download JavaTuples Jar library and save it on your system.Open the GitHib link and download the “javatuples-1.2-dist.zip” as shown below −After downloading, unzip and ... Read More

Where objects, methods and variables are stored in memory in Java?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

3K+ Views

There are five main memory areas which are used to various Java elements. Following is the list of the same. Class Area - This area contains the static members of the class. Method Area - This area contains the method definition and executable code. Heap Area - This area contains the objects which are dynamically allocated/deallocated. if an object is no more referenced by any live reference it is deallocated. Stack Area - This area contains the local variables. Pool Area - Contains immutable objects like string.

volatile Keyword in Java

Chandu yadav
Updated on 23-Jun-2020 15:30:10

5K+ Views

The volatile modifier is used to let the JVM know that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory.Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.Examplepublic class MyRunnable implements Runnable {    private volatile boolean active;    public void run() {       active = true;       while (active) {       }    }    public void stop() {       active = false;    } }

Java program to check string as palindrome

Arjun Thakur
Updated on 23-Jun-2020 15:21:47

3K+ Views

A string is Palindrome if position of each character remain same in case even string is reversed.For example 'MADAM' is a palidrome string as position of each character remain same even if string 'MADAM' is reversed.Now in order to identify a string as palindrome or not we can use library method approach and also without library method approach.But if we want to check if "Madam" is palindrome or not , it will show us that it is not a palindrome because of the upper case of first letter.Example - Without library method. Live Demopublic class Palindrome {    public static void ... Read More

Java program to check occurrence of each vowel in String

Chandu yadav
Updated on 23-Jun-2020 15:22:32

2K+ Views

To count occurence of vowels in a string again use Map utility of java as used in calculating occurence of each character in string.Put each vowel as key in Map and put initial value as 1 for each key.Now compare each character with key of map if a character matches with a key then increase its corresponding value by 1.Examplepublic class OccurenceVowel {    public static void main(String[] args) {       String str = "AEAIOG";       LinkedHashMap hMap = new LinkedHashMap();       hMap.put('A', 0);       hMap.put('E', 0);       hMap.put('I', 0);       hMap.put('O', 0);       hMap.put('U', 0);       for (int i = 0; i

instanceof operator vs isInstance method in java

Chandu yadav
Updated on 23-Jun-2020 15:18:19

207 Views

isInstance method is equivalent to instanceof operator. The method is used in case of objects are created at runtime using reflection. General practice says if type is to be checked at runtime then use isInstance method otherwise instanceof operator can be used. See the example below −Example Live Demopublic class Tester{    public static void main(String[] args) throws ClassNotFoundException {       Integer i = new Integer(10);       System.out.println(usingInstanceOf(i));       System.out.println(usingIsInstance(i));    }    public static String usingInstanceOf(Object i){       if(i instanceof String){          return "String";       } ... Read More

Java program to check occurrence of each character in String

Chandu yadav
Updated on 23-Jun-2020 14:22:58

17K+ Views

In order to find occurence of each character in a string we can use Map utility of Java.In Map a key could not be duplicate so make each character of string as key of Map and provide initial value corresponding to each key as 1 if this character does not inserted in map before.Now when a character repeats during insertion as key in Map increase its value by one.Continue this for each character untill all characters of string get inserted.Examplepublic class occurenceOfCharacter {    public static void main(String[] args) {       String str = "SSDRRRTTYYTYTR";       ... Read More

Generating random numbers in Java

Vikyath Ram
Updated on 21-Jun-2020 15:16:08

1K+ Views

We can generate random numbers using three ways in Java.Using java.util.Random class − Object of Random class can be used to generate random numbers using nextInt(), nextDouble() etc. methods.Using java.lang.Math class − Math.random() methods returns a random double whenever invoked.Using java.util.concurrent.ThreadLocalRandom class − ThreadLocalRandom.current().nextInt() method and similar othjer methods return a random numbers whenever invoked.Exampleimport java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Tester {    public static void main(String[] args) {       generateUsingRandom();       generateUsingMathRandom();       generateUsingThreadLocalRandom();    }    private static void generateUsingRandom() {       Random random = new Random(); ... Read More

1 2 3 4 5 ... 28 Next
Advertisements