Static and Non-Static Blank Final Variables in Java

AmitDiwan
Updated on 08-Jul-2020 10:28:41

1K+ Views

Static variable: Declared with the help of the keyword ‘static’, they are also known as class variables. They are defined within a constructor or outside a class function. When a variable is static, it is shared between all objects of the class, irrespective of the number of objects that are created.Demonstrating how the ‘static’ keyword, when used with variable works −Example Live Demopublic class Demo{    String name;    static String designation;    public void display_data(){       System.out.println("The name is: " + name);       System.out.println("The designation of this team members is : " + designation);    } ... Read More

Find IP Address of the Client in Java

AmitDiwan
Updated on 08-Jul-2020 10:25:22

2K+ Views

To find the IP Address of the client, the Java code is as follows −Example Live Demoimport java.net.*; import java.io.*; import java.util.*; import java.net.InetAddress; public class Demo{    public static void main(String args[]) throws Exception{       InetAddress my_localhost = InetAddress.getLocalHost();       System.out.println("The IP Address of client is : " + (my_localhost.getHostAddress()).trim());       String my_system_address = "";       try{          URL my_url = new URL("http://bot.whatismyipaddress.com");          BufferedReader my_br = new BufferedReader(new          InputStreamReader(my_url.openStream()));          my_system_address = my_br.readLine().trim();       }   ... Read More

Find the Second Most Repeated Word in a Sequence in Java

AmitDiwan
Updated on 08-Jul-2020 10:20:18

684 Views

To find the second most repeated word in a sequence in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    static String second_repeated(Vector my_seq){       HashMap my_map = new HashMap(my_seq.size()){          @Override          public Integer get(Object key){             return containsKey(key) ? super.get(key) : 0;          }       };       for (int i = 0; i < my_seq.size(); i++)       my_map.put(my_seq.get(i), my_map.get(my_seq.get(i))+1);       int first_val = Integer.MIN_VALUE;       int sec_val ... Read More

Find the First Repeated Word in a String in Java

AmitDiwan
Updated on 08-Jul-2020 10:17:39

962 Views

To find the first repeated word in a string in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    static char repeat_first(char my_str[]){       HashSet my_hash = new HashSet();       for (int i=0; i

Find Uncommon Values Concatenated from Both Strings in Java

AmitDiwan
Updated on 08-Jul-2020 10:16:20

280 Views

To find the uncommon values concatenated from both the strings in Java, the code is as follows −Example Live Demoimport java.util.*; import java.lang.*; import java.io.*; public class Demo{    public static String concat_str(String str_1, String str_2){       String result = "";       int i;       HashMap my_map = new HashMap();       for (i = 0; i < str_2.length(); i++)       my_map.put(str_2.charAt(i), 1);       for (i = 0; i < str_1.length(); i++)       if (!my_map.containsKey(str_1.charAt(i)))       result += str_1.charAt(i);       else       ... Read More

Find All Tables Containing ColumnA and ColumnB in MySQL

AmitDiwan
Updated on 08-Jul-2020 09:16:49

154 Views

To find specific column names, use information_schema.columns Here, I am using Id in place of columnA and Name in place of columnB −mysql> select table_name as TableNameFromWebDatabase    -> from information_schema.columns    -> where column_name IN ('Id', 'Name')    -> group by table_name    -> having count(*) = 3;This will produce the following output. Following are the table with columns Id and Name −+--------------------------+ | TableNameFromWebDatabase | +--------------------------+ | student                  | | distinctdemo             | | secondtable              | | groupconcatenatedemo   ... Read More

Convert Bean to JSON Object Excluding Properties Using JSONConfig in Java

raja
Updated on 08-Jul-2020 09:05:50

1K+ Views

The JsonConfig class is a utility class that helps to configure the serialization process. We can convert a bean to a JSON object with few properties that can be excluded using the setExcludes() method of JsonConfig class and pass this JSON config instance to an argument of static method fromObject() of JSONObject.Syntaxpublic void setExcludes(String[] excludes)In the below example, we can convert bean to a JSON object by excluding some of the properties.Exampleimport net.sf.json.JSONObject; import net.sf.json.JsonConfig; public class BeanToJsonExcludeTest {    public static void main(String[] args) {       Student student = new Student("Raja", "Ramesh", 35, "Madhapur");       JsonConfig jsonConfig = new ... Read More

Convert Map to JSON Object Using JSON-lib API in Java

raja
Updated on 08-Jul-2020 08:20:25

6K+ Views

A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value. We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format. Initially, we can create a POJO class and pass this instance as an argument to the put() method of Map class and finally add this map instance to the accumulateAll() method of JSONObject.Syntaxpublic void accumulateAll(Map map)In the below example, we can convert Map to a JSON object.Exampleimport java.util.*; import net.sf.json.JSONObject; public class ConvertMapToJSONObjectTest { ... Read More

Output Only the Name of the Month in MySQL

AmitDiwan
Updated on 08-Jul-2020 08:09:12

169 Views

To display only the month number, use DATE_FORMAT() along with STR_TO_DATE(). Let us first create a table:mysql> create table DemoTable1320 -> ( -> MonthName varchar(20) -> ); Query OK, 0 rows affected (0.43 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable1320 values('10/10/2010'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1320 values('11/12/2018'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1320 values('12/01/2019'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable1320;Output+------------+ | MonthName  | +------------+ | 10/10/2010 | | 11/12/2018 | ... Read More

MySQL Query to Count Dates and Fetch Repeated Dates

AmitDiwan
Updated on 08-Jul-2020 08:08:40

220 Views

To display the count, use aggregate function COUNT(*). Let us first create a table −mysql> create table DemoTable1321 -> ( -> ArrivalDatetime timestamp -> ); Query OK, 0 rows affected (0.50 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable1321 values(now()); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1321 values('2019-01-10 12:34:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1321 values('2019-06-12 11:34:00'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1321 values('2019-06-12 04:50:00'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1321 values('2019-09-18 10:50:45'); Query OK, 1 ... Read More

Advertisements